Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

ios - How to send a silent Push Notification payload

I just want to know how I can determine what action to do on a silent push:

This is the aps that I sent to the client:

"aps": {
    "content-available": 1
}

My problem now is when I add type: "Order_Update" to determine that the silent push is for the Order Update to display an alert notification.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a few options for it! Let's take a small ride to understand all the different payloads and their usage.


Simple Payload

Displayed in Notification Center : Yes

Wakes app to perform background task : No

{
    "aps" : {
        "alert" : "You received simple notification!",
        "badge" : 1,
        "sound" : "default"
    }
}

Payload With Custom Notification Sound

Displayed in Notification Center : Yes

Wakes app to perform background task : No

Step 1 : Add custom notification sound file (.wav or .aiff extensions only. e.g. notification.wav) in your app bundle.

Step 2 : Configure your payload as shown below to play your custom sound

{
    "aps" : {
        "alert" : "It's a custom notification sound!",
        "badge" : 1,
        "sound" : "notification.wav"
    }
}

Notification With Custom Payload

Displayed in Notification Center : Yes

Wakes app to perform background task : No

{
    "aps" : {
        "alert" : "It's a notification with custom payload!",
        "badge" : 1,
        "content-available" : 0         
    },
    "data" :{
        "title" : "Game Request",
        "body" : "Bob wants to play poker",
        "action-loc-key" : "PLAY"
    },

}

Here the data dictionary holds custom information whatever you want. It will also display as normal notification with the alert message "It's a notification with custom payload!".


Normal Silent Notification

It will not a show an alert as a notification bar; it will only notify your app that there is some new data available, prompting the app to fetch new content.

Displayed in Notification center : No

Awake app to perform background task : Yes

{
    "content-available" : 1
}

Silent Notification With Custom Payload

Here comes the magic to show a notification alert as well awake your app in background for a task! (Note: only if it's running in background and has not been killed explicitly by the user.) Just add the extra parameter "content-available" : 1 in your payload.

Displayed in Notification Center : Yes

Wakes app to perform background task : Yes

{
    "aps" : {
        "alert" : "Notification with custom payload!",
        "badge" : 1,
        "content-available" : 1
    },
     "data" :{
        "title" : "Game Request",
        "body" : "Bob wants to play poker",
        "action-loc-key" : "PLAY"
     }
}

Use any of these payloads according to your app requirements. For background app refresh refer to Apple's documentation. I hope this gives you all the necessary information. Happy coding :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...