在receiveMessage中调用时,FCM推送通知不会更新视图

时间:2019-04-29 23:07:06

标签: javascript angular typescript firebase firebase-cloud-messaging

我遵循了这个tutorial: Push Notifications with Angular 6 + Firebase Cloud Messaging

一切正常。使用其他浏览器时,我可以看到推送的通知。我需要在此处添加onInit()函数:

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

,因此它可以刷新通知列表和通知长度。

我使用的是Spring Boot后端,我只需要刷新通知列表即可。只有NavBar组件需要刷新。

我正在使用FCM为用户生成令牌,以便在数据库(Postgres)中进行某些更新时可以将通知发送给其他用户。

我什至无法在该函数之外存储有效负载的值。 CurrentMessage.next()无法将有效负载的值保留在receiveMessage()函数之外。

当我console.log CurrentMessage时,值是null函数之外的receiveMessage()。有没有刷新我的列表的方法?

1 个答案:

答案 0 :(得分:0)

我基本上希望从后端获得未读通知的列表:

 
 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>

这不是最好的方法,但它确实有效。如果有人有更好的主张,那就太好了。 不要忘记在this.angularFireMessaging.messages.subscribe()内部调用this.GetUnreadNotification()       (有效载荷)=> {....})