1

just a simple question: How to make the single-line notification that will disappear after a short time.

Just like Whatsapp's:

Whatsapp notification

My current notification code is still very basic:

Intent intent = new Intent("com.example.MyChat");
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new Notification.Builder(context)
        .setContentTitle("New message from " + name)
        .setContentText(message)
        .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
        .getNotification();

notification.defaults |= Notification.DEFAULT_SOUND;

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);

Thanks

hrsetyono
  • 4,474
  • 13
  • 46
  • 80

2 Answers2

1

You can use AlarmManager to invoke after a short time, say 5 seconds.

Also you can use a service, which gets launched, when first time the notification appears and it could cancel the notification after some time.

here you can cancel Notification using,

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTFICATION_ID);

Have a look at this post, it might help you as well.

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • Is there any built-in `Notifiation` method for this? – hrsetyono Dec 22 '12 at 07:42
  • 1
    No, there is no built in method to cancel notification after some time. either you have to click it or you could invoke a service when application is launched, which sjould cancel it after some time. – Sahil Mahajan Mj Dec 22 '12 at 07:45
  • I'm not cancelling the notification, I just want to show the text across the status bar. Is there a way? – hrsetyono Dec 22 '12 at 07:50
  • 1
    text or a notification.? be specific. for text, we have a very beautifull feature, ***TOAST***. – Sahil Mahajan Mj Dec 22 '12 at 07:52
  • Sorry, I mean the notification. Just like in *Whatsapp* when new message come, it will display a statement across status bar (like picture above). But with my code, it only shows the notification icon which easily ignored. – hrsetyono Dec 22 '12 at 07:59
  • Yes I have read those methods but it doesn't seem to solve my problem. I don't want to cancel the notification, I want it to show a **snippet** of the title when the notification arrived. Sorry for the confusion. – hrsetyono Dec 22 '12 at 09:10
  • 1
    Sorry to say, I cant understand, what exactly you want to do. Do you want to show a message snippet in the notification area or somewhere else. and as far as the above picture is concerned, I'm not able to see it. – Sahil Mahajan Mj Dec 22 '12 at 09:14
  • Here I reupload the image http://imgur.com/3krGb when we receive whatsapp or email, you can see a text snippet in the status bar above (usually for time, battery, signal) – hrsetyono Dec 22 '12 at 10:37
  • 1
    as for image, imgur.com is blocked in our office, so sorry. as far I can say, email also works on push notification and I think whatsapp to works on it. for that you need a huge setup(GCM). I prefer not to mess with it. – Sahil Mahajan Mj Dec 22 '12 at 10:40
  • what you were talking was something matching to GCM notification. otherwise, a service could have solved your issue, it will seem similar like a notification that disappears after some time. – Sahil Mahajan Mj Dec 22 '12 at 12:26
1

This is called the "ticker" and it is shown briefly by the status bar if you set the tickerText property of your Notification.

See setTickerText.

dsandler
  • 2,471
  • 17
  • 11