[OLD v54] 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())
                    }
                }
            }
        }
    }

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
) {
    override fun setNotificationStyle(
        notificationConstruct: NotificationCompat.Builder,
        data: Map<String, String>,
        notificationStyle: NotificationStyle
    ) {
        //change the notification style by passing a different value as "notificationStyle"
        super.setNotificationStyle(notificationConstruct, data, notificationStyle = NotificationStyle.BIG_TEXT)
        //super.setNotificationStyle(notificationConstruct, data, notificationStyle = NotificationStyle.BIG_PICTURE)
    }

    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:

  • 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 either be achieved by passing a value into setNotificationStyle() method, or overriding it. One of it's parameters is notificationConstruct: NotificationCompat.Builder which you can use for chaining.

  • Specifying one of the styles, provided by the SDK:
override fun setNotificationStyle(
        notificationConstruct: NotificationCompat.Builder,
        data: Map<String, String>,
        notificationStyle: NotificationStyle
    ) {
        super.setNotificationStyle(notificationConstruct, data, notificationStyle = NotificationStyle.BIG_TEXT)
        //super.setNotificationStyle(notificationConstruct, data, notificationStyle = NotificationStyle.BIG_PICTURE)
    }

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 setNotificationStyle(
        notificationConstruct: NotificationCompat.Builder,
        data: Map<String, String>,
        notificationStyle: NotificationStyle
    ) {
        //in this case you should not have the "super.setNotificationStyle()" call
        notificationConstruct.setStyle(NotificationCompat.BigTextStyle())
    }

Callbacks:

  • 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:

//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())
        }
    }
}