Configuring push notifications (Android)
Prerequisites
Google Firebase Cloud Messaging is necessary to handle Mobile Campaigns sent from Synerise.
- Follow the instructions in this article.
- 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
-
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>
-
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. -
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)); } }
-
Pass the incoming push notification payload to the
Injector
in yourFirebaseMessagingService
implementation:public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); boolean isSynerisePush = Injector.handlePushPayload(remoteMessage.getData()); } }
onMessageReceived(RemoteMessage)
stops simple notifications from being displayed while the app is the active screen. -
In order to configure a notification icon and notification icon color, you need to set the following two parameters in
AndroidManifest.xml
, in theapplication
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
andwhite color
.Note: Check the repository of our sample app for an example usage of building your non-Synerise notification.
Keep Firebase token always up-to-date
You must always keep the Firebase token updated. In many cases in the application lifecycle, such as authorization, destroyed sessions, user context change, periodic jobs (Work Manager), and so on, the registration needs to be updated. In these situations, the SDK invokes the snr_registerForPushNotificationsIsNeeded(origin) method or snr_registerForPushNotificationsIsNeeded() method.
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
notificationDefaultChannelId(String)
andNotificationHighPriorityChannelId(String)
methods of Builder during SDK initialization. - You cannot configure more than two notification channels.
- 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
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
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.