我在一个角度项目上有一个奇怪的错误,这些是代码片段@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}); } } );}
1 回答
开满天机
TA贡献1786条经验 获得超12个赞
问题是 firebase 在 Angular 的视图线程之外请求用户令牌,因此我必须在 Angular 的视图线程中更新模型。
this.ngZone.run(() =>{
this.user.webPushEnabled = true;
})
它帮助了我。
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报
0/150
提交
取消