Push notifications

Configuring Firebase

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 Knowledge Base.

Implementing Firebase Notifications in Applications

  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);
                   call.execute(() -> Log.d(TAG, "Register for Push succeed: " + refreshedToken),
                                apiError -> Log.w(TAG, "Register for push failed: " + refreshedToken));
               });
           }
       }
       
  3. 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));
               }
           }
       
  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.

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) 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.

You can specify your custom action when user 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.

  1. 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.
  2. 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>
    
  3. Send a deep link from app.synerise.com by defining the the Deep link parameter as syne://test?param=value, where:
    • syne and test are the scheme and host provided in the intent filter.
    • parameter is the parameter name.
    • value is the parameter value.
      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.
  4. 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");
       }
       
  5. If you want to implement your own behavior, refer to the following code:
       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 user interacts with the URL action. Return true if the activity should be closed after the action executes, false otherwise.
    • onDeepLink(InjectorSource, String) - callback is fired when a user interacts with the DEEP_LINKING action. Return true if the activity should be closed after the action executes, false otherwise.
😕

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.