[英]Firebase, Swift: Send a push notification to specific user given device token
I have a Firebase Swift chat app where I'd like to send a push notification to a specific user.我有一个 Firebase Swift 聊天应用程序,我想在其中向特定用户发送推送通知。 I have already captured and have access to the user's device token.我已经捕获并可以访问用户的设备令牌。
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.所有参考资料都提到必须有一个“网络应用程序”来管理这个,但我还没有找到任何具体的例子。
Is it necessary to have a web app to manage push notifications for Firebase?是否有必要使用 Web 应用程序来管理 Firebase 的推送通知?
If so, are there any examples of how this can work?如果是这样,是否有任何示例说明这是如何工作的?
Thank you.谢谢。
import FirebaseMessaging
in your AppDelegate.swift
file.在AppDelegate.swift
文件中import FirebaseMessaging
。AppDelegate
class.使MessagingDelegate协议符合您的AppDelegate
类。didRefreshRegistrationToken
delegate method and get your user's registration token:编写didRefreshRegistrationToken
委托方法并获取用户的注册令牌: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)
No, it is not necessary to have a web app to manage push notifications with Firebase不,没有必要使用网络应用程序来管理 Firebase 的推送通知
Look at the answer from Ashvini, it gives you a method to send messages directly from your iOS app, to other devices.看看 Ashvini 的回答,它为您提供了一种直接从 iOS 应用程序向其他设备发送消息的方法。
You can also use the Firebase Console to manually send push messages to multiple users at the same time.您还可以使用 Firebase 控制台同时手动向多个用户发送推送消息。
Here is the documentation for sending messages through a web app/server.这是通过 Web 应用程序/服务器发送消息的文档。 Firebase Documentation Firebase 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.