Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 947
Sydney Loteria Avatar asked Mar 31 '16 04:03

Sydney Loteria


People also ask

How do I send silent push notifications?

Control Panel. To send a Silent Push Notification to your mobile app, check the checkbox Silent Push in the iOS and Android push settings of the Send Push form.

How do I send a silent push notification in Swift?

In order to send a silent push notification, you must first obtain the Firebase cloud messaging server key and Firebase FCM token. After that, you can make use of any API client tools out there to make a POST request to Google API, which will then send the notification payload to your testing device.

How do I send silent push notifications on Android?

There are two ways users can receive silent push notifications on Android. Users can long press on a notification to get an option to display notifications silently. Users can also enable silent notifications by heading to Settings > App & Notifications > Search for app and choose> Notifications, and turn it off.

What is a silent push notification?

Silent notifications allow you to notify your app in the background when important events occur.


2 Answers

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

like image 171
Dipen Panchasara Avatar answered Sep 26 '22 07:09

Dipen Panchasara


As i understand, you want extra data inside payload, so you can identify what push notification type is,or what action need to be handled.

For that edit your payload as:

 $body = array(
    'content-available' => 1,
    'sound' => ''
    );  

$payload = array();
$payload['aps'] = $body;
$payload['action'] = 'order_update';

Then in your iOS Code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{


    NSString *action = userInfo["action"];


    if([userInfo[@"aps"][@"content-available"] intValue]== 1 && [action isEqualToString:@"order_update") //order update notification
    {
        //handle Your Action here
        return;
    }


}

Hope this solves your problem!

like image 25
gunjot singh Avatar answered Sep 26 '22 07:09

gunjot singh