【问题标题】:FCM push notification does not update view when called in receiveMessage在 receiveMessage 中调用时,FCM 推送通知不会更新视图
【发布时间】:2019-09-18 13:57:27
【问题描述】:

我关注了这个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() 函数之外的值是null。有没有办法刷新我的列表?

【问题讨论】:

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


    【解决方案1】:

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

     
     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( (有效载荷) => { .... })

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 2020-04-21
      • 2017-12-22
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2017-12-09
      • 2020-10-14
      • 1970-01-01
      相关资源
      最近更新 更多