Manual display and tracking of push notification
This document describes how to manually build, display and track push notifications from Adobe Journey Optimizer using the AEPMessaging extension. You should use the automatic display and tracking method unless you have a specific requirement to manually build, display, and track push notifications.
Pre-requisites
Integrate and register the Messaging extension in your app.
Sync the push token
To retrieve the push token from Firebase Messaging Service, please follow the tutorial within the Firebase documentation. Then use setPushIdentifier
API to sync the device's push token with profile in Adobe Experience Platform.
Although this API is provided in Mobile Core, the use of this API is required and leveraged by the Adobe Journey Optimizer extension to sync provided push tokens with Adobe Experience Platform services.
Copied to your clipboardpublic class YourApp extends Application {@Overridepublic void onCreate() {super.onCreate();FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {@Overridepublic void onComplete(@NonNull Task<String> task) {if (task.isSuccessful()) {String token = task.getResult();MobileCore.setPushIdentifier(token);}}});}}
Building and displaying notification
In
onMessageReceived
method ofYourAppFirebaseMessagingService
class, create aMessagingPushPayload
object from the remoteMessage.MessagingPushPayload
will unpack the remoteMessage and provide APIs for getting attributes used for creating the push notification.Copied to your clipboardMessagingPushPayload payload = new MessagingPushPayload(remoteMessage);Use the Public APIs of MessagingPushPayload to get the attributes required for creating the push notification.
Copied to your clipboard// Following are a few examples of using the public APIs of MessagingPushPayloadString title = payload.getTitle();String body = payload.getBody();int badgeCount = payload.getBadgeCount();int notificationPriority = payload.getNotificationPriority();String channelId = payload.getChannelId();String icon = payload.getIcon();String imageUrl = payload.getImageUrl();The intent which is created while building the notification needs to be updated with necessary Adobe information to track push notification interactions.
Copied to your clipboardMessaging.addPushTrackingDetails(final Intent intent, // intent which will be used when user interacts with the notification.final String messageId, // message.id which represents the id of the push notificationfinal Map<String, String> data) // message.data which represents the data part of the remoteMessage.Use NotificationManager to create and display the built notification.
Here is a sample code for creating a notification channel and building the notification.
Copied to your clipboardpublic class YourAppFirebaseMessagingService extends FirebaseMessagingService {@Overridepublic void onMessageReceived(@NonNull RemoteMessage remoteMessage) {super.onMessageReceived(remoteMessage);// Create a MessagingPushPayload object from the remoteMessageMessagingPushPayload payload = new MessagingPushPayload(remoteMessage);// Write your code for creating or using an existing notification channel.// Creating an intent and attaching tracking details to it.Intent intent = new Intent(this, MainActivity.class);Messaging.addPushTrackingDetails(intent, remoteMessage.getMessageId(), remoteMessage.getData());// Write your code to create a pending intent// Create a notification builderNotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(payload.getTitle()).setContentText(payload.getBody()).setAutoCancel(true).setContentIntent(pendingIntent);// Write your code to add custom buttons to notification, if neccessary.// Write your code to download and attach media to notification, if neccessary.notificationManager.notify(notificationId, notificationBuilder.build());}}
Tracking push notification interactions
After the application is opened by the user by clicking on the push notification, use the handleNotificationResponse
API to send the push notification interactions feedback to Platform.
Copied to your clipboardMessaging.handleNotificationResponse(final Intent intent, // Intent which contains information related to messageId and datafinal boolean applicationOpened, // whether application was opened or notfinal String actionId) // actionId of the element which performed the custom action.
Sending push notification interaction feedback when application is opened without any custom action
Add the following code where you have access to intent
after the user has interacted with the push notification:
Copied to your clipboardMessaging.handleNotificationResponse(intent, true, null);
Sending feedback when application is opened with a custom action
Similar to the example above, call the handleNotificationResponse
API but this time with a custom action:
Copied to your clipboardMessaging.handleNotificationResponse(intent, true, <actionId>);
Sending feedback when application is not opened but a custom action is performed by the user
Add the following code where you have access to intent
after the user has interacted with the push notification:
Copied to your clipboardMessaging.handleNotificationResponse(intent, false, <actionId>);
Here is a sample code for sending push notification interactions feedback to Platform.
Copied to your clipboardpublic class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final Intent intent = getIntent();// use your unique way to determine isAppOpenFromPushInteractionif (isAppOpenFromPushInteraction) {// tracking application opened with no custom actionMessaging.handleNotificationResponse(intent, true, null);}}}