Can apple push notifications send more parameters than alert and sound?

后端 未结 4 1225
无人共我
无人共我 2020-12-13 05:20

I have several pieces of metadata that I need to associate with a push notification.

For example user no, message id.

Can I send more parameters than what ap

4条回答
  •  春和景丽
    2020-12-13 05:53

    Here's how I send my custom key/values based on this tutorial

    func sendPushNotification(to token: String, title: String, body: String, messageId: String, fromUsername: String, fromUID: String, timeStamp: Double) {
    
        let urlString = "https://fcm.googleapis.com/fcm/send"
        let url = NSURL(string: urlString)!
    
        // apple's k/v
        var apsDict = [String: Any]()
        apsDict.updateValue(title, forKey: "title")
        apsDict.updateValue(body, forKey: "body")
        apsDict.updateValue(1, forKey: "badge")
        apsDict.updateValue("default", forKey: "sound")
    
        // my custom k/v
        var myCustomDataDict = [String: Any]()
        myCustomDataDict.updateValue(messageId, forKey: "messageId")
        myCustomDataDict.updateValue(fromUsername, forKey: "username")
        myCustomDataDict.updateValue(fromUID, forKey: "uid")
        myCustomDataDict.updateValue(timeStamp, forKey: "timeStamp")
    
        // everything above to get posted
        var paramDict = [String: Any]()
        paramDict.updateValue(token, forKey: "to")
        paramDict.updateValue(apsDict, forKey: "notification")
        paramDict.updateValue(myCustomDataDict, forKey: "data")
    
        let request = NSMutableURLRequest(url: url as URL)
        request.httpMethod = "POST"
                                                         // ** add the paramDict here **
        request.httpBody = try? JSONSerialization.data(withJSONObject: paramDict, options: [.prettyPrinted])
    
        // send post ...
    }
    

    Here's how I read everything from the notification in AppDelegate. The method below is a delegate method in AppleDelegate. Use this tutorial for it

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void {
    
        let userInfo = response.notification.request.content.userInfo
    
        if let userInfo = userInfo as? [String: Any] {
    
            readPushNotification(userInfo: userInfo)
        }
    
        completionHandler()
    }
    
    func readPushNotification(userInfo: [String: Any]) {
    
        for (k,v) in userInfo {
            print(k)
            print(v)
        }
    
        // my custom k/v
        if let messageId = userInfo["messageId"] as? String {
            print(messageId)
        }
    
        if let fromUsername = userInfo["username"] as? String {
            print(fromUsername)
        }
    
        if let fromUID = userInfo["uid"] as? String {
            print(fromUID)
        }
    
        if let timeStamp = userInfo["timeStamp"] as? String {
            print(timeStamp)
        }
    
        // apple's k/v
        if let aps = userInfo["aps"] as? NSDictionary {
    
            if let alert = aps["alert"] as? [String: Any] {
    
                if let body = alert["body"] as? String {
                    print(body)
                }
    
                if let title = alert["title"] as? String {
                    print(title)
                }
            }
    
            if let badge = aps["badge"] as? Int {
                print(badge)
            }
        }
    }
    

提交回复
热议问题