Using custom key-value pairs sent within a Push Notification - [Sample: iOS, Android]

In this tutorial we'll cover how to use key-value pairs in a Push Notification and how those can be retrieved when receiving the Push Message, depending on the platform. 

Setting up the Push Message

  1. Go to the Leanplum Dashboard and create a new Push Messaging campaign.
  2. Check the Advanced Option group. It contains the group Data and the 'Mute inside the app' boolean. Let's focus on what can be added to the Data group.
  3. Variables can be added to the Data group as key-value pairs with a specific object type.
    In the sample, we are setting a String specifying its name and its value.

 

Android

Sample

Android sample app is available here.

Configuring the Android project

In order to get the data from the Push Notification, the Android project needs to be configured adding a custom Push Listener service, extending 'LeanplumPushListenerService', where the code for handling the incoming data can be placed.

For example, in the sample project, we are getting the "String_name" value from the Bundle object:

public class CustomPushListenerService extends LeanplumPushListenerService {

public void onMessageReceived(String var, Bundle notificationPayload) {
super.onMessageReceived(var, notificationPayload);

// This code is executed when the Notification is received.

// With the following the Advanced Data can be retrieved from the Push Notification
// Be sure the Variable name match - in this sample I'm assuming to set a String variable in the Advanced Data on Dashboard
String dataString = notificationPayload.getString("String_name");
// Printing to console the String value
Log.i("#### ", dataString);

}
}

The Custom Listener service needs now to be added to the AndroidManifest.xml in addition to the other services. 

For example:

<application
... >

<service
android:name="com.leanplum.android_customkeyvaluespair.CustomPushListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application> 

iOS

Sample

iOS sample app is available here.

Configuring the iOS project

Add in the AppDelegate class a 'didReceiveRemoteNotification' function. From there we can handle the notification payload and retrieve the variables values being passed.

For example:

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSString *messageTitle = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    NSString *stringData = [userInfo objectForKey:@"stringData"];
}

 In this sample, 'NSString *messageTitle = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];' will return the Push Notification message title, while 'NSString *String_name = [userInfo objectForKey:@"String_name"];will return the value of the string defined in the message as 'String_name'.

 


Was this article helpful?
Have more questions? Submit a request