When receiveMessage is triggered, the FCM push notification fails to refresh the view

After following this helpful tutorial on Push Notifications with Angular 6 + Firebase Cloud Messaging, everything is working perfectly. I am able to receive notifications when using another browser.

To ensure that my notification list and the length of notifications get refreshed, I need to include the onInit() function in the code snippet below:

receiveMessage() { 
    this.angularFireMessaging.messages.subscribe( 
    (payload) => { 
    console.log("new message received. ", payload); 
    this.currentMessage.next(payload); 
    this.navBar.onInit();
    }) 
} 

I have a Spring Boot backend setup and only require the refreshing of the notification list within the NavBar component.

Using FCM, I generate tokens for users in order to send notifications to others whenever updates are made in my Postgres DB.

Despite trying to store the payload value outside the function and even using CurrentMessage.next(), the value seems to be null once outside the receiveMessage() function. Is there a workaround to properly refresh my list?

Answer №1

I was looking to create a list of unread notifications from my BackEnd:

 
 ngOnInit() {
// ......... some other code
  $("body").on('DOMSubtreeModified', ".get-notification",  () =>{
        
        if($('.get-notification').text() != "" && $('.get-notification').text() != ""+this.listNotifLength){
            this.GetUnreadNotification();
           // console.log($('.get-notification').text());
            $('.notification').removeClass('get-notification');
          }
       
        });
   }
 
 GetUnreadNotification() {
 //....... some other code
  $('.notification').addClass('get-notification');
                $('.get-notification').text(this.listNotifLength );
                
                
                }
                            <span class="notification" *ngIf="listNotifLength != -1" >{{listNotifLength}}</span>

It's not the most efficient way, but it gets the job done. If anyone has a better suggestion, I'm all ears. Don't forget to include this.GetUnreadNotification() inside the this.angularFireMessaging.messages.subscribe( (payload) => { .... })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Execute async functions sequentially with a queue and limit to only one function running at a time

Within my javascript code, there is an async function named generateThumbnail. This function is triggered by a specific event, such as clicking on a button. The issue I'm facing is that running the function multiple times concurrently leads to a memor ...

Could routerLinkActive be used to substitute a class rather than simply appending one?

I have a navigation bar icon that links to an admin route. I want this icon to change its appearance when on that specific route. To achieve this, I can simply replace the mdi-settings-outline class with mdi-settings, displaying a filled version of the sam ...

Troubleshooting: My jQuery script for fading in content upon document ready is not

I'm currently working on a website where I want everything to fade in when the user enters the site. Take a look at my code: var celyLogin = $(".container"); $(document).ready(function () { /*! Fades in page on load */ $("container") ...

Generate a fresh DOM element when a particular height threshold is achieved, utilizing a portion of the previous DOM element's content

Update: 27th December 2016 The heading has been modified because any DOM element can be the target, whether it is a <p> element or not. Additional information has been provided about the tools being used and the desired outcome. Are there nativ ...

Using React.js to add MongoDB documents into the database

Is there a way to directly insert documents into a MongoDB collection from a React component? I have been working on a personal project, which is a chat web application used for training purposes. For instance, when a user wants to post a new message in a ...

Is there a way to postpone these animations without relying on setTimeout?

As I develop a single-page website, my goal is to smoothly transition between sections by first animating out the current content and then animating in the new content. Currently, I have achieved this using setTimeout(), where I animate out the current con ...

Successfully running npm update on a Mac seemed to have updated the package, however, the

I needed to upgrade my npm version using the following command: npm install npm@latest -g After running the above command, I encountered the following output: /Users/ariful.haque/.npm-global/bin/npm -> /Users/ariful.haque/.npm-global/lib/node_modules ...

What causes a component to not update when it is connected to an Array using v-model?

Array1 https://i.stack.imgur.com/cY0XR.jpg Array are both connected to the same Array variable using v-model. However, when changes are made to Array1, Array2 does not update. Why is this happening? Process: Upon examining the logs, it can be observed th ...

toggle visibility on click using Angular

Within my Angular application, there is a plus button: <div style="display:inline-block;text-align:left;margin-bottom:2vh;" class="col-md-10 col-md-offset-2"> <button type="button" class="btn btn-default" (click)="MoreSentences(result.w ...

What is the method for altering the color of the webkit-slider-thumb using JavaScript?

I am looking to adjust the color of an input range using JavaScript instead of CSS. Can someone help me with this request? Current CSS Code: input[type='range']::-webkit-slider-thumb { -webkit-appearance: none; background: goldenrod !importa ...

Utilizing the random function in a loop can result in unpredictable and unexpected values

Why is it that I get numbers ranging from 17 to 70,000 in the three console.log statements? However, in the loop, y always seems to fall between 200 and 800. Why is that? console.log("RND " + Math.floor(Math.random()*5000)*17) console.log("RND " + Math ...

Is it possible to modify the CSS styles for a specific portion of an input value?

Currently, I am in the process of developing a form using React.js where a specific input must match the label written above it. Here is an example: https://i.stack.imgur.com/S2wOV.png If there happens to be a typo, the text should turn red like this: h ...

I am looking to halt the AJAX requests within an asynchronous function after reaching a limit of 10 requests

I've been working on an async function that sends AJAX requests based on the previous response. The function is functioning properly, but it's sending multiple requests in quick succession. I need to implement a 5-second interval between each req ...

Angular custom filter applied only when a button is clicked

I have recently started exploring angular custom filters and I am curious to know if there is a way to trigger the filters in an ng-repeat only upon clicking a button. Check out this example on jsfiddle : http://jsfiddle.net/toddmotto/53Xuk/ <div ...

Retrieving Data from Vue JS Store

I am fetching value from the store import {store} from '../../store/store' Here is the Variable:- let Data = { textType: '', textData: null }; Upon using console.log(store.state.testData) We see the following result in the cons ...

Node.js uses Firebase Admin to send notifications to a different service account

Exploring two different routes in nodejs. Let's start with one.js var admin = require("firebase-admin"); var topic = "/topics/news"; var serviceAccount = require("/path/serice-1-firebase-adminsdk.json"); var firebase = admin.initializeApp({ cred ...

Guide on sending a request to an API and displaying the retrieved information within the same Express application

I recently developed a basic express app with API and JWT authentication. I am now attempting to enhance the app by incorporating page rendering through my existing /api/.. routes. However, I am facing challenges in this process. app.use('/', en ...

Issue with Ajax post redirection back to original page

I'm facing an issue with my ajax call where I send multiple checkbox values to a php file for processing and updating the database. The post request and database updates are successful, but the page doesn't return to the calling php file automati ...

What steps are needed to configure ESLint to exclusively analyze .ts files?

I need ESLint to exclusively focus on processing .ts files and not .js files. In order to achieve that, I made a .eslintignore file and included the following lines: *.js **/*.js Nevertheless, it appears that ESLint is disregarding this file. Is there so ...

Ways to display a different div when clicking on a div?

Good afternoon, I've noticed that this question has been asked numerous times before, but none of the solutions provided seem to work for my specific issue. My problem involves a div with the class .title3. I want another div with the class .Content ...