ios 苹果推送通知可以发送比警报和声音更多的参数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11236630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:00:24  来源:igfitidea点击:

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

iospush-notification

提问by Atma

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

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

For example user no, message id.

例如用户编号、消息 ID。

Can I send more parameters than what apple supports:

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

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

Is this possible?

这可能吗?

回答by Lily Ballard

Yes. In the Push Notification Programming Guide section The Notification Payloadit states

是的。在推送通知编程指南部分通知有效负载它说明

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. You should not include customer information as custom payload data. Instead, use it for such purposes as setting context (for the user interface) or internal metrics. For example, a custom payload value might be a conversation identifier for use by an instant-message client application or a timestamp identifying when the provider sent the notification. Any action associated with an alert message should not be destructive—for example, deleting data on the device.

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

So your payload might look like

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

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

Further down on that same page are a number of examples that demonstrate this.

在同一页面的进一步下方是一些演示这一点的示例。

回答by Mathew Varghese

Of course. You can send custom parameters as payload with apple push notification. Like Kevin Ballard said the payload will looks like the above. But remember one thing always you are dealing with push notification, As per the apple constraints the push notifications, The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this. So please take this too in your consideration when you are going to add more data to notification.

当然。您可以使用苹果推送通知将自定义参数作为有效负载发送。就像 Kevin Ballard 所说的那样,payload 看起来像上面那样。但是请记住,您总是在处理推送通知,根据苹果对推送通知的限制,通知有效负载允许的最大大小为 256 字节;Apple 推送通知服务拒绝任何超出此范围的通知。因此,当您要向通知添加更多数据时,也请考虑这一点。

回答by Ved Rauniyar

You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:

不允许在 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.

So in your case you should do something like:

因此,在您的情况下,您应该执行以下操作:

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

Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

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

回答by Lance Samaria

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 tutorialfor it

以下是我读到的一切从AppDelegate的通知。下面的方法是 AppleDelegate 中的一个委托方法。使用此教程

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