Home > Blockchain >  How to show dynamic variables in a push notification in Android (Kotlin)
How to show dynamic variables in a push notification in Android (Kotlin)

Time:11-28

My application is counting some numbers and I want to show that number in the push notification.

I have 2 questions; how do I pass this number as a parameter to my notification_view.xml and how do I update that number when changed? Do I need to update the current notification using the same notification channel?

I didn't put any code example because I couldn't even find something to try.

CodePudding user response:

you can try to update notification,for example:

mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
    mNotifyBuilder.setContentText(currentText)
        .setNumber(  numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());
  • Related