订阅时我有注册ID,但是当我收到推送通知时,我无法弄清楚如何在服务工作文件中获取它?
client.js
pushManager.subscribe({ userVisibleOnly: true }).then(({ endpoint }) => {
const registrationId = endpoint.split('https://android.googleapis.com/gcm/send/')[1];
// How do I get this registrationId in service-worker.js below?
});
Run Code Online (Sandbox Code Playgroud)
服务worker.js
self.addEventListener('push', event => {
// I'd like to make a request to server to get the notification data, but I need the registrationId for that. How do I get registration id here?
});
Run Code Online (Sandbox Code Playgroud)
在里面ServiceWorkerGlobalScope,你可以得到有效的ServiceWorkerRegistration,它反过来暴露PushManager,然后给你一个getSubscription()方法,它返回一个Promise解决当前订阅.
有很多层要遵循,但实际的代码非常简单:
self.registration.pushManager.getSubscription().then(subscription => {
// Do something with subscription.endpoint
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1795 次 |
| 最近记录: |