如何从Rest API发送推送通知 [英] How to send push notification from Rest API

查看:57
本文介绍了如何从Rest API发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Firebase 中创建了我的应用程序,并配置了云消息传递功能.当我从 Firebase 控制台发送通知时,设备会收到通知,但是如果我尝试通过Rest API(使用PostMan)进行发送.然后通知未到达设备,但响应显示为成功.

这是我的PostMan请求

URI- https://fcm.googleapis.com/fcm/send

标题:Content-Type:应用程序/json授权:key = MY_SERVER_KEY

正文:{"data":{"title":"Firebase","detail":我是firebase"},"to":我的FCM令牌在这里"}

响应:{"multicast_id":7834540847388366233,成功":1,失败":0,"canonical_ids":0,结果": [{"message_id":"0:1532422122326299%ebf5f25ef9fd7ecd"}]}

我已经搜索过,看来文档中提到的方式与我遵循的方式相同谁能告诉我为什么它不起作用?谢谢

解决方案

我在这里发布是为了指导他人.按照@ rv7284的指导,我们需要使用 notification 键而不是其他任何键发送数据.

因此,在请求正文时应如下所示,其他请求参数(如标头和所有参数)将与问题中提到的相同.

  {通知":{"title":"Firebase",细节":我是火力基地"},至":您的FCM令牌"} 

I have created my App in Firebase and configured for cloud messaging. When I am sending the notification from Firebase Console, device gets the notification but if I try to send via Rest API (Using PostMan). Then notification not coming to device but the response is showing as Success.

Here is my PostMan request

URI - https://fcm.googleapis.com/fcm/send

Header: Content-Type:application/json Authorization:key=MY_SERVER_KEY

Body: { "data": { "title": "Firebase", "detail": "I am firebase" }, "to" : "MY FCM TOKEN HERE" }

Response : { "multicast_id": 7834540847388366233, "success": 1, "failure": 0, "canonical_ids": 0, "results": [ { "message_id": "0:1532422122326299%ebf5f25ef9fd7ecd" } ] }

I have searched and it seems the way mentioned in documentation is same as I am following Can anyone let me know why it is not working? Thanks

解决方案

I am posting here so as to guide others. As per the guidance of @rv7284, We need to send the data in notification key rather any other key.

So While requesting the body should look like this, Other request parameter like header and all will be same as mentioned in the question.

{ "notification": {
 "title": "Firebase",
 "detail": "I am firebase"
},
 "to" : "YOUR FCM TOKEN"
}

这篇关于如何从Rest API发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆