簡體   English   中英

在iOS Firebase Cloud Messaging(FCM)中,將APNS發送到主題,但發送方不是來自iPhone

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM