【问题标题】:Can apple push notifications send more parameters than alert and sound?苹果推送通知可以发送比警报和声音更多的参数吗?
【发布时间】:2012-06-29 12:00:21
【问题描述】:

我有几条需要与推送通知相关联的元数据。

例如用户号,消息ID。

我可以发送比苹果支持的更多的参数吗:

 {aps =     {
    alert = joetheman;
    sound = default;
};}

这可能吗?

【问题讨论】:

    标签: ios push-notification


    【解决方案1】:

    是的。在推送通知编程指南部分The Notification Payload 中声明

    提供者可以在 Apple 保留的 aps 命名空间之外指定自定义负载值。自定义值必须使用 JSON 结构化和原始类型:字典(对象)、数组、字符串、数字和布尔值。您不应将客户信息作为自定义有效负载数据包含在内。相反,将其用于设置上下文(用于用户界面)或内部指标等目的。例如,自定义有效负载值可能是供即时消息客户端应用程序使用的会话标识符,或者是标识提供者何时发送通知的时间戳。与警报消息相关的任何操作都不应具有破坏性——例如,删除设备上的数据。

    所以你的有效载荷可能看起来像

    {
        "aps": {
            "alert": "joetheman",
            "sound": "default"
        },
        "message": "Some custom message for your app",
        "id": 1234
    }
    

    在同一页面的下方有许多示例可以证明这一点。

    【讨论】:

    • 我可以在字典中加入“消息”和“id”然后发送它。在那种情况下,我遇到了一些奇怪的问题。
    • 请注意,该链接指的是 Mac 文档 - iOS 文档(尽管不一定不同)在这里:developer.apple.com/library/ios/documentation/…
    【解决方案2】:

    当然。您可以使用苹果推送通知将自定义参数作为有效负载发送。就像凯文巴拉德所说的那样,有效载荷将如上所示。但是请记住,您始终在处理推送通知的一件事,根据苹果对推送通知的限制,通知有效负载允许的最大大小为 256 字节; Apple Push Notification Service 拒绝任何超出此范围的通知。因此,当您要向通知添加更多数据时,请考虑这一点。

    【讨论】:

    【解决方案3】:

    您不能将自定义标签放在 aps 标签内。以下是相关文档的说明:

    Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
    

    所以在你的情况下,你应该这样做:

    {
        "aps": {
            "alert": "Hello Push",
            "sound": "default"
        },
        "People": {
            "Address": "Your address here",
            "Name": "Your Name here",
            "Number": "XXXXXXXXXX"
        }
    }
    

    因此,您可以通过在主 JSON 中而不是在“aps”中查找自定义有效负载来读取它:

    【讨论】:

    • :- { "aps": { "alert": "Hello Push", "sound": "default" }, "data": { "payload" : { "dataa": { " btn_action": "Some text", "lock_mac" : "0Some text", "device_type" : "iOS", "some key" : "Some text" } } } } 这是 iOS 的有效载荷吗
    【解决方案4】:

    以下是我根据 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 ...
    }
    

    这是我从 AppDelegate 中的通知中读取所有内容的方法。下面的方法是 AppleDelegate 中的一个委托方法。为它使用this tutorial

    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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多