Configuring push notifications (Android)

Prerequisites


Google Firebase Cloud Messaging is necessary to handle Mobile Campaigns sent from Synerise.

  1. Follow the instructions in this article.
  2. Integrate the Firebase project with Synerise. See this article.

Documentation on how to prepare your first push notification is available in our user guide.

Set up Firebase Cloud Messaging for Synerise SDK


  1. Register your service in the AndroidManifest:

       <application
               android:name=".App"
               android:allowBackup="true"
               android:icon="@mipmap/ic_launcher"
               android:label="@string/app_name"
               android:roundIcon="@mipmap/ic_launcher_round"
               android:supportsRtl="true"
               android:theme="@style/AppTheme">
               ...
               <service android:name=".service.MyFirebaseMessagingService">
                   <intent-filter>
                       <action android:name="com.google.firebase.MESSAGING_EVENT" />
                   </intent-filter>
               </service>
           </application>
       

  2. In your application, implement registration for Firebase notifications:

       public class App extends MultiDexApplication implements OnRegisterForPushListener {
    
           private static final String TAG = App.class.getSimpleName();
    
           @Override
           public void onCreate() {
               super.onCreate();
    
                Synerise.Builder.with(this, syneriseClientApiKey, appId)
                                       .notificationIcon(R.drawable.ic_notification_icon)
                                       .pushRegistrationRequired(this)
                                       ...
                                       .build();
           }
    
           @Override
           public void onRegisterForPushRequired() {
               FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> {
                   String refreshedToken = instanceIdResult.getToken();
                   Log.d(TAG, "Refreshed token: " + refreshedToken);
    
                   IApiCall call = Client.registerForPush(refreshedToken, true);
                   call.execute(() -> Log.d(TAG, "Register for Push succeed: " + refreshedToken),
                                apiError -> Log.w(TAG, "Register for push failed: " + refreshedToken));
               });
           }
       }
       

    Important: The second parameter of the registration method is the agreement for mobile push campaigns. In the Profile’s card in Synerise, you can find it in the Subscriptions section (if you have the required access permission). Learn more about the Client.registerForPush(token, mobilePushAgreement) method in the method reference.
  3. Add registerForPush method inside onNewToken callback. This should be done in your class which extends FirebaseMessagingService

           @Override
           public void onNewToken(String refreshedToken) {
               super.onNewToken(refreshedToken);
    
               Log.d(TAG, "Refreshed token: " + refreshedToken);
    
               if (refreshedToken != null) {
                   IApiCall call = Client.registerForPush(refreshedToken, true);
                   call.execute(() -> Log.d(TAG, "Register for Push succeed: " + refreshedToken),
                                apiError -> Log.w(TAG, "Register for push failed: " + refreshedToken));
               }
           }
       

  4. Pass the incoming push notification payload to the Injector in your FirebaseMessagingService implementation:

       public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
           @Override
           public void onMessageReceived(RemoteMessage remoteMessage) {
               super.onMessageReceived(remoteMessage);
    
               boolean isSynerisePush = Injector.handlePushPayload(remoteMessage.getData());
           }
       }
       
    Overriding onMessageReceived(RemoteMessage) stops simple notifications from being displayed while the app is the active screen.

  5. In order to configure a notification icon and notification icon color, you need to set the following two parameters in AndroidManifest.xml, in the application section:

       <meta-data
                android:name="com.synerise.sdk.messaging.notification_icon"
                android:resource="@drawable/ic_notification_icon" />
       <meta-data
                android:name="com.synerise.sdk.messaging.notification_icon_color"
                android:resource="@color/amaranth" />

    The default values are: android icon and white color.

    Note: Check the repository of our sample app for an example usage of building your non-Synerise notification.

Assign notifications to channels

Starting with Android 8.0 (API level 26), all notifications must be assigned to a channel. Otherwise, they are not displayed.

You can implement notifications in one of the following ways:

  • If you already have a channel defined in your application, use the notificationChannelId(String) and NotificationHighPriorityChannelId(String) methods of Builder during SDK initialization.
  • If you want the SDK to set the channel names to default (same as the application name), initialize the SDK without the methods mentioned above.

Callback methods


Note: NotificationListener is available from SDK 4.9.0 version.

If you want to receive callbacks to inform the application about notification’s state, implement OnNotificationListener using the Injector.setOnNotificationListener method.

For details, see this article.

See sample code from the application below:

Injector.setOnNotificationListener(new OnNotificationListener() {
            @Override
            public void onNotificationReceived(NotificationInfo notificationInfo) {
            }

            @Override
            public void onNotificationClicked(NotificationInfo notificationInfo) {
            }

            @Override
            public void onNotificationDismissed(NotificationInfo notificationInfo) {
            }

            @Override
            public void onActionButtonClicked(NotificationInfo notificationInfo, String actionButton) {
            }
        });

Configure notification encryption


To enable encrypted push notifications, you must change the configuration of your workspace in the Synerise Platform. For details, read Google Firebase.

In the mobile application, you must set encryption to true in the notification settings.

Synerise.settings.notifications.setEncryption(true);

The SDK performs the encryption as a part of the Synerise.Notifications.handleNotification method.

If you use only the "Synerise" issuer in push notifications, no more actions are required.

If you need custom integration of encrypted push notifications, implement the following solution:

Map<String, String> data = remoteMessage.getData();
    if (Injector.isPushEncrypted(data)) {
        data = Injector.decryptPushPayload(data);
    }
// your operations on push notification
Note:

The decryptPushPayload method returns raw data when the payload is not encrypted. If the crypter fails, the method returns null.

For more information, read the description of the decryption method.

Handling actions from push notifications


😕

We are sorry to hear that

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

😉

Awesome!

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

Close modal icon Placeholder alt for modal to satisfy link checker