猿问

Angular *ngIf 单击页面或 alt+tab 后更新(重新聚焦)

我在一个角度项目上有一个奇怪的错误,这些是代码片段


@Injectable()

export class FirebaseMessagingService {

    public tokenReceivedEmitter: any = new EventEmitter();

    public messageReceivedEmitter: any = new EventEmitter();


constructor(

    private angularFireMessaging: AngularFireMessaging) {

    this.angularFireMessaging.messaging.subscribe(

        (messaging) => {

            messaging.onMessage = messaging.onMessage.bind(messaging);

            messaging.onTokenRefresh = messaging.onTokenRefresh.bind(messaging);

        }

    );

}


/**

 * request permission for notification from firebase cloud messaging

 *

 * @param userId userId

 */

requestPermission(userId) {

    this.angularFireMessaging.requestToken.subscribe(

        (token) => {

            this.tokenReceivedEmitter.emit({status: true, result: token});

        },

        (err) => {

            this.tokenReceivedEmitter.emit({status: false, result: err});

        }

    );

}


/**

 * hook method when new notification received in foreground

 */

receiveMessage() {

    this.angularFireMessaging.messages.subscribe(

        (payload) => {

            this.messageReceivedEmitter.emit(payload);

        });

}

这就是 firebase 消息服务,它在收到推送通知时发出令牌接收事件。


现在在组件中


ngOnInit(){

    // Subscribing to firebase token receive

         this.firebaseTokenSubscription = this.messagingService.tokenReceivedEmitter.subscribe(

            (message) => {

                if (message.status) {

                    const token = message.result;

                    this.sendNotificationToken(token);

                } else {

                    this.snackBar.open(message.result, this.translate.instant('CLOSE') 

                    {duration:3000});

                }

        }

    );

}


慕码人8056858
浏览 100回答 1
1回答

开满天机

问题是 firebase 在 Angular 的视图线程之外请求用户令牌,因此我必须在 Angular 的视图线程中更新模型。this.ngZone.run(() =>{     this.user.webPushEnabled = true;})它帮助了我。
随时随地看视频慕课网APP

相关分类

Html5
我要回答