2017-05-15 10 views
6

पर क्लिक करते समय एक कस्टम यूआरएल खोलें, मैं अपनी वेबसाइट के उपयोगकर्ताओं को पुश नोटिफिकेशन भेजने के लिए Webpush ruby gem लागू कर रहा हूं।वेब पुश अधिसूचना

सर्वर कोड:

Webpush.payload_send({ 
     message: notification.message, 
     url: notification.url, # I can't figure out how to access this key 
     id: notification.id, # or this key from the service worker 
     endpoint: endpoint, 
     p256dh: p256dh_key, 
     vapid: vapid_keys, 
     ttl: 24 * 60 * 60, 
     auth: auth_key, 
    }) 

मैं एक सेवा कार्यकर्ता अधिसूचना दिखाने के लिए और यह क्लिक करने योग्य बना क्लाइंट पक्ष पर स्थापित किया है।

self.addEventListener("push", function (event) { 
    var title = (event.data && event.data.text()) || "New Message"; 

    event.waitUntil(
    self.registration.showNotification(title, { 
     body: "New push notification", 
     icon: "/images/[email protected]", 
     tag: "push-notification-tag", 
     data: { 
     url: event.data.url, // This is returning null 
     id: event.data.id // And this is returning null 
     } 
    }) 
) 
}); 

self.addEventListener('notificationclick', function(event) { 
    event.notification.close(); 
    event.waitUntil(
    clients.openWindow(event.data.url + "?notification_id=" + event.data.id) 
); 
}) 

यह सब ठीक कस्टम कुंजी (url, id) है कि मैं के माध्यम से गुजर रहा हूँ सेवा कार्यकर्ता के भीतर से सुलभ नहीं हैं को छोड़कर काम कर रहा है,।

क्या कोई भी वेबपश मणि के माध्यम से कस्टम डेटा को पारित करने के बारे में जानता है?

+0

इसमें कोई विशेषज्ञ नहीं है, लेकिन मैं ऐसा करूंगा: संदेश: JSON.stringify (अधिसूचना) सर्वर, और JSON.parse क्लाइंट पर ... –

+0

@ जोनास - मैं आपके समाधान के साथ जा रहा था। यदि आप इसे उत्तर के रूप में लिखते हैं, तो मैं इसे स्वीकार करूंगा। सहायता के लिए धन्यवाद। – BananaNeil

उत्तर

4

Webpush (with a payload) documentation से, ऐसा लगता है कि आपको JSON.stringify() विधि का उपयोग करके अपने सभी डेटा को संदेश में रखना चाहिए, और JSON.parse() के साथ सेवा कार्यकर्ता में इसे पुनर्प्राप्त करना चाहिए।

सर्वर:

Webpush.payload_send({ 
    message:JSON.stringify({ 
    message: notification.message, 
    url: notification.url, 
    id: notification.id, 
    }), 
    endpoint: endpoint, 
    p256dh: p256dh_key, 
    vapid: vapid_keys, 
    ttl: 24 * 60 * 60, 
    auth: auth_key, 
}) 

ग्राहक:

event.waitUntil(
    self.registration.showNotification(title, { 
    body: "New push notification", 
    icon: "/images/[email protected]", 
    tag: "push-notification-tag", 
    data: { 
    url: JSON.parse(event.message).url 
    } 
}) 
5

कस्टम डेटा में event.notification वस्तु आता नहीं घटना सीधे ( notificationclick में) में। तो यदि आप अधिसूचनाक्लिक फ़ंक्शन में कस्टम डेटा वैरिएबल लाने के लिए चाहते हैं, तो आपको इसे इस तरह करना चाहिए :)

self.addEventListener('notificationclick', function(event) { 
    event.notification.close(); 
    event.waitUntil(
    clients.openWindow(event.notification.data.url + "?notification_id=" + event.notification.data.id) 
); 
}) 
+1

समस्या पुश घटना से डेटा प्राप्त कर रही थी, क्लिक ईवेंट से नहीं। लेकिन आप सही हैं, यह एक और बग था जिसे मैंने ठीक करने की आवश्यकता समाप्त कर दी थी। – BananaNeil

संबंधित मुद्दे