Enabling push notifications with the Connect SDK on Android
Push notifications allow users to instantly receive important information on their mobile devices. To enable push notifications in an Android native mobile app that uses the Connect SDK, you must perform server-side configuration and make changes to the project files for the Android native mobile app.
Server-side configuration
Perform the following server-side configuration steps:
- Go to the Google Developer portal and generate a configuration file for your Android application that includes:
Sender ID
- The Google Cloud Services (GCM) identifier used to authenticate the mobile app with the Android Push Notification Integration services.Server API Key
- A key used by the server to authenticate with the Google Cloud Messaging (GCM) services.
- Create a Pega 7 Platform application and do the following steps:
- On the Mobile tab of the Application rule form, click Custom mobile app.
- Create an Android certificate set that specifies the push notifications Google Server API Key and Sender ID obtained in the previous step.
Remember that to use an Android certificate set, you must also upload a signing certificate, even though it is not used. For more information, see Configuring push notifications on Android. - In the Certificate set name list, select the new Android certificate set created in the previous step.
- Click .
- Send push notifications by using the Push Console or by adding a push notifications shape to a workflow of your application. For more information, see Accessing the Push Console and Adding the push notifications shape to a workflow.
Procedure
Perform the following steps to enable push notifications in an Android native mobile app project that uses the Connect SDK.
- Open the Android Studio project for your mobile app.
- Create a class that extends the
InterfaceNotification.NotificationHandlerProvider
interface as shown in the following example.public class MyApplication extends Application implements Notification.NotificationHandlerProvider { (...) @Override public NotificationHandler getNotificationHandler() { return new NotificationHandler() { @Override public int getSmallIcon() { // chceck https://developer.android.com/reference/android/app/Notification.Builder.html#setSmallIcon(int) return id_of_notification_small_icon; } @Override public void onNotificationReceived(@NonNull Context context, @NonNull NotificationData data) { // do something with received data. Methos is called right before showing notificaion. Can be ignored. } @NonNull @Override public String getNotificationChannelId() { // Supporded from Android 8.0. return ""; } @NonNull @Override public NotificationChannel createNotificationChannel(@NonNull Context context) { // Optional. Supporded from Android 8.0. // Allow to create custom NotificationChannel. Channel must have the same id as result of getNotificationChannelId() method. // check: https://developer.android.com/reference/android/app/NotificationChannel return } @Nullable @Override public PendingIntent createNotificationContentIntent(@NonNull Context context, @NonNull NotificationData notificationData) { // create and return PendingIntent to customize notification clicking event. Return null to ignore notification clicking. // check: https://developer.android.com/reference/android/app/Notification.Builder#setContentIntent(android.app.PendingIntent) return null; } @NonNull @Override public android.app.Notification buildNotification(@NonNull Context context, @NonNull NotificationData data) { // Override creating notification that will be shown. return notification; } }; } }
- In the
Activity
class, enable notifications by adding the following synchronous or asynchronous code using the previously generatedSender ID
that was obtained from the server-side configuration in step 1. You call theenable()
method after you have successfully authenticated to the Pega Platform application.
Synchronous call:try{ pegaApi.notification().register(, context.getString(R.string.gcm_defaultSenderId)); } catch (NotificationException e) { // error occured } // registration completed
Asynchronous call:pegaApi.notification().register(, context.getString(R.string.gcm_defaultSenderId), new Notification.NotificationRegisterCallback() { @Override void onSuccess(){ // registration completed } @Override void onFailure(NotificationException e) { // error occured } });
By calling this method in your project for the mobile app, push notifications are enabled using theNotification.NotificationRegisterCallback()
method.