Foreground Notifications Handling

It is important to define the right behavior for the situation when a push notification is received in foreground. You don't want to interrupt the user workflow, but still be delivering important information to the user.

Normally you have 3 options on how to handle a push notification received in the foreground:

  • Show OS style notification
  • Show custom dialog
  • Ignore the notification

The default behavior on both iOS and Android is to show OS style notification. You can find information on how to change it below.

 

Objective-C:

The default behavior is to show notifications in the foreground with alert, sound and badge. If you don't want to show notifications in the foreground, you could do the following:

[XPush registerForegroundNotificationOptions:^XPNotificationType(XPMessage *message) {
    return nil;
}];

 

If you have different type of messages in your app you might want to implement different behavior for each. It could be done by having logic inside the function:

[XPush registerForegroundNotificationOptions:^XPNotificationType(XPMessage *message) {
    If (message.data[@"type"] == @"article") {
        return XPNotificationType_Alert | XPNotificationType_Sound | XPNotificationType_Badge;
    }
    return nil;
}]; 

 

The method described above (registerForegroundNotificationOptions) is designed to define the behavior for the default iOS notifications. In order to implement the custom behavior (like custom dialogs) you should use another method called message response handler.

[XPush registerMessageResponseHandler:^(XPMessageResponse *response) {
    if (response.action.type == XPActionType_Present) {
        // Logic when message received in foreground
    }
}]; 

 

After showing custom dialog you can programatically proceed with the message click action (e.g. open URL) when user clicks a button:

[XPush clickMessage:response.message actionIdentifier:nil]];

You can also track the message as clicked without running any actions (e.g. open URL):

[XPush reportMessageClick:response.message actionIdentifier:nil]];

 

A complete example:

// Don't show OS notifications in foreground
[XPush registerForegroundNotificationOptions: ^XPNotificationType(XPMessageResponse *response) {
        return nil;
}];

// Register custom message handler
[XPush registerMessageResponseHandler: ^(XPMessageResponse * _Nonnull response) {        
        if (response.action.type == XPActionType_Present) {
            // Show an article dialog with "View" button
            [self showArticleDialog: response.message viewButtonCallback: ^{
                // Process message click
                [XPush clickMessage:response.message actionIdentifier:nil];
            }];
        }
    }];

 

Swift 3:

To not show any notifications when the app is in the foreground, you could do the following.


XPush.registerForegroundNotificationOptions({(_ x: XPMessageResponse) -> XPNotificationType in
        return []
})

To show messages based on some specific criteria, for example, if it has some payload value paired with a key called showMessage you could implement the following:


XPush.registerForegroundNotificationOptions({(_ x: XPMessageResponse) -> XPNotificationType in
if(x.message.data?["showMessage"] != nil){
return [.alert, .sound, .badge]
}
return []
})

You can also report the messages as clicked/closed by calling XPush.reportMessageClicked(XPMessageResponse) orXPush.reportMessageClosed(XPMessageResponse) . The following example demonstrates how to record all messages that you have chosen not to show as clicked:


XPush.registerForegroundNotificationOptions({(_ x:XPMessageResponse) -> 
       XPNotificationType in

        if (x.message.data ?["showMessage"] !=nil){
            return [.alert, .sound, .badge]
        }
        //if we are not showing the message report it as clicked
        XPush.reportMessageClicked(x)

        return []
       })
})

To programatically proceed with the message click action (e.g. open URL), you can call the function:

XPush.click(x)

The following is an example to demonstrate this:


XPush.registerForegroundNotificationOptions({(_ x:XPMessageResponse) -> 
       XPNotificationType in

        if (x.message.data ?["showMessage"] !=nil){
            return [.alert, .sound, .badge]
        }
        //proceed with message click
        XPush.clickMessage(x)

        return []
       })
})

 

Android 

In android, when initialising PushConnector, you can set the behaviour for foreground notifications by setting .setShowForegroundNotifications() to either true or false (default is true). If set to false, notifications won't show while your app is in the foreground, unless you specifically call mPushConnector.showNotification().

new PushConnector.Builder(XPUSH_APP_KEY, GOOGLE_PROJECT_NUMBER)
.setShowForegroundNotifications(false)
.create(this);

Now when a message is received we can choose how we want to handle it. To do this we will be using the messageResponseReceived callback (see here for more details).

@Override
public void messageResponseReceived(Message messagePayload,
HashMap<String, String> responsePayload,
WeakReference<Context> uiReference) {

if (responsePayload.get("responseType").equals("present")) {

}
}

Some of the useful functions you can use are 

Show notification. Show the notification.

mPushConnector.showNotification(messagePayload);

Click message. Parameters are message and the action of that message to click. If set to null, the default action of the notification will be used. So if the message has a click action of Open URL, the URL will open in the phones browser.

mPushConnector.clickMessage(messagePayload, null);

reportMessageClicked. Parameters are the message to report as clicked, and the action to report as clicked (should be null if not using interactive notifications)

mPushConnector.reportMessageClicked(messagePayload, null);

reportMessageDismissed. Parameters are the message to report as closed, and the action to report as closed (should be null if not using interactive notifications)

mPushConnector.reportMessageDismissed(messagePayload, null);

 

 

Have more questions? Submit a request

0 Comments

Article is closed for comments.