Key-value pairs sent within a push

iOS and Android samples for using key-value pairs with push

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. Create a new push notification campaign.
  2. Check the Advanced options group. It contains the group Data. Click on Add field to Data.
  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.
1472

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:

func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {
        if let aps = userInfo["aps"] as? [String:Any] {
            if let messageTitle = aps["alert"] {
                print("Message Title: \(messageTitle)")
            }
        }
        if let stringData = userInfo["stringData"] as? String {
            print("String Data: \(stringData)")
        }
        completionHandler(.newData)
    }
- (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, messageTitle will return the Push Notification message title, while stringData will return the value of the string defined in the message as stringData.