2

I have an app in which i have to update notification text and title without sending new notification. How do i do that i have tried below mentioned code.

code:-

notifica("Test", "Testinggggggggggg");
    m_send = (Button) findViewById(R.id.send_another);

    m_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            builder.setContentTitle("Test3");
            builder.setContentText("Test3333333333");
            notificationManager.notify(0, builder.build());

        }
    });
}

public void notifica(String title, String message) {
    int color = getResources().getColor(R.color.colorPrimary);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(this, HomeScreen.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title);
    builder.setContentText(message);
    builder.setAutoCancel(false);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setColor(color);
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
    builder.setSound(defaultSoundUri);
    builder.setContentIntent(pendingIntent);

    notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());
}

}

Ragvender
  • 67
  • 2
  • 7
  • Look at https://stackoverflow.com/questions/14885368/update-text-of-notification-not-entire-notification – Kapil G Aug 01 '17 at 11:28

3 Answers3

6

use this to update the title with same notification_id as you are using 0 for now

builder.setContentTitle(title);
notificationManager.notify(0, builder.build());
Rahul
  • 1,380
  • 1
  • 10
  • 24
2

From Android Developer

notify added in API level 1

void notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Juan
  • 5,525
  • 2
  • 15
  • 26
0

Using same notification id you can update notification text using local broadcast manager.

Ravi
  • 134
  • 1
  • 6