# Receiving a notification

FCM

Firebase Cloud Messaging allows you to send push notifications to your users directly from your app.

INFO

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user reengagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

If you don’t have FCM integrated with your app yet, you have to configure Push Notification by visiting https://firebase.google.com/docs/android/setup (opens new window).

Register token updates

You don’t have to register FCM token. SDK will do it for you.

Define custom notification channel added in 1.1.0

To change default "user.com" sdk channel notification name just override specific string in values/strings.xml.

<string name="user_com_channel_name">Adverts</string>

Passing notification to SDK updated in 1.1.0

In your service which extends FirebaseMessagingService, pass the notification to User.com SDK

public class FcmService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        boolean notificationConsumed =
            UserCom.getInstance().onNotification(getApplicationContext(), remoteMessage);

        if (!notificationConsumed) {
            // Handle your app notification here
        }
    }
}

Remember to initialize your service in AndroidManifest.xml:

<service
    android:name="your.app.package.fcm.FcmService"
    android:exported="false"
    android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

SDK will use only notifications containing user_com_notification tag, so if you have already FCM in your project, SDK will not interfere with existing notifications.

Pass notification clicked event updated in 1.1.0

To pass notification clicked event to User.com web panel you must include handleRouteFromNotification() function in your android.intent.category.LAUNCHER activity. We recommend to use it in Activity.onCreate() method.

  boolean notificationConsumed =
      UserCom.getInstance().onNotification(getApplicationContext(), remoteMessage);

  if (!notificationConsumed) {
      // Handle your app notification here
  }

Stylize notification added in 1.1.0

By default sdk display simple grey dot as notification icon. If you’re defined in AndroidManifest.xml custom Firebase Cloud Messaging icon or color (using one of com.google.firebase.messaging.default_notification_icon or com.google.firebase.messaging.default_notification_color metadata) then sdk will use the same style.

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_notification" />
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

You can also define custom icon and colors only for user.com sdk notifications by define com.user.sdk.default_notification_icon or com.user.sdk.default_notification_color metadata.

<meta-data
    android:name="com.user.sdk.default_notification_icon"
    android:resource="@drawable/ic_notification_user_com" />
<meta-data
    android:name="com.user.sdk.default_notification_color"
    android:resource="@color/colorAccent_user_com" />

Handling notifications Manually handle User.com notification

You can receive UserComNotification object from Activity.getIntent() or com.google.firebase.messaging.RemoteMessage. Simply use UserComNotification.create() factory.

Receiving notification from Activity looks like:

@Nullable UserComNotification notification =
UserComNotification.create(getIntent().getExtras());

and from FirebaseMessagingService.onMessageReceived() like this:

@Nullable UserComNotification notification =
UserComNotification.create(remoteMessage.getData());

These creators return null if passed data does not contain user_com_notification key, so you can use this safely with your own notifications.

In-app message

In-app message will be displayed as default Alert Dialog. Because it’s created without any theme, it will be necessary to style this Dialog to match your app style.

In-app dialog buttons

If you pass a link parameter to in-app push data, then an in-app dialog will show two buttons

  • negative to dismiss dialog with @string/user_com_in_app_message_close_button text
  • positive with text from action_button_title parameter

If the link parameter will be empty, then only positive button with action_button_title text will be displayed.

To change default “Close” button just override specific string in values/strings.xml.

      <string name="user_com_in_app_message_close_button">Dismiss</string>

Stylize in app dialog

To change positive button color override user_com_in_app_message_dialog_positive color in values/colors.xml:

      <color name="user_com_in_app_message_dialog_positive">@color/accent</color>

Remember to pass color explicit without using ?attr value.

You can also style in app dialog overriding one of below styles using by User.com SDK:

  • User.com.InApp.Dialog.Button.Positive - positive button style
<style name="User.com.InApp.Dialog.Button.Positive" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/user_com_in_app_message_dialog_positive</item>
</style>
  • User.com.InApp.Dialog.Button.Negative - negative (Close) button style
<style name="User.com.InApp.Dialog.Button.Negative" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">?android:textColorSecondary</item>
</style>
  • User.com.InApp.Dialog - a whole in app dialog style
<style name="User.com.InApp.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="buttonBarPositiveButtonStyle">@style/User.com.InApp.Dialog.Button.Positive</item>
    <item name="buttonBarNegativeButtonStyle">@style/User.com.InApp.Dialog.Button.Negative</item>
</style>

Chrome Custom Tabs

If In-App Message contains link parameter and the user clicks on positive Dialog button, then he will be redirected to the browser. By default, it will be Chrome Custom Tabs (opens new window) that you can customize to match your app style by invoking a UserCom.Builder.setCustomTabsBuilder(CustomTabsIntent.Builder) function:

@Override
public void onCreate() {new UserCom.Builder()
            .setCustomTabsBuilder(getCustomTabsBuilder())
            .build();
}

private static CustomTabsIntent.Builder getCustomTabsBuilder() {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setToolbarColor(Color.GREEN);
    return builder;
}

You can also disable this feature permanently using UserCom.Builder.openLinksInChromeCustomTabs(boolean) method:

UserCom.Builder().openLinksInChromeCustomTabs(false).build();