Messaging campaigns
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.
-
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 staticPinpointManager
variable declared in theMainActivity
shown in a previous step. Use the following steps to detect and display Push Notification in your app. -
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;56import 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;1011import java.util.HashMap;1213public class PushListenerService extends FirebaseMessagingService {14 public static final String TAG = PushListenerService.class.getSimpleName();1516 // Intent action used in local broadcast17 public static final String ACTION_PUSH_NOTIFICATION = "push-notification";18 // Intent keys19 public static final String INTENT_SNS_NOTIFICATION_FROM = "from";20 public static final String INTENT_SNS_NOTIFICATION_DATA = "data";2122 @Override23 public void onNewToken(String token) {24 super.onNewToken(token);2526 Log.d(TAG, "Registering push notifications token: " + token);27 MainActivity.getPinpointManager(getApplicationContext()).getNotificationClient().registerDeviceToken(token);28 }2930 @Override31 public void onMessageReceived(RemoteMessage remoteMessage) {32 super.onMessageReceived(remoteMessage);33 Log.d(TAG, "Message: " + remoteMessage.getData());3435 final NotificationClient notificationClient = MainActivity.getPinpointManager(getApplicationContext()).getNotificationClient();3637 final NotificationDetails notificationDetails = NotificationDetails.builder()38 .from(remoteMessage.getFrom())39 .mapData(remoteMessage.getData())40 .intentAction(NotificationClient.FCM_INTENT_ACTION)41 .build();4243 NotificationClient.CampaignPushResult pushResult = notificationClient.handleCampaignPush(notificationDetails);4445 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 added49 in the notification center. If the app was in the foreground, an50 event was recorded indicating the app was in the foreground,51 for the demo, you will broadcast the notification to let the main52 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 }6263 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 }6970 /**71 * Helper method to extract push message from bundle.72 *73 * @param data bundle74 * @return message string from push notification75 */76 public static String getMessage(Bundle data) {77 return ((HashMap) data.get("data")).toString();78 }79} -
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 -
Provide a campaign name, choose
Next
, chooseFilter by standard attributes
, and then chooseandroid
as the platform. -
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
. -
Provide text for a sample title and body for push notification, and then choose
Next Step
. -
Choose
Immediate
, and then chooseNext Step
. -
Review the details on the screen, and then choose
Launch Campaign
. -
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.