Messaging campaigns

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

The Amazon Pinpoint console enables you to target your app users with push messaging. You can send individual messages or configure campaigns that target a group of users that match a profile that you define. For instance, you could email users that have not used the app in 30 days, or send an SMS to those that frequently use a given feature of your app.

The following steps show how to receive push notifications targeted for your app.

  1. Add a push listener service to your app.

    The name of the class must match the push listener service name used in the app manifest. pinpointManager is a reference to the static PinpointManager variable declared in the MainActivity shown in a previous step. Use the following steps to detect and display Push Notification in your app.

  2. The following push listener code assumes that the app's MainActivity is configured using the manifest setup described in a previous section.

    1import android.content.Intent;
    2import android.os.Bundle;
    3import android.support.v4.content.LocalBroadcastManager;
    4import android.util.Log;
    5
    6import com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClient;
    7import com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationDetails;
    8import com.google.firebase.messaging.FirebaseMessagingService;
    9import com.google.firebase.messaging.RemoteMessage;
    10
    11import java.util.HashMap;
    12
    13public class PushListenerService extends FirebaseMessagingService {
    14 public static final String TAG = PushListenerService.class.getSimpleName();
    15
    16 // Intent action used in local broadcast
    17 public static final String ACTION_PUSH_NOTIFICATION = "push-notification";
    18 // Intent keys
    19 public static final String INTENT_SNS_NOTIFICATION_FROM = "from";
    20 public static final String INTENT_SNS_NOTIFICATION_DATA = "data";
    21
    22 @Override
    23 public void onNewToken(String token) {
    24 super.onNewToken(token);
    25
    26 Log.d(TAG, "Registering push notifications token: " + token);
    27 MainActivity.getPinpointManager(getApplicationContext()).getNotificationClient().registerDeviceToken(token);
    28 }
    29
    30 @Override
    31 public void onMessageReceived(RemoteMessage remoteMessage) {
    32 super.onMessageReceived(remoteMessage);
    33 Log.d(TAG, "Message: " + remoteMessage.getData());
    34
    35 final NotificationClient notificationClient = MainActivity.getPinpointManager(getApplicationContext()).getNotificationClient();
    36
    37 final NotificationDetails notificationDetails = NotificationDetails.builder()
    38 .from(remoteMessage.getFrom())
    39 .mapData(remoteMessage.getData())
    40 .intentAction(NotificationClient.FCM_INTENT_ACTION)
    41 .build();
    42
    43 NotificationClient.CampaignPushResult pushResult = notificationClient.handleCampaignPush(notificationDetails);
    44
    45 if (!NotificationClient.CampaignPushResult.NOT_HANDLED.equals(pushResult)) {
    46 /**
    47 The push message was due to a Pinpoint campaign.
    48 If the app was in the background, a local notification was added
    49 in the notification center. If the app was in the foreground, an
    50 event was recorded indicating the app was in the foreground,
    51 for the demo, you will broadcast the notification to let the main
    52 activity display it in a dialog.
    53 */
    54 if (NotificationClient.CampaignPushResult.APP_IN_FOREGROUND.equals(pushResult)) {
    55 /* Create a message that will display the raw data of the campaign push in a dialog. */
    56 final HashMap<String, String> dataMap = new HashMap<>(remoteMessage.getData());
    57 broadcast(remoteMessage.getFrom(), dataMap);
    58 }
    59 return;
    60 }
    61 }
    62
    63 private void broadcast(final String from, final HashMap<String, String> dataMap) {
    64 Intent intent = new Intent(ACTION_PUSH_NOTIFICATION);
    65 intent.putExtra(INTENT_SNS_NOTIFICATION_FROM, from);
    66 intent.putExtra(INTENT_SNS_NOTIFICATION_DATA, dataMap);
    67 LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    68 }
    69
    70 /**
    71 * Helper method to extract push message from bundle.
    72 *
    73 * @param data bundle
    74 * @return message string from push notification
    75 */
    76 public static String getMessage(Bundle data) {
    77 return ((HashMap) data.get("data")).toString();
    78 }
    79}
  3. To create a new campaign to send notifications to your app from the Amazon Pinpoint console run the following command from your app project folder.

    1amplify notifications console
  4. Provide a campaign name, choose Next, choose Filter by standard attributes, and then choose android as the platform.

  5. You should see 1 device as a targeted endpoint, which is the app you are running on the Android device. Choose the option and then choose Next Step.

  6. Provide text for a sample title and body for push notification, and then choose Next Step.

  7. Choose Immediate, and then choose Next Step.

  8. Review the details on the screen, and then choose Launch Campaign.

  9. A notification should appear on the Android device. You may want to try testing your app receiving notifications when it is in the foreground and when closed.

Next Steps