Skip to content

Push Notifications

Sam Woodall edited this page Jul 14, 2021 · 8 revisions

This guide walks you through setting up the Rover Campaigns SDK in your app to enable push notifications.

Note: We have designed the SDK to afford implementers with a high degree of control and a minimum of magic. As such, there will be a little bit of boilerplate required below, but this approach will make it easier for you to debug problems and manage whatever unusual integration situations might arise in your app, which is particularly handy when dealing with Google libraries such as FCM.


Receive Firebase Push Token

Because the Rover Campaigns SDK is avoiding directly calling the FCM library itself, you'll need to pass a closure into NotificationsAssembler for requestPushToken to allow it to request the token from Firebase. It was already included in the RoverCampaigns.initialize(…) example given in Installation and Initialization, but it is repeated here for clarity:

NotificationsAssembler(
    // ...,
    requestPushToken = { tokenCallback ->
        FirebaseMessaging.getInstance().token.addOnCompleteListener { task -> 
            if (task.isSuccessful) tokenCallback(task.result) 
        }
    }
)

Firebase Message Receiver

The FCM library calls your Firebase Message Receiver with the incoming push notifications themselves. They need to be delivered to the Rover Campaigns SDK so they can be populated into Android's notification drawer and added to the Rover Notification Center. The FCM library may also call it with a new push token in the event of a remote update to the push token by Google.

Create a Firebase Message Receiver:

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.rover.campaigns.core.RoverCampaigns
import io.rover.campaigns.notifications.PushReceiverInterface

class FirebaseMessageReceiver : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        RoverCampaigns.shared?.resolve(PushReceiverInterface::class.java)?.onMessageReceivedData(
            remoteMessage.data
        )
    }

    override fun onNewToken(newToken: String) {
        RoverCampaigns.shared?.resolve(PushReceiverInterface::class.java)?.onTokenRefresh(
            newToken
        )
    }
}

And add it to the application’s manifest:

<service android:name=".FirebaseMessageReceiver" android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
    <intent-filter>
         <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
     </intent-filter>
</service>

Note: That in the event that you already have Firebase Cloud Messaging configured in your application for receiving non-Rover Campaigns pushes you can merge the logic above (that, as you can see, is calling methods on Rover Campaigns' PushReceiverInterface) into your existing implementations of the token and message receiver methods.