9

Tôi có thể gửi thông báo đẩy và nhân viên dịch vụ đang thực hiện cuộc gọi dịch vụ chỉ để gửi id đăng ký GCM với cuộc gọi dịch vụ đó. Làm thế nào để có được id đăng ký hoặc id thuê bao trong công nhân dịch vụID đăng ký GCM trong Công nhân dịch vụ trong Thông báo đẩy cho chrome

đây là mã của tôi

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

}); 
+0

mục đích http là gì: // localhost/pushMsg/Push_Notification/msg.php –

+0

@sanjayradadiya, đây là dữ liệu json hiển thị trong thông báo như tiêu đề, hình ảnh, mô tả ngắn, v.v. – Yogendra

Trả lời

8

Bạn nên đã có đăng ký có sẵn trên đối tượng pushManager nếu bạn đã đăng ký người sử dụng. Vì vậy, một cái gì đó như thế này nên làm việc:

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

Đó là toàn bộ thiết bị đầu cuối, vì vậy nếu bạn chỉ muốn id bạn có thể có được điều này:

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

Cụ thể, 'subscription.endpoint' là thứ bạn muốn trong Chrome 44+ và trong các trình duyệt khác khi chúng thêm hỗ trợ. Nếu bạn vẫn quan tâm đến Chrome 43, hãy đọc http://updates.html5rocks.com/2015/03/push-notificatons-on-the-open-web#supporting-pre-chrome-44-push –

+0

Phải, cảm ơn. Hoặc tôi đoán id có thể được coi là yếu tố cuối cùng của điểm cuối. Tôi đã cập nhật câu trả lời của mình. –

+0

registration.pushManager.getSubscription(). (Chức năng (đăng ký) { console.log ("có id đăng ký:", subscription.endpoint) }); đặt mã ở trên như sau trong trường hợp của tôi: https://android.googleapis.com/gcm/send – Yogendra

Các vấn đề liên quan