[英]How can I detect when the real iOS/APNS push token is registered with Firebase Cloud Messaging (FCM)?
[英]In iOS Firebase Cloud Messaging (FCM), send APNS to topic but other than sender from iphone
订阅主题以将APNS发送到所有设备:
//subscribe to topic to send message to multiple device
Messaging.messaging().subscribe(toTopic: "alldevices")
注意:“所有设备”是所有已订阅设备将获得APNS的主题名称
通过以下主题以编程方式将APNS发送到所有设备:
func sendPushMessage(todoItem:TodoItem, isAdded:Bool = true) {
let url = URL(string: "https://fcm.googleapis.com/fcm/send")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let strKey:String = "Here FCM Server Key"
request.setValue("key=\(strKey)", forHTTPHeaderField: "Authorization")
var message:String = ""
if isAdded {
message = "Todo with title '\(todoItem.title)' added"
}
else{
message = "Todo with title '\(todoItem.title)' removed"
}
let dictData = ["to":"/topics/alldevices","priority":"high","notification":["body":message,"title":"Community","badge":"1"]] as [String : Any]
do {
let jsonData = try JSONSerialization.data(withJSONObject: dictData, options: .prettyPrinted)
// here "jsonData" is the dictionary encoded in JSON data
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
let strData :String = String(data: responseData!, encoding: String.Encoding.utf8)!
print("data : \(strData)")
NSLog("\(String(describing: response) )")
})
task.resume()
} catch {
print(error.localizedDescription)
}
}
这里的问题是发件人(使用应用程序的用户)也获得了APNS,因为发件人也订阅了主题“ alldevices”。 但是从技术上讲,发件人不需要APNS,因为发件人自己为所有其他设备生成了APNS
注意:我们没有单独的服务器来发送或管理APNS。 FCM唯一在这里为我们管理APNS。
我认为您无法通过API进行此操作。 也许通过Firebase控制台的“使用用户”的通知部分,但这显然无法解决您的问题。 如果将来/如果Firebase API开始支持用户观众,也许将来可以实现。
您可能要看的一件事是发送“数据”消息,而不是“显示”消息。
这将向所有其他用户发送“静默”通知,并且一旦接收到,客户端电话将决定是否需要显示该通知。
例如,您的Firebase帖子将更改为使用“数据”对象,而不是“通知”对象
{
"to": "/topics/alldevices",
"content_available": true,
"data": {
"sender": "{{this_phones_uuid}}"
},
"priority": "high"
}
“发件人”是您创建并存储在用户手机上的唯一标识符。
收到通知时,仅当“发送者”值不等于手机上存储的UUID时,才显示通知。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let sender = userInfo["sender"] as? String {
if sender != {{your_stored_uuid}} {
let notification = UNMutableNotificationContent()
notification.title = "title"
notification.body = "body"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
let request = UNNotificationRequest.init(identifier: "todo", content: notification, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
}
您可能需要在使用此方法之前先研究一下可靠性,因为操作系统可以限制静默通知,有时甚至不发送静默通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.