简体   繁体   中英

Notification via iron-ajax (Polymer)

Can we send notifications in PolymerJS using iron-ajax element?

Here's a sample of CURL request(works just fine):

curl --header "Authorization: key=my_key" --header "Content-Type: application/json" -d '{"to": "my_token", "notification": {"title": "Hello Bro", "body": "Message Yo man"}}' https://fcm.googleapis.com/fcm/send

I'm using my own values for "my_key" and "my_token" instances. In chrome it gives me something like this: Screenshot sample

Here's a code-sample for my iron-ajax element:

 <iron-ajax id="xhr" auto url="https://fcm.googleapis.com/fcm/send" headers='{"Authorization": "[[my_key]]"}' handle-as="json" content-type="application/json" body='{"to":"[[my_token]]","notification": {"title": "Hello Bro", "body": "Message Yo man"}}' method="POST"> </iron-ajax> 

Basing on interaction with some elements on the page I'm trying to trigger the notification via JS:

this.$.xhr.generateRequest();

I'm getting this response:

{"multicast_id":5623718911822310219,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1489155273403695%e609af1cf9fd7ecd"}]}

So it appears to be successful, though this time I don't receive any notifications. Am i missing something here, or might there be a better way of achieving it in Polymer? Thank you in advance for any insights on this matter!

IT WORKS as is;) You can't send notification to yourself via your own UI. Also if you're sending notification to specific app/website(atleast via desktop) - that app/website shouldn't be a current active tab in the browser(or browser shouldn't be your active app as for the receiver), this of course makes sense - but it took me some time to figure it out. Though again there is much easier way of doing this via vanilla js - you can even trigger this via your browser console (with correct parameters in place). I hope this helps someone, good luck !

  function triggerNotification(key,token,title,message){ var notification = { 'title': title, 'body': message }; fetch('https://fcm.googleapis.com/fcm/send', { 'method': 'POST', 'headers': {'Authorization': 'key=' + key, 'Content-Type': 'application/json'}, 'body': JSON.stringify({'notification': notification, 'to': token}) }).then(function() { console.log("Response: ",arguments); }).catch(function(error) { console.error(error); }) } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM