繁体   English   中英

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

[英]Notification via iron-ajax (Polymer)

我们可以使用iron-ajax元素在PolymerJS中发送通知吗?

这是CURL请求的示例(工作得很好):

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

我正在为“my_key”和“my_token”实例使用我自己的值。 在chrome中它给我这样的东西: 截图示例

这是我的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> 

基于与页面上某些元素的交互,我试图通过JS触发通知:

this.$.xhr.generateRequest();

我收到了这个回复:

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

所以它似乎是成功的,虽然这次我没有收到任何通知。 我在这里遗漏了什么,或者可能有更好的方法在聚合物中实现它? 提前感谢您对此事的任何见解!

它的工作原理;)您无法通过自己的UI向自己发送通知。 此外,如果您要向特定的应用程序/网站发送通知(至少通过桌面) - 该应用程序/网站不应该是浏览器中的当前活动选项卡(或者浏览器不应该是您的活动应用程序,如接收者),当然是有道理的 - 但我花了一些时间才弄明白。 虽然通过vanilla js更容易实现这一点 - 您甚至可以通过浏览器控制台(具有正确的参数)触发此操作。 我希望这有助于某人,祝你好运!

  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