简体   繁体   English

通过iron-ajax(聚合物)通知

[英]Notification via iron-ajax (Polymer)

Can we send notifications in PolymerJS using iron-ajax element? 我们可以使用iron-ajax元素在PolymerJS中发送通知吗?

Here's a sample of CURL request(works just fine): 这是CURL请求的示例(工作得很好):

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 curl --header“Authorization:key = my_key”--header“Content-Type:application / json”-d'{“to”:“my_token”,“notification”:{“title”:“Hello Bro”,“ body“:”消息Yo man“}}' https://fcm.googleapis.com/fcm/send

I'm using my own values for "my_key" and "my_token" instances. 我正在为“my_key”和“my_token”实例使用我自己的值。 In chrome it gives me something like this: Screenshot sample 在chrome中它给我这样的东西: 截图示例

Here's a code-sample for my iron-ajax element: 这是我的iron-ajax元素的代码示例:

 <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: 基于与页面上某些元素的交互,我试图通过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. 它的工作原理;)您无法通过自己的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). 虽然通过vanilla js更容易实现这一点 - 您甚至可以通过浏览器控制台(具有正确的参数)触发此操作。 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); }) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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