2

I have a problem with updating custom notification value in my service. I use RemoteView and want to update textview every second and I don't have any true idea for doing this. it's my code:

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

contentView = new RemoteViews(getPackageName(), R.layout.notificationview);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
contentView.setTextViewText(R.id.text, "This is a custom layout"); 
contentView.setTextViewText(R.id.title, "Title");
notification.contentView = contentView;

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; 

startForeground(13, notification);

any idea ?

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
Meysam
  • 694
  • 6
  • 20
  • `update textview every second` - that's very bad idea. – Marcin Orlowski Apr 30 '15 at 14:16
  • 1
    @MarcinOrlowski whats your suggestion for updating notification ? – Meysam May 01 '15 at 06:21
  • @MarcinOrlowski What makes it a bad idea? Does it suffer from the same performance issues as widgets? I know that widgets are not recommended at all to update every second, does the same apply to RemoteViews in general, and if so - why? – Simon Forsberg Jul 15 '22 at 08:45
  • in 2015 it could be literally anything, with nasty flickering, performance hit and other things normal back then. it might be not a problem any more as tech advanced still updating things to frequent than it is really needed is always a bad idea and resource waste. – Marcin Orlowski Jul 15 '22 at 10:59

1 Answers1

2

I still haven't completely figured out on how to update the notification text properly BUT what i did notice is that updating TextView in the RemoteView will cause some performance issues and it will also cause the overall ui to slow down quite a lot. Anyways... after messing around with the notification this is currently what i have:

NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(activity);

notiBuilder.setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Sup bro!")
    .setContentText("It works dude!")
    .addAction(R.drawable.icon, "Some Text", pendingAction);

NotificationManager.notify(NOTIFICATION_ID, notify);

and then you can update the notification text like this:

notiBuilder.setContentText("Updating text...");
NotificationManager.notify(NOTIFICATION_ID, notify);

using this method you shouldn't have any performance issue. I'm still working on it, if i figure something out ill let you know.