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.
所有参考资料都提到必须有一个网络应用程序"来管理这个,但我还没有找到任何具体的例子.
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.
是否需要一个网络应用来管理 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-messaging/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' pod 添加到您的项目中.
在您的
.AppDelegate.swift
文件中导入 FirebaseMessaging- 使 MessagingDelegate 协议符合您的
AppDelegate
类. - 编写
didRefreshRegistrationToken
委托方法并获取用户的注册令牌:
- 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屋!