简体   繁体   English

Firebase、Swift:向给定设备令牌的特定用户发送推送通知

[英]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.所有参考资料都提到必须有一个“网络应用程序”来管理这个,但我还没有找到任何具体的例子。

  1. Is it necessary to have a web app to manage push notifications for Firebase?是否有必要使用 Web 应用程序来管理 Firebase 的推送通知?

  2. If so, are there any examples of how this can work?如果是这样,是否有任何示例说明这是如何工作的?

Thank you.谢谢。

  • First do the all configuration for Firebase Cloud Messaging .首先对Firebase Cloud Messaging进行所有配置 Can follow this link https://firebase.google.com/docs/cloud-messaging/ios/client可以按照此链接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.现在,您需要为要向其发送推送通知的关联用户访问Firebase 注册令牌 You can get this token by following below steps:您可以通过以下步骤获取此令牌:
    1. Add 'Firebase/Messaging' pod to your project.“Firebase/Messaging” pod 添加到您的项目中。
    2. import FirebaseMessaging in your AppDelegate.swift file.AppDelegate.swift文件中import FirebaseMessaging
    3. Conform MessagingDelegate protocol to your AppDelegate class.使MessagingDelegate协议符合您的AppDelegate类。
    4. Write 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.

相关问题 iOS Firebase 推送通知:如何提供 Firebase 用户的设备令牌并发送通知 - iOS Firebase Push Notifications : How To Give Firebase User's Device Token And Send Notification 快速从用户到用户Firebase发送推送通知 - Swift Send Push Notification from user to user Firebase 如何在 iOS 中使用 Google Firebase 向特定用户发送推送通知? - How to send push notification to a specific user using Google Firebase in iOS? 使用Firebase iOS Swift将特定设备的通知推送到特定设备 - Push Notification From Specific Device To Specific Device Using Firebase iOS Swift (快速3)我需要向用户发送通知,以在特定时间打开应用。 更好地使用本地或推送通知? - (Swift 3) I need to send a notification to the user to open the app at a specific time. Better to use a local or push notification? 设备令牌无效时将推送通知发送到ios设备崩溃 - Send push notification to ios device crash when device token is invalid 推送通知设备令牌 - Push Notification Device Token 推送通知中的设备令牌 - Device token in push notification 推送通知设备令牌? - Push Notification Device Token? 直接从设备swift将推送通知发送到设备3 - send push notification to device directly from a device swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM