chrome 推送通知中 Service Worker 中的 GCM 注册 ID
GCM registration id in Service Worker in Push Notification for chrome
我能够发送推送通知,并且在服务工作者中我正在进行服务调用,我只想通过该服务调用发送 GCM 注册 ID。如何在 Service Worker 中获取注册 ID 或订阅 ID
这是我的代码
self.addEventListener('push', function(event) {
console.log('Received a push message from local', event);
var title = 'My title file. Testing on';
var body = 'New Push Message.';
var icon = 'refresh_blueicon.png';
var tag = 'my-push-tag';
event.waitUntil(
// Here i need to wind GCM Registration id / Subscription id with external service call
fetch('http://localhost/pushMsg/Push_Notification/msg.php').then(function(response){
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
throw new Error();
}
// Examine the text in the response
return response.json().then(function(data) {
self.registration.showNotification(data.title, {
body: data.msg,
icon: icon,
tag: tag
})
})
})
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn’t close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
如果您已经订阅了用户,您应该已经可以在 pushManager
对象上进行订阅。所以这样的事情应该有效:
registration.pushManager.getSubscription().then(function(subscription) {
console.log("got subscription id: ", subscription.endpoint)
});
这就是整个端点,所以如果您只想要 ID,您可以得到这个:
subscription.endpoint.split("/").slice(-1))
我能够发送推送通知,并且在服务工作者中我正在进行服务调用,我只想通过该服务调用发送 GCM 注册 ID。如何在 Service Worker 中获取注册 ID 或订阅 ID
这是我的代码
self.addEventListener('push', function(event) {
console.log('Received a push message from local', event);
var title = 'My title file. Testing on';
var body = 'New Push Message.';
var icon = 'refresh_blueicon.png';
var tag = 'my-push-tag';
event.waitUntil(
// Here i need to wind GCM Registration id / Subscription id with external service call
fetch('http://localhost/pushMsg/Push_Notification/msg.php').then(function(response){
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
throw new Error();
}
// Examine the text in the response
return response.json().then(function(data) {
self.registration.showNotification(data.title, {
body: data.msg,
icon: icon,
tag: tag
})
})
})
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn’t close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
如果您已经订阅了用户,您应该已经可以在 pushManager
对象上进行订阅。所以这样的事情应该有效:
registration.pushManager.getSubscription().then(function(subscription) {
console.log("got subscription id: ", subscription.endpoint)
});
这就是整个端点,所以如果您只想要 ID,您可以得到这个:
subscription.endpoint.split("/").slice(-1))