gpt4 book ai didi

angular - Firebase 网络推送通知不起作用 messages.onMessage

转载 作者:行者123 更新时间:2023-12-02 04:20:21 26 4
gpt4 key购买 nike

我已经为我的 PWA ionic 4 应用程序配置了网络推送通知。 Web推送通知正好在选项卡在后台或除我的应用程序中切换时工作。

The issue is when the tab is active I get the push messaging inside application section of chrome inspection but there is no notification fired.

下面是代码:

app.component.ts

async ngOnInit() {
firebase.initializeApp(environment.firebase);
}

ngAfterViewInit() {
this.platform.ready().then(async () => {
await this.notificationsService.requestPermission();
});
}

通知.service.ts
export class NotificationsService {
init(): Promise<void> {
return new Promise<void>((resolve, reject) => {
navigator.serviceWorker.ready.then(
registration => {
// Don't crash an error if messaging not supported
if (!firebase.messaging.isSupported()) {
resolve();
return;
}

const messaging = firebase.messaging();

// Register the Service Worker
messaging.useServiceWorker(registration);

// Initialize your VAPI key
messaging.usePublicVapidKey(environment.firebase.vapidKey);


// Listen to messages when your app is in the foreground
messaging.onMessage(payload => {
console.log("Payload is here", payload);
});
// Optional and not covered in the article
// Handle token refresh
messaging.onTokenRefresh(() => {
messaging
.getToken()
.then((refreshedToken: string) => {
console.log(refreshedToken);
})
.catch(err => {
console.error(err);
});
});

resolve();
},
err => {
reject(err);
}
);
});
}

因此,如果应用程序选项卡在 chrome 中打开,则触发通知时应调用 console.logmessaging.onMessage但它没有被执行。
我在 firebase-messaging-sw.js 中使用了 7.8.0 的 firebase 版本.

最佳答案

当您的应用程序在前台时,您的应用程序将不会收到通知 messaging可观察的,但它会在 messages 上发出可观察的。

所以你应该像这样订阅它

firebase.messages.subscribe((message: any) => {
if (message.notification) {
// Show your notification from here
}
});

关于angular - Firebase 网络推送通知不起作用 messages.onMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60438682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com