Receiving push messages and showing notifications - GlobalMessageServices/BCS-GMS-SDK-Android GitHub Wiki

Intent broadcast

Push data broadcast

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

!not the actual code, ONLY AN EXAMPLE!

Intent().apply {
    action = BROADCAST_PUSH_DATA_INTENT_ACTION
    sendBroadcast(this)
}
//it is recommended to use the constant itself, rather than the value
const val BROADCAST_PUSH_DATA_INTENT_ACTION = "com.push.android.pushsdkandroid.Push"

Queue broadcast

When you call pushSDK.checkMessageQueue(), if there are any undelivered messages - they will be sent via broadcast

!not the actual code, ONLY AN EXAMPLE!

Intent().apply {
    action = BROADCAST_QUEUE_INTENT_ACTION
    sendBroadcast(this)
}
//it is recommended to use the constant itself, rather than the value
const val BROADCAST_QUEUE_INTENT_ACTION= "com.push.android.pushsdkandroid.Push"

Receiving the broadcast

Such intent will contain the push data/queued messages in it's extras as String.

You can obtain it using BroadcastReceiver:

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

    private val mPlugInReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            when (intent.action) {
                PushSDK.BROADCAST_PUSH_DATA_INTENT_ACTION-> {
                    intent.extras?.let {
                        Log.d("TAG1", it.getString(PushSDK.BROADCAST_PUSH_DATA_EXTRA_NAME).toString())
                    }
                }
                PushSDK.BROADCAST_QUEUE_INTENT_ACTION-> {
                    intent.extras?.let {
                        Log.d("TAG2", it.getString(PushSDK.BROADCAST_QUEUE_EXTRA_NAME).toString())
                    }
                }
            }
        }
    }

Note: Upon receiving notifications this way, you should manually send a delivery report for the message if you want one, as delivery reports will not be sent automatically if no notification was sent

Do not forget to register the receiver, for example in your activity:

    override fun onStart() {
        super.onStart()
        val filter = IntentFilter()
        filter.addAction(PushSDK.BROADCAST_PUSH_DATA_INTENT_ACTION)
        filter.addAction(PushSDK.BROADCAST_QUEUE_INTENT_ACTION)
        registerReceiver(mPlugInReceiver, filter)
    }

Receiving push messages with PushKFirebaseService

It is recommended to override the PushKFirebaseService methods as shown below

class MyPushKFirebaseService : PushKFirebaseService(
    summaryNotificationTitleAndText = Pair("title", "text"),
    notificationIconResourceId = android.R.drawable.ic_notification_overlay
) {
    
    /**
     * Called when data push is received from the Messaging Hub
     */
    override fun onReceiveDataPush(
        appIsInForeground: Boolean,
        isDoNotDisturbModeActive: Boolean,
        areNotificationsEnabled: Boolean,
        isNotificationChannelMuted: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onReceiveDataPush(
            appIsInForeground,
            isDoNotDisturbModeActive,
            areNotificationsEnabled,
            isNotificationChannelMuted,
            remoteMessage
        )

        //can be used to configure, for example set "isDoNotDisturbModeActive" to false,
        // to send notifications in "Do not disturb mode" anyways
//        super.onReceiveDataPush(
//            appIsInForeground = appIsInForeground,
//            isDoNotDisturbModeActive = false,
//            areNotificationsEnabled = areNotificationsEnabled,
//            isNotificationChannelMuted = isNotificationChannelMuted,
//            remoteMessage = remoteMessage
//        )
    }

    /**
     * Prepares NotificationCompat.Builder object for showing
     */
    override fun prepareNotification(data: Map<String, String>): NotificationCompat.Builder? {
        return super.prepareNotification(data)
        //can customize NotificationCompat.Builder object here, e.g.:
//        val notificationConstruct = pushSdkNotificationManager.constructNotification(data, PushSdkNotificationManager.NotificationStyle.BIG_TEXT)
//        notificationConstruct?.apply {
//            setContentText("some new text")
//        }
//        return notificationConstruct
    }

    /**
     * Callback - when notification is sent
     */
    override fun onNotificationSent(
        appIsInForeground: Boolean,
        isDoNotDisturbModeActive: Boolean,
        areNotificationsEnabled: Boolean,
        isNotificationChannelMuted: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onNotificationSent(
            appIsInForeground,
            isDoNotDisturbModeActive,
            areNotificationsEnabled,
            isNotificationChannelMuted,
            remoteMessage
        )
    }

    /**
     * Callback - when notification will not be sent
     */
    override fun onNotificationWontBeSent(
        appIsInForeground: Boolean,
        isDoNotDisturbModeActive: Boolean,
        areNotificationsEnabled: Boolean,
        isNotificationChannelMuted: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onNotificationWontBeSent(
            appIsInForeground,
            isDoNotDisturbModeActive,
            areNotificationsEnabled,
            isNotificationChannelMuted,
            remoteMessage
        )
    }

}

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

  • summaryNotificationTitleAndText = 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

  • notificationIconResourceId= android.R.drawable.ic_notification_overlay

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

Specifying notification style

It can be achieved by overriding the prepareNotification() method.

You don't have to override the method if you want default behavior

  • Specifying one of the styles, provided by the SDK:
    override fun prepareNotification(data: Map<String, String>): NotificationCompat.Builder? {
        //return super.prepareNotification(data)
        //can customize NotificationCompat.Builder object here, e.g.:
        val notificationConstruct = pushSdkNotificationManager.constructNotification(data, PushSdkNotificationManager.NotificationStyle.BIG_TEXT)
        notificationConstruct?.apply {
            setContentText("some new text")
        }
        return notificationConstruct
    }

Current styles are:

enum class NotificationStyle {
        /**
         * Shows notification without a style;
         * Text will be displayed as single line;
         * Will display the picture as large icon if push message has one
         */
        NO_STYLE,

        /**
         * Default style (Recommended);
         * Sets "Big text" style to allow multiple lines of text;
         * Will display the picture as large icon if push message has one
         */
        BIG_TEXT,

        /**
         * Shows image as big picture;
         * Or uses default style (no style) if image can not be displayed
         */
        BIG_PICTURE
    }
  • Manually chaining your style to the NotificationCompat.Builder object:
override fun prepareNotification(data: Map<String, String>): NotificationCompat.Builder? {
        //return super.prepareNotification(data)
        //can customize NotificationCompat.Builder object here, e.g.:
        val notificationConstruct = pushSdkNotificationManager.constructNotification(data, PushSdkNotificationManager.NotificationStyle.NO_STYLE)
        notificationConstruct?.apply {
            setContentTitle("some new text")
            setContentText("some new text")
            setStyle(NotificationCompat.BigTextStyle())
        }
        return notificationConstruct
    }

Callbacks:

Callbacks provide certain information that is used to determine whether to send notification or not.

  • 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 onNotificationSent and onNotificationWontBeSent calls as well.

override fun onReceiveDataPush(
        appIsInForeground: Boolean,
        isDoNotDisturbModeActive: Boolean,
        areNotificationsEnabled: Boolean,
        isNotificationChannelMuted: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onReceiveDataPush(
            appIsInForeground,
            isDoNotDisturbModeActive,
            areNotificationsEnabled,
            isNotificationChannelMuted,
            remoteMessage
        )

        //can be used to configure, for example set "isDoNotDisturbModeActive" to false,
        // to send notifications in "Do not disturb mode" anyways
//        super.onReceiveDataPush(
//            appIsInForeground = appIsInForeground,
//            isDoNotDisturbModeActive = false,
//            areNotificationsEnabled = areNotificationsEnabled,
//            isNotificationChannelMuted = isNotificationChannelMuted,
//            remoteMessage = remoteMessage
//        )
    }
  • onDisplayNotification

Called when a notification is sent

Note: Best effort is made, however displaying notification is not guaranteed

override fun onNotificationSent(
        appIsInForeground: Boolean,
        isDoNotDisturbModeActive: Boolean,
        areNotificationsEnabled: Boolean,
        isNotificationChannelMuted: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onNotificationSent(
            appIsInForeground,
            isDoNotDisturbModeActive,
            areNotificationsEnabled,
            isNotificationChannelMuted,
            remoteMessage
        )
    }
  • onNotificationWontBeSent

Called when notification will not be sent. For example when user disabled notifications in the app's settings.

override fun onNotificationWontBeSent(
        appIsInForeground: Boolean,
        isDoNotDisturbModeActive: Boolean,
        areNotificationsEnabled: Boolean,
        isNotificationChannelMuted: Boolean,
        remoteMessage: RemoteMessage
    ) {
        super.onNotificationWontBeSent(
            appIsInForeground,
            isDoNotDisturbModeActive,
            areNotificationsEnabled,
            isNotificationChannelMuted,
            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:

//it is recommended to use the constant itself, rather than the value
const val NOTIFICATION_CLICK_INTENT_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?.let {
    if (it.action == PushSDK.NOTIFICATION_CLICK_INTENT_ACTION) {
        it.extras?.apply {
            Log.d("TAG", getString(PushSDK.NOTIFICATION_CLICK_PUSH_DATA_EXTRA_NAME).toString())
        }
    }
}