Firebase,Swift:向给定设备令牌的特定用户发送推送通知 [英] Firebase, Swift: Send a push notification to specific user given device token
问题描述
我有一个Firebase Swift聊天应用程序,我想在其中向特定用户发送推送通知.我已经捕获并可以访问用户的设备令牌.
I have a Firebase Swift chat app where I'd like to send a push notification to a specific user. I have already captured and have access to the user's device token.
所有参考文献都提到必须有一个"Web应用程序"来管理它,但是我还没有找到任何具体的例子.
All the references mention having to have a 'web app' to manage this, but I haven't managed to find any specific examples of this.
-
是否需要一个Web应用程序来管理Firebase的推送通知?
Is it necessary to have a web app to manage push notifications for Firebase?
如果可以,是否有任何示例说明如何工作?
If so, are there any examples of how this can work?
谢谢.
推荐答案
- 首先对 Firebase Cloud Messaging 进行所有配置.可以点击 https://firebase.google.com/docs/cloud-消息传递/ios/客户端
- 现在,您需要为要发送推送通知的关联用户访问 Firebase注册令牌.您可以按照以下步骤获取此令牌:
- First do the all configuration for Firebase Cloud Messaging. Can follow this link https://firebase.google.com/docs/cloud-messaging/ios/client
- Now you need to access Firebase registration token for your associated user whom you want to send a push notification. You can get this token by following below steps:
- 将'Firebase/Messaging'窗格添加到您的项目中. 在您的
-
导入FirebaseMessaging
. - 将 MessagingDelegate 协议符合您的
AppDelegate
类. - 编写
didRefreshRegistrationToken
委托方法并获取用户的注册令牌:
AppDelegate.swift
文件中- Add 'Firebase/Messaging' pod to your project.
import FirebaseMessaging
in yourAppDelegate.swift
file.- Conform MessagingDelegate protocol to your
AppDelegate
class. - Write
didRefreshRegistrationToken
delegate method and get your user's registration token:
-
现在,您可以准备向特定用户发送推送通知了.发送通知如下:
Now you are ready to send push notification to your specific user. Send notification as below:
func sendPushNotification(payloadDict: [String: Any]) { let url = URL(string: "https://fcm.googleapis.com/fcm/send")! var request = URLRequest(url: url) request.setValue("application/json", forHTTPHeaderField: "Content-Type") // get your **server key** from your Firebase project console under **Cloud Messaging** tab request.setValue("key=enter your server key", forHTTPHeaderField: "Authorization") request.httpMethod = "POST" request.httpBody = try? JSONSerialization.data(withJSONObject: payloadDict, options: []) let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { print(error ?? "") return } if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { print("statusCode should be 200, but is \(httpStatus.statusCode)") print(response ?? "") } print("Notfication sent successfully.") let responseString = String(data: data, encoding: .utf8) print(responseString ?? "") } task.resume() }
-
现在将上面的函数调用为:
Now call above function as:
let userToken = "your specific user's firebase registration token" let notifPayload: [String: Any] = ["to": userToken,"notification": ["title":"You got a new meassage.","body":"This message is sent for you","badge":1,"sound":"default"]] self.sendPushNotification(payloadDict: notifPayload)
这篇关于Firebase,Swift:向给定设备令牌的特定用户发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!