0

As Richard said in this question > Possible to continuously update text in the notification area? , I have the same issue here .
I want to update text in the notification area .
This question is answered and accepted , but the answer doesn't help me .
It's about to create text as bitmap dynamically and set it to Small Icon of notification .
But as Richard commented , the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly.

Please kindly show me the right way to do this stuff .

Community
  • 1
  • 1
zey
  • 5,939
  • 14
  • 56
  • 110

2 Answers2

0

It's quite simple to update your Notification. First of all when you create it you must create it with an id.

NotificationManager 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("Title")
    .setContentText("Text")
    .setSmallIcon(R.drawable.ic_notify);
mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

Now to update your Notification you only need to notify the new notification with the old id and this will be updated (you don't need to reset the paraneter you don't want to update).

  mNotifyBuilder.setContentText(setContentText("New Text")
  mNotifyBuilder.setSmallIcon(R.drawable.ic_new_notify);
    //this update your notification
    mNotificationManager.notify(
                notifyID,
                mNotifyBuilder.build());
Matteo Corti
  • 484
  • 2
  • 13
  • Thanks for your answer , I've already done with this code . Check this link >> (http://developer.android.com/guide/topics/ui/notifiers/notifications.html ) But it's just update the content text of `Notification Drawer` , what I want is to show text in `Notification Area` and to update this text continuously . Notice - to update in `Notification Area` , not in `Notification Drawer` . – zey Sep 29 '14 at 18:14
  • Ok, but to do so you have to dinamically create an icon with your text and then update it it's the only way to achive what you want... – Matteo Corti Sep 29 '14 at 18:20
  • As I mentioned in my question `the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly`. Isn't it ? Or , is there any way to do it ? – zey Sep 29 '14 at 18:22
  • You could dinamically create a bitmap with text here an example [link] (http://snipplr.com/view/73714/android-draw-text-to-dynamically-sized-bitmap/). Or you can use instead this method [link] (http://stackoverflow.com/questions/4688052/animation-in-notification-bar-cutsom-view) which achive what you want – Matteo Corti Sep 29 '14 at 18:25
  • Sorry I want to apologize for my istake but `setSmallIcon(int drawableid)` method only work with existing drawable resurces, to you use a bitmap you have to use `setLargeIcon(Bitmap icon)` but if don't make another mistake (now I'm going to check), this icon is show in the notification drawer and not in the notification area – Matteo Corti Sep 29 '14 at 18:32
  • This tutorial will let you make whatever you want with your notification by creating a custom notification view http://www.framentos.com/en/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/. Choose you what of this solution do you prefer. I hope to have helped you. – Matteo Corti Sep 29 '14 at 18:38
  • Oh , Custom notification ? That may be helpful , ok , I'm gonna check this out . Thank you by the way :) You help me a lot . – zey Sep 29 '14 at 18:44
  • Ok then let me no of You reach what you achive – Matteo Corti Sep 30 '14 at 05:32
  • It's doesn't match my need @Corti , it's just replacing custom notification xml to default .No process else changed .When I use this code, the image icon and text are show in notification area just like default.when I update the notification , my notification placed in the whole notification area . I don't want to interrupt user when the program is run in the background . I just want to update the text in notification area ! – zey Sep 30 '14 at 18:29
  • @zey I'm sorry but the only way to achive what you want is to use predefinited drawable image. – Matteo Corti Sep 30 '14 at 18:36
  • Yes @Corti , my app shows the data usage , kilobytes per second in notification .If I use predefined drawable image , i would have to make drawable images count of 1024 . Isn't it ? – zey Sep 30 '14 at 18:44
  • Yes it seem to be the only way to achive what you want. To use a dinamically generated bitmap you must use the Large Icon but it is shown in the drawable. – Matteo Corti Sep 30 '14 at 18:46
0

I tried to make one but it has one disadvantage is that, it has one empty notification in notification drawer,

public static int when = 0;
private void generateNotification(Context context) {
    Log.i(TAG, "generateNotification");
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mNotification = new NotificationCompat.Builder(
            this)
            // .setContentTitle("AppName")
            // .setContentText("Message For Notification Drawer")
            // .setSound(soundUri)
            // .setDefaults(Notification.DEFAULT_SOUND)
            // .setVibrate(new long[] { 1000, 1000 })
            // .addAction(R.drawable.ic_launcher, "View", pIntent)
            // .addAction(0, "Remind", pIntent)
            // .setNumber(fCount)
            // .setWhen(when)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker(Integer.toString(when)) // Set String you want
            .setAutoCancel(true)
            .setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // notificationManager.notify(when, mNotification.build());
    notificationManager.notify(1, mNotification.build());
    when++;
}
Chitrang
  • 5,097
  • 1
  • 35
  • 58