(OLD, v1.0.0.47) Receiving push messages and showing notifications - GlobalMessageServices/BCS-GMS-SDK-Android GitHub Wiki

Intent broadcast

WARNING The feature is soon to be reworked, so it is not recommended to use it this way yet WARNING

Whenever a push message is received, a broadcast is sent via intent:

const val DEFAULT_BROADCAST_ACTION = "com.push.android.pushsdkandroid.Push"

Intent().apply {
    action = DEFAULT_BROADCAST_ACTION
    sendBroadcast(this)
}

The broadcast can be received using BroadcastReceiver in your code like this:

val mPlugInReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            PushKPushMess.message?.let {
                Log.d("TAG", it)
        }
    }
}

Do not forget to register the receiver, for example:

val filter = IntentFilter()
        filter.addAction(PushKFirebaseService.DEFAULT_BROADCAST_ACTION)
        registerReceiver(mPlugInReceiver, filter)

Receiving push messages with PushKFirebaseService

It is recommended to override the PushKFirebaseService methods as shown below

class MyPushKFirebaseService : PushKFirebaseService(
    PARAM_NOTIFICATIONS_SUMMARY_TITLE_AND_TEXT = Pair("title", "text"),
    PARAM_NOTIFICATIONS_ICON_RESOURCE_ID = android.R.drawable.ic_notification_overlay,
    PARAM_NOTIFICATIONS_STYLE = NotificationStyle.LARGE_ICON
) {
    override fun onReceiveDataPush(appIsInForeground: Boolean, remoteMessage: RemoteMessage) {
        super.onReceiveDataPush(appIsInForeground, remoteMessage)
    }

    override fun onDisplayNotification(appIsInForeground: Boolean, remoteMessage: RemoteMessage) {
        super.onDisplayNotification(appIsInForeground, remoteMessage)
    }

    override fun onUnableToDisplayNotification(
        areNotificationsEnabled: Boolean,
        appIsInForeground: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onUnableToDisplayNotification(
            areNotificationsEnabled,
            appIsInForeground,
            remoteMessage
        )
    }
}

You can configure your notifications by passing the following parameters into the constructor:

  • PARAM_NOTIFICATIONS_SUMMARY_TITLE_AND_TEXT = Pair("title", "text")

Summary notification title and text <title, text>, used for displaying a "summary notification" which serves as a root notification for other notifications

Notifications will not be bundled(grouped) if null

Learn more: https://developer.android.com/training/notify-user/group

  • PARAM_NOTIFICATIONS_ICON_RESOURCE_ID = android.R.drawable.ic_notification_overlay

An icon resource id, this will be used as small icon for notifications

  • PARAM_NOTIFICATIONS_STYLE: NotificationStyle = NotificationStyle.LARGE_ICON

Notification style, all notifications will use the specified style.

Current styles are:

enum class NotificationStyle {
        /**
         * Default style
         * Plain title/text notification
         */
        DEFAULT_PLAIN_TEXT,

        /**
         * shows image as large icon
         * or uses default style if image can not be displayed
         */
        LARGE_ICON,

        /**
         * shows image as big picture
         * or uses default style if image can not be displayed
         */
        BIG_PICTURE
    }

Methods:

  • onReceiveDataPush

Called when a data push is received.

You can remove the super. call to disable notifications, and handle data pushes your way instead. That would disable onDisplayNotification and onUnableToDisplayNotification calls as well.

override fun onReceiveDataPush(appIsInForeground: Boolean, remoteMessage: RemoteMessage) {
    super.onReceiveDataPush(appIsInForeground, remoteMessage)
}
  • onDisplayNotification

Called when a notification is displayed

override fun onDisplayNotification(appIsInForeground: Boolean, remoteMessage: RemoteMessage) {
    super.onDisplayNotification(appIsInForeground, remoteMessage)
}
  • onUnableToDisplayNotification

Called when a notification can not be displayed, but should be. For example when user disabled notifications in the app's settings.

override fun onUnableToDisplayNotification(
        areNotificationsEnabled: Boolean,
        appIsInForeground: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onUnableToDisplayNotification(
            areNotificationsEnabled,
            appIsInForeground,
            remoteMessage
        )
    }

Receiving message when user clicks a notification

When user clicks a notification, your app will be launched, and you can receive an intent with the following action:

DEFAULT_NOTIFICATION_ACTION = "pushsdk.intent.action.notification"

This intent will contain the push data in it's extras as String.

You can obtain it using something like:

intent.extras?.let{
    Log.d("TAG", getString("data", "empty"))
}