Skip to main content

Push Notifications

Positive User sends push notifications through Firebase Cloud Messaging (FCM). The SDK handles token registration, notification display, and analytics automatically.


Setup​

1. Add a Firebase Messaging Service​

Create a service that extends FirebaseMessagingService and pass incoming messages to the SDK:

class FcmService : FirebaseMessagingService() {

override fun onMessageReceived(remoteMessage: RemoteMessage) {
val handled = UserCom.getInstance().onNotification(applicationContext, remoteMessage)

if (!handled) {
// Handle your own notifications here
}
}
}

onNotification() returns true if the message belongs to Positive User (identified by the user_com_notification key) and false otherwise, so it won't interfere with your existing notification handling.

2. Register the service in AndroidManifest.xml​

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

3. Track notification clicks​

Call handleRouteFromNotification() in your launcher Activity's onCreate() to record notification clicks:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

UserCom.getInstance().handleRouteFromNotification(intent)
}

FCM token​

No manual token registration needed - SDK retrieves and registers the FCM token automatically during register().


Customize notification appearance​

By default, the SDK uses a grey dot as the notification icon. To use your own icon and color, add meta-data entries in AndroidManifest.xml.

Firebase-wide defaults (apply to all Firebase notifications):

<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" />

Positive User-specific overrides (apply only to Positive User notifications):

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

Customize the notification channel name​

Override the user_com_channel_name string in your res/values/strings.xml to rename the default notification channel:

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

Manual notification handling​

To access notification data directly, use UserComNotification.create(). Pass either the intent extras (from an Activity) or the remote message data (from FirebaseMessagingService):

// From Activity
val notification = UserComNotification.create(intent.extras)

// From FirebaseMessagingService
val notification = UserComNotification.create(remoteMessage.data)

create() returns null if the data doesn't contain the user_com_notification key.


Parameters​

MethodDescription
onNotification(Context, RemoteMessage)Passes an incoming FCM message to the SDK. Returns true if handled.
handleRouteFromNotification(Intent)Tracks a notification click. Call this from your launcher Activity. Returns true if the intent contains a Positive User notification.