Is the following structure with body and loc-args

2019-07-28 19:17发布

问题:

Would the following payload notification cause any problems if you have body and loc-args and loc-key in the same payload?

{"aps":{"alert":{"body" : "Bob wants to play poker", "loc-key":"general","loc-args":["Test DEFAULT."]},"badge":1,"sound":"default"}}

The reason I have it set up like this. Is that I am not sure if the notification to just have a body in would be handle during the app, so it would use the localized loc-args in with the following key-valus in the localizable string:

"general"="%@";

回答1:

There are two options to localize an alert message embedded in a push notification. One is to localize on the server and send the localized message as a string value of the alert key. Eg:

{"alert": "A localized sentence."}

Optionally, if you want to also set the launch image and/or have the view button of the alert read something other than 'View', you would replace the string value of alert with a dictionary. In this case the string value should be the value of body in the dictionary. Eg:

{'alert': {'body: "A localized sentence.", action-loc-key: "...", launch-image: "..."' }}

The docs warn that if you only have the body key in the dictionary, don't use a dictionary. Instead set the value of body as the string value of alert.

The other option is to use the loc-key, loc-args pair and have the localization take place on the device.

Although I have not verified which one iOS defaults to if both body and loc-key are present in the alert dictionary, I guess it will pick one and ignore the other. Someone with access to a working push notification dev setup could verify which one.

Ultimately, one can infer from the docs that body and loc-key should be used in an exclusive or relationship. Either one or the other, but not both.



回答2:

I've made several tests.

For example sending

{
    "aps": {
        "alert": {
            "body": "hello world!",
            "loc-args": ["andy", "my text"],
            "loc-key": "comment"
        }
    },
    "sound": "default",
    "extras": {}
}

On my device I have a string in the loc file that looks like:

"comment" = "%@ has comment %@";

So the notification that I'm receiving is:

Andy has comment my text

Now I've sent a new notification like this:

    {
        "aps": {
            "alert": {
                "body": "hello world!",
                "loc-args": ["andy", "my text"],
                "loc-key": "does_not_exist"
            }
        },
        "sound": "default",
        "extras": {}
    }

with a loc-key that does not exist, and the notification that I'm receiving is

does_not_exist

so as kadam said in his answer:

Ultimately, one can infer from the docs that body and loc-key should be used in an exclusive or relationship. Either one or the other, but not both.

Blockquote

According to this post, if the key is missing the key is used. That's why 'body' is always ignored and is EITHER ONE or the OTHER.



回答3:

I'm not 50 yet so I can't comment but to follow-up on @kadam answer, just for future reference to those who will visit this question.

Although I have not verified which one iOS defaults to if both body and loc-key are present in the alert dictionary, I guess it will pick one and ignore the other. Someone with access to a working push notification dev setup could verify which one.

I have tried it and it defaults to the value of loc-key if it has a value on the app itself. If none then it uses the value of body.