如何在firebase的实时数据库中发送新消息的推送通知
问题描述:
我使用firebase实时数据库制作了一个聊天应用程序,同时我也收到了这些消息。但我也想整合推送通知。当他们收到消息时,它应该向用户发送推送通知。有可能吗?如何在firebase的实时数据库中发送新消息的推送通知
答
如果其他用户在数据库推送令牌首先你需要访问与查询,然后使用AFNetworking您可以发送推送通知给其他用户,如下图所示:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer.setValue("key=yourServerKey", forHTTPHeaderField: "Authorization")
manager.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-Type")
let sendPushForChatUrl = "https://fcm.googleapis.com/fcm/send"
let to = otherUserPushToken as! String
let notification = notificationData as! [String: AnyObject]
let param = ["to":to, "content_available":true, "priority":"high", "notification":notification] as [String : Any]
manager.post(sendPushForChatUrl,
parameters: prepareObjects(param),
success: { (operation: AFHTTPRequestOperation?,responseObject: Any?) in
print("Suceess")
print(responseObject ?? "")
} ,
failure: { (operation: AFHTTPRequestOperation?,error: Error?) in
print("Error: " + error!.localizedDescription)
print("Fail")
})
,并要求例子是:
{
"to":"userPushToken",
"priority":"high",
"content_available": true,
"notification":{
"notification-type":"chat",
"target":"Current User",
"title":"Current User has sent you a message",
"text": "hello1",
"badge": 5 //Badge you want to show on app icon
}
}
如果您想调试它,它也可以在postman上运行。
编辑:下面
在你的情况是你的工作代码:
@IBAction func sendPushTapped(_ sender: Any) {
let manager = AFHTTPSessionManager()
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer = AFJSONResponseSerializer()
manager.requestSerializer.setValue("key=AIzaSyBpJPPYSkRKH6cpDV_bpe2NQiytvPTlTcw", forHTTPHeaderField: "Authorization")
manager.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-Type")
let fcmToken = "fkXo5yZvrKc:APA91bGxj2tJKWungvA4zyOUFBp5leUghd0wczDbZSkzImjZqRfSIgEFtKCQWZL0xEPZ3xfasfJdoX_JosX87VK7ioyap1fJJcTQ9rJzlC5ahwDktTnLGVqRg-wNZZw4TVvaCemEQFs2"
let sendPushForChatUrl = "https://fcm.googleapis.com/fcm/send"
let message = ["notification-type":"chat", "target":"Current User", "title": "Hello", "text": "msg!", "sound": "default", "badge": 5] as [String : Any]
let param = ["to":fcmToken, "priority":"high", "content_available": true, "notification":message] as [String : Any]
print("Push Url :", sendPushForChatUrl)
print("Push Parameters :", prepareObjects(param))
manager.post(sendPushForChatUrl, parameters: prepareObjects(param), progress: { (NSProgress) in
print("Progress ::", NSProgress)
}, success: { (task:URLSessionDataTask, responseObject) -> Void in
print("Push response", responseObject ?? "")
}, failure: { (task:URLSessionDataTask?, error:Error) -> Void in
print("Failure: ",error.localizedDescription)
})
}
func prepareObjects(_ dict : Dictionary<String, Any>) -> NSMutableDictionary
{
let dictParameters = NSMutableDictionary()
for (key, value) in dict
{
dictParameters.setValue(value as? AnyObject, forKey: key)
}
return dictParameters
}
+1
评论不适用于扩展讨论;这个对话已经[转移到聊天](http://chat.*.com/rooms/161589/discussion-on-answer-by-richie-rich-how-to-send-push-notifications-for-the-the-新-M)。 –
请按照下列:https://firebase.google.com/docs/cloud-messaging/ios/client& https://firebase.google.com/docs/cloud-messaging/ios/certs – Rivendell