Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update existing notifications in Android ?

I am using the following code for displaying notification . Upon displaying the notification , I go to the activity if I click in the notifications .

void Notify(String notificationTitle, String notificationMessage){
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.noti, notificationTitle,System.currentTimeMillis());

        Intent notificationIntent = new Intent(this, SmsActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(this, notificationTitle,notificationMessage, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
       } 

Now I want to update the text of notifications . How can I do that ?

like image 299
osimer pothe Avatar asked Sep 14 '15 09:09

osimer pothe


People also ask

How do I update the notification text for a foreground service in Android?

When you want to update a Notification set by startForeground(), simply build a new notication and then use NotificationManager to notify it. The key point is to use the same notification id.


1 Answers

You can try this

    //First time    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                        .setContentText(context.getString(R.string.notif_text))
                        .setContentTitle(title)
                        .setSmallIcon(R.drawable.ic_action_alarm_2)
                        .setAutoCancel(false)
                        .setOngoing(running)
                        .setOnlyAlertOnce(true)
                        .setContentIntent(
                                PendingIntent.getActivity(context, 10, 
                                        new Intent(context, YourActivity.class)                                 
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
                                0)
                        )

            notificationManager.notify(id, builder.build());

     //Second time

            builder.setContentTitle(title);
            notificationManager.notify(id, builder.build());
like image 58
Akshay Avatar answered Nov 08 '22 07:11

Akshay