Push notifications
Configuring Firebase
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 Knowledge Base.
Implementing Firebase notifications in applications
- 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); call.execute(() -> Log.d(TAG, "Register for Push succeed: " + refreshedToken), apiError -> Log.w(TAG, "Register for push failed: " + refreshedToken)); }); } }
- Add the Firebase registration method:
@Override public void onNewToken(String refreshedToken) { super.onNewToken(refreshedToken); Log.d(TAG, "Refreshed token: " + refreshedToken); if (refreshedToken != null) { IApiCall call = Client.registerForPush(refreshedToken); 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()); } }
- In order to configure 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 and white color.
OverridingonMessageReceived(RemoteMessage)
stops simple notifications from being displayed while the app is the active screen.Note: Check our sample app https://github.com/Synerise/android-sdk for an example usage of building your non-Synerise notification.
Assigning 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)
andNotificationHighPriorityChannelId(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.
Handling links and deep links
You can specify your custom action when customer interacts with your banner or walkthrough.
Currently, two linking actions are available: Open URL and Deep Link.
The SDK allows you to easily handle those actions. By default, an URL opens the browser and deep linking opens an activity.
-
Make your activity available for deep linking by including the following parameters in your AndroidManifest:
<activity android:name=".ui.linking.DeepLinkingActivity"> <intent-filter> <action android:name="syne://test" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="syne" android:host="test" /> </intent-filter> </activity>
Important: Your action name must be the same as your URI scheme and host. -
Optional: Specify an activity to call after closing the activity that was called by deep linking. Provide an additional intent category:
<activity android:name=".ui.linking.DeepLinkingActivity" android:parentActivityName=".ui.linking.ParentDeepLinkingActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="test" android:scheme="syne" /> </intent-filter> </activity>
-
Send a deep link from
app.synerise.com
by defining the theDeep link
parameter as syne://test?param=value, where:syne
andtest
are the scheme and host provided in the intent filter.parameter
is the parameter name.value
is the parameter value.
-
If the link begins with
https
, set Action type in the content editor to Open url when creating the communication in Synerise Application.Tip: If your deep link doesn’t contain the://
characters after the scheme, it is treated as a regular string key and set to the Intent’s action.
This means that you can set an action name (in your AndroidManifest activity’s intent filter) to any string and then match it with the provided deep link. -
Receive data from the deep link by implementing the following code:
String data = intent.getDataString(); if (data != null) { Uri uri = Uri.parse(data); value = uri.getQueryParameter("param"); }
-
If you want to implement your own behavior, refer to the following code:
Important: The callbacks presented below work only with banners and walkthrough.public class App extends Application implements OnInjectorListener { @Override public void onCreate() { super.onCreate(); Synerise.Builder.with(this, syneriseClientApiKey, appId) .notificationIcon(R.drawable.ic_notification_icon) .build(); } @Override public boolean onOpenUrl(InjectorSource source, String url) { // your action here SystemUtils.openURL(this, url); // default behavior return source != InjectorSource.WALKTHROUGH; // default behavior } @Override public boolean onDeepLink(InjectorSource source, String deepLink) { // your action here SystemUtils.openDeepLink(this, deepLink); // default behavior return source != InjectorSource.WALKTHROUGH; // default behavior }
onOpenUrl(InjectorSource, String)
- callback is fired when a customer interacts with the URL action. Returntrue
if the activity should be closed after the action executes,false
otherwise.onDeepLink(InjectorSource, String)
- callback is fired when a customer interacts with the DEEP_LINKING action. Returntrue
if the activity should be closed after the action executes,false
otherwise.
Handling universal linking
In order to implement universal linking, you need to configure it in the AndroidManifest.xml
file in your application.
Add data to your activity in order to connect a URL with a screen.
<activity
android:name=".ui.dev.tracker.TrackerViewActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="synerise.com"
android:pathPrefix="/tracker"
android:scheme="https" />
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
</activity>
Configuring notification encryption
To enable encrypted push notifications, you must change configuration of your workspace in Synerise web app. For details, read Google Firebase.
In the mobile application, you must set encryption
as true in the notifications
settings.
Synerise.settings.notifications.setEncryption(true);
The SDK performs the encryption as 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.