服务工作人员在Chrome推送通知中的GCM注册ID [英] GCM registration id in Service Worker in Push Notification for chrome

查看:212
本文介绍了服务工作人员在Chrome推送通知中的GCM注册ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够发送推送通知,并且在服务人员中,我正在进行服务呼叫,我只是希望将该GCM注册ID发送给该服务呼叫。如何在服务人员中获得注册ID或订阅ID



这里是我的代码

  self.addEventListener('push',function(event){
console.log('Received a local message',event);

var title ='我的';
var body ='New Push Message';
var icon ='refresh_blueicon.png';
var tag ='my-push-tag';

event.waitUntil(
//这里我需要将GCM注册ID /订阅ID与外部服务呼叫绑定


fetch('http:/ ('response.status!== 200'){
console.log('看起来像/状态码:'+
response.status);
抛出新的错误();
}
//检查响应中的文本
()函数(数据){

self.registrati on.showNotification(data.title,{
body:data.msg,
icon:icon,
tag:tag
})
})
})


);
});


self.addEventListener('notificationclick',function(event){
console.log('On notification click:',event.notification.tag);
// Android在您点击它时不会关闭通知
//请参阅:http://crbug.com/463146
event.notification.close();

//这看起来是看当前是否已经打开,
//如果是
则关注event.waitUntil(clients.matchAll({
type:window$ b $ (var i = 0; i< clientList.length; i ++){
var client = clientList [i];
if(function(clientList){
) client.url =='/'&&'focus'in client)
return client.focus();
}
if(clients.openWindow)
return clients .openWindow('/');
}));

});


解决方案

您应该已经在<$如果您已经订阅了用户,c $ c> pushManager 对象。所以像这样的东西应该工作:
$ b $ pre $ registration.pushManager.getSubscription()。然后(函数(订阅){
console.log(获得订阅ID:,subscription.endpoint)
});

这就是整个终端,所以如果你只想要这个id,你可以得到这个:

  subscription.endpoint.split(/)。slice(-1))


I am able to send push notification and in service worker i am making a service call i just to want to send GCM registration id with that service call. How to get registration id or subscription id in service worker

here is my code

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('/');
  }));

});

解决方案

You should already have the subscription available on the pushManager object if you have already subscribed the user. So something like this should work:

registration.pushManager.getSubscription().then(function(subscription) {
  console.log("got subscription id: ", subscription.endpoint)
});

That's the whole endpoint, so if you just want the id you could get this:

subscription.endpoint.split("/").slice(-1))

这篇关于服务工作人员在Chrome推送通知中的GCM注册ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆