Solved

Android Push notifications in background

  • 8 December 2023
  • 4 replies
  • 239 views

Badge

Hey,
I’m trying to implement your SDK into our android app. Push notifications are working fine but only when app is opened or it is in tray. When the app is closed push notification is never being delivered.

This is your approach from SDK README, and it works as described above

public class PushNotificationService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("FIREBASE_PUSH_NOTIFICATIONS ", "Notification received");
        super.onMessageReceived(remoteMessage);
        new KlaviyoNotification(remoteMessage).displayNotification(this);


I tried also basic firebase implementation without Klaviyo, and it works even with app closed
 

    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("FIREBASE_PUSH_NOTIFICATIONS ", "Notification received");
        super.onMessageReceived(remoteMessage);

        remoteMessage.getData().forEach((key, value) -> System.out.println(key + ":" + value));

        Intent intent = new Intent(this, PushNotificationService.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(1)
                .setContentTitle("test")
                .setContentText("body").setAutoCancel(true).setContentIntent(pendingIntent);;
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
        manager.notify(0, builder.build());
    }


Is it a bug in your implementation?

Looking for support to solve this problem :)

icon

Best answer by Evan.Masseau 1 May 2024, 18:12

View original

4 replies

Hi,

Thanks for your feedback. I wanted to start by sharing this link to a Github issue on the same topic, in case any of the findings over there can help you. In the past I have not been able to reproduce this issue, and suspected it was outside of our control. Given your feedback that it did indeed work differently than a firebase notification, I’m reopening to take another crack at it. I’ll update here and in the github issue when I have more findings! 

In the meantime to help me diagnose, would you mind verifying (here or in Github if you like) information about the device you tested on e.g. android version, manufacturer, and Klaviyo SDK version? I also just want to check if this is a plain Android app, or if you’re using a hybrid framework like Flutter.

Thanks again

Hi Evan,

 

Hi,

Thanks for your feedback. I wanted to start by sharing this link to a Github issue on the same topic, in case any of the findings over there can help you. In the past I have not been able to reproduce this issue, and suspected it was outside of our control. Given your feedback that it did indeed work differently than a firebase notification, I’m reopening to take another crack at it. I’ll update here and in the github issue when I have more findings! 

In the meantime to help me diagnose, would you mind verifying (here or in Github if you like) information about the device you tested on e.g. android version, manufacturer, and Klaviyo SDK version? I also just want to check if this is a plain Android app, or if you’re using a hybrid framework like Flutter.

Thanks again

 

I believe the issue here is that Background messages only work if the message priority is set to 'high' on the firebase payload. Which means the payload you guys are sending should include the priority for it to trigger properly on background apps. This should probably be an option when creating the push notification on the portal, letting whoever is creating the campaigns or flows to decide if the message is time-sensitive or not. you can read more about Firebase’s priority options here: https://firebase.google.com/docs/cloud-messaging/android/message-priority

 

The payload structure with the android priority should be like this:

payload: [

    token: “”,

    data: [ ...title,body,url ],

    android: [

       priority: “high” // options are high or normal

    ]

]

 

Hope that helps,

Euler R.

Badge +2

I don’t understand how/why this issue is closed. I’m still having the problem, and when I print out the payload that I receive when my app is open it doesn’t include the `priority: “high”` part. I don’t see any controls within Klaviyo to set the priority to high. Is there something else I could be missing?

 

Additionally, the payload only includes the data field, and according to the FCM docs, it looks like in order for the notifications to display when the app is not running it needs to include the “notification” field.

 

Are others having luck with this?

Hi folks, just wanted to add some technical background and encourage you to reach out to Klaviyo support if you’re having trouble with Android notifications.

  • We currently send all Android notifications with a delivery priority of high. You can confirm this in a debugger by inspecting the the RemoteMessage object within onMessageReceived(). Either look at the bundle entry keyed google.original_priority, or call getOriginalPriority()

 

  • There is no user interface for choosing the delivery priority. We always use high because it is the appropriate choice for a message that results in a user-facing notification expecting interaction, which all of our messages do. I’ve passed along the idea to our product team though. 
  • To the question of data vs notification messages: Regardless of the payload, all messages are delivered to your messaging service (registered in your manifest) the same way, via the com.google.firebase.MESSAGING_EVENT intent. The difference between a notification and data message lies in how the firebase SDK dispatches. Since its open source, you can review that code here.
    • A notification message is displayed by the firebase SDK without invoking onMessageReceived.
    • A data message is passed directly to onMessageReceived. We turn that message in to a notification in a very similar way to the firebase SDK. If you wish to inspect the code, take a look at KlaviyoPushService.

 

We have investigated the deliverability question a few times now, this github issue is still the best place to view that discussion. The quick summary is that aside from a bug on an earlier version of our React Native SDK, we haven’t found issues with Klaviyo code. We did patch a react native bug, but otherwise it seems to be either an issue with certain device manufacturers, or with service conflicts that can be resolved by re-ordering the manifest file. 

Reply