Push Notifications
The SDK shows Positive User push notifications while your app is in the foreground, counts clicks and gives your code the link of the tapped notification, so you can open the right screen. How push notifications work across platforms: Push Notifications.
Requirements
- The SDK initialized in
Application.onCreate, see Installation. - The FCM token passed to the SDK, see Push Token.
- A
FirebaseMessagingServiceregistered in your manifest. - On Android 13 or later, the notification permission granted by the user. The SDK declares
POST_NOTIFICATIONS, but does not ask for it. Ask for it in your app as described by Android.
Who shows the notification
| App state | Who shows the notification | Your code runs |
|---|---|---|
| Background or closed | Android, from the notification part of the FCM message. The SDK is not involved. | No, until the user taps the notification |
| Foreground | The SDK, when your service passes the message to handleNotification | onMessageReceived |
Both ways use the same icon, colour, channel and image, as long as you set them as described in Appearance. A tap opens your launcher activity in both cases, with the message data in the intent extras.
Setup
-
Pass received messages to
handleNotificationin yourFirebaseMessagingService:override fun onMessageReceived(message: RemoteMessage) {if (UserCom.default?.handleNotification(message.data) == true) return// your own messages} -
Pass the intent of your launcher activity to
handleNotificationClickinonCreateand inonNewIntent, and open the screen for the link when it returnstrue:
- Kotlin
- Java
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) handleNotificationTap(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
handleNotificationTap(intent)
}
private fun handleNotificationTap(intent: Intent) {
if (UserCom.default?.handleNotificationClick(intent) != true) return
val link = intent.getStringExtra("link")
if (!link.isNullOrEmpty() && link != "None") {
// open the matching screen for Uri.parse(link)
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) handleNotificationTap(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleNotificationTap(intent);
}
private void handleNotificationTap(Intent intent) {
UserCom sdk = UserCom.getDefault();
if (sdk == null || !sdk.handleNotificationClick(intent)) return;
String link = intent.getStringExtra("link");
if (link != null && !link.isEmpty() && !link.equals("None")) {
// open the matching screen for Uri.parse(link)
}
}
}
handleNotificationClick returns true only for a new tap. Android hands the same intent to your activity again after a rotation, after the app process was recreated, or when the user opens the app from recents. The repeated intent returns false, so each push notification is counted once and your app does not open the same screen twice.
Send a push notification to your test contact, with the app in the background, and tap it. The click is counted in the campaign statistics once, also after you rotate the device.
Handling
React to notification taps
The link extra is the exact string entered in the panel. A push notification sent without a link carries the string "None", so treat it as no link. The SDK does not open the link itself and does not consume the extras. How to route a link to a screen, including App Links: Opening App Screens.
If your launcher activity is a splash screen that starts another activity, call handleNotificationClick in the splash screen, where the intent of the tap arrives, and pass the link on.
Tell your own notifications apart
handleNotification returns true for a Positive User message, including in-app messages, and false for any other, which your app handles as usual. isUserComNotification checks the data without any other effect.
if (UserCom.default?.isUserComNotification(message.data) != true) {
// your own message
}
Appearance
Icon and colour
Set the icon and colour in your AndroidManifest.xml, inside <application>, with the Firebase keys. Android reads them for notifications shown in the background, and the SDK reads the same keys in the foreground:
<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/notification" />
Use a monochrome icon with a transparent background. Without these keys, Firebase uses your app icon in the background and the SDK uses its own default icon in the foreground.
The SDK ignores the keys com.user.sdk.default_notification_icon and com.user.sdk.default_notification_color. Use the Firebase keys above.
Channel name and description
On Android 8 and later, Positive User notifications use their own channel. Users see its name and description in the notification settings of your app. The defaults are "Marketing Notifications" and "Notifications sent by the app", in English only. Override them in your app's res/values/strings.xml, and translate them in your values-* folders:
<string name="user_com_channel_name">Offers and news</string>
<string name="user_com_channel_description">Promotions and product news</string>
The SDK updates the name and description at every initialize, so a change in your strings or in the device language reaches existing installations.
Channel sound and importance
Android lets an app set the importance, sound and vibration of a channel only when the channel is created. After that, they belong to the user. The SDK creates the channel with high importance, so notifications appear as heads up banners.
To use other settings, create the channel yourself before initialize, with the ID UserCom.NOTIFICATION_CHANNEL_ID. The SDK then keeps your settings and only updates the name and description:
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= 26) {
val channel = NotificationChannel(
UserCom.NOTIFICATION_CHANNEL_ID,
getString(R.string.user_com_channel_name),
NotificationManager.IMPORTANCE_DEFAULT
)
channel.enableVibration(false)
getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
}
UserCom.initialize(this, "YOUR_MOBILE_SDK_KEY", "https://your-domain.user.com")
}
This works only on installations where the channel does not exist yet. Where the channel was already created, by the SDK or by an earlier version of your app, Android keeps its current settings.
Images
An image set in the panel is shown in both app states. In the foreground the SDK downloads it, up to 5 MB, and waits for it at most 5 seconds. If the download fails, the notification is shown without the image.
Methods
handleNotification
Passes a received message to the SDK. For a push notification it shows the notification while the app is in the foreground. For an in-app message it shows the message, now or at the next app open. It never counts a click. Call it from onMessageReceived.
Optional unless marked required
- data
- The data of the FCM message,
message.data.
Returns true for a Positive User message, false for any other. The result can be ignored. Safe to call on the thread Firebase uses for onMessageReceived: the SDK moves its UI work to the main thread.
- Kotlin
- Java
UserCom.default?.handleNotification(message.data)
UserCom sdk = UserCom.getDefault();
if (sdk != null) sdk.handleNotification(message.getData());
The notification is not shown when notifications are off for the app, when the user turned off the channel, or when the notification permission is missing. With UserCom.debugLogging on, the log says which of these applies.
handleNotificationClick
Passes the intent of an activity to the SDK. For a new tap on a Positive User push notification, it reports the click. Call it from onCreate and onNewIntent of your launcher activity.
Optional unless marked required
- intent
- The intent that started or reached the activity.
Returns true only for a new tap on a Positive User push notification. Returns false for any other intent, for in-app messages, and for an intent of a tap already reported, for example after a rotation or when the activity is recreated.
- Kotlin
- Java
val isNewTap = UserCom.default?.handleNotificationClick(intent) == true
UserCom sdk = UserCom.getDefault();
boolean isNewTap = sdk != null && sdk.handleNotificationClick(getIntent());
isUserComNotification
Returns true when the message data is a Positive User message. It has no other effect.
val isPositiveUser = UserCom.default?.isUserComNotification(message.data) == true
NOTIFICATION_CHANNEL_ID
UserCom.NOTIFICATION_CHANNEL_ID is the ID of the notification channel Positive User push notifications use, "userComNotificationChannel". The backend sends this ID with every push notification. Use it to create the channel yourself, see Channel sound and importance.
Deprecated methods
These stay until 3.0:
| Deprecated | Use instead |
|---|---|
onNotification(Context, RemoteMessage) | handleNotification(remoteMessage.data) |
handleRouteFromNotification(Intent) | handleNotificationClick(intent), which it calls |
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| The contact cannot be selected in a push campaign | The contact has no device with a push token: no FCM token was passed to the SDK, or the token moved to another contact that later used the same device. | See Push Token. |
| The push notification is sent, but never arrives on the device | A different Firebase project in the panel, notifications turned off for the app, or the notification permission missing on Android 13 or later. | Check the Firebase project and the notification settings of the app. |
| Notifications look different in the foreground and in the background | The icon or colour is set with the old com.user.sdk.* keys or only in code. | Use the Firebase meta-data keys, see Icon and colour. |
| Push notifications arrive, but clicks are not counted | handleNotificationClick is not called in the activity the tap opens, onNewIntent is missing, or the SDK is initialized after the activity calls it. | Call it in onCreate and onNewIntent of your launcher activity, and initialize in Application.onCreate. |
| Tapping a notification does not open the expected screen | The SDK does not open push notification links. | Read the link extra when handleNotificationClick returns true. See Opening App Screens. |
| Your channel sound or importance is ignored | The channel already existed on the device. | Expected. Android keeps the settings of an existing channel. They apply on new installations. |
| Push notifications arrive late on some networks | The network drops idle connections, for example a company VPN, which delays Firebase delivery for all apps. | Test on mobile data or another Wi-Fi. |
More cases: Android Troubleshooting.