How to send a silent push

Push notifications can be silent, meaning that they contain no alert or sound and are mainly used to trigger actions in the app background or update the app with a payload.

Setting up silent push notifications

iOS

To utilise silent push notifications in iOS, you need to configure your application to do so even when it is in the background.

In order to configure this, in XCode, add the Background Modes capability under the Signing & Capabilities pane to the main app target. Select the Remote Notifications checkbox.

Silent pushes on iOS are delivered to your app via the background fetch delegate. Add the method to your AppDelegate and forward XPush notifications to the SDK:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    if (userInfo[@"xpush"] != nil) {
        [XPush applicationDidReceiveRemoteNotification:userInfo
                               fetchCompletionHandler:completionHandler];
        return;
    }
    completionHandler(UIBackgroundFetchResultNoData);
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    if userInfo["xpush"] != nil {
        XPush.applicationDidReceiveRemoteNotification(userInfo,
                                                     fetchCompletionHandler: completionHandler)
        return
    }
    completionHandler(.noData)
}

Android

There is not any requirements to use silent push on Android apps.

Sending Silent Push Notifications

It's possible to send a silent push by creating an API-triggered campaign and executing it using the campaign execute external endpoint. Set the push_silent flag to 1 in the message payload.

🚧

Attaching the title and the body with push_silent set to 1 is not recommended as it can cause undesired behaviour. For further details on iOS, please refer to the official Apple documentation on background updates.

A Sample silent push notification payload can be seen below:

curl --request POST \
  --url 'https://external-api.xtremepush.com/api/external/execute/campaign' \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --data-raw '{
    "id":"<campaign_id>",
    "apptoken":"<app_token>",
    "title":"Silent Push Notification",
    "android_push":0,
    "android_push_production":0,
    "ios_push":1,
    "ios_push_production":1,
    "ios_push_sandbox":1,
    "conditions":{
      "1":{
        "1":[
          "id",
          "=",
          "<device_id>"
        ],
        "operator":"AND"
      },
      "operator":"AND"
    },
    "broadcast":"0",
    "messages":{
      "1":{
        "push_silent":1,
        "payload_add":[
          {
              "key":"",
              "value":""
          },
          {
              "key":"",
              "value":""
          }
        ]
      }
    }
  }'
curl --request POST \
     --url https://external-api.xtremepush.com/api/external/execute/campaign \
     --header 'accept: application/json' \
     --header 'content-type: application/json'{
  "id":"",
  "apptoken":"",
  "title":"Silent Push Notification”,
  "android_push":1,
  "android_push_production":1,
  "ios_push":0,
  "ios_push_production":0,
  "ios_push_sandbox":0,
  "conditions":{
     "1":{
        "1":[
           "id",
           "=",
           "<device_id>"
        ],
        "operator":"AND"
     },
     "operator":"AND"
  },
  "broadcast":"0",
  "push_text":"sample text",
  "messages":{
     "1":{
        "push_silent":1,
        "payload_add":[
           {
              "key":"",
              "value":""
           },
           {
              "key":"",
              "value":""
           },
           {
              "key":"",
              "value":""
           }
        ]
     }
  }
}'