38

I would like to be able to fire a notification to alert the users about a timer that has finished, however i do not wish to have an intent when you click the notification.

I've tried passing in null for the intent

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";

notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(HELLO_ID, notification);
Squonk
  • 48,735
  • 19
  • 103
  • 135
Garbit
  • 5,805
  • 6
  • 39
  • 72
  • 2
    Just a warning about this null method - I was also looking for this answer. On my HTC Desire this causes a reboot of the device! Pretty bad, since the notification was being created on Boot - so I got into a boot-reboot-boot cycle... – Richard Le Mesurier Nov 04 '11 at 09:47
  • 1
    Also interestingly null works fine on the 4.0 emulator. It causes a FC on my Galaxy S2 (2.3). – Timmmm Jan 17 '12 at 19:29

3 Answers3

76

You may pass the parameter

PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)

instead of

null

on

notification.setLatestEventInfo(context, contentTitle, contentText, null);

faradaj
  • 3,629
  • 1
  • 22
  • 21
  • I was calling from a broadcastreceiver and found that it was coming up with a null exception. However I tried this class I found (some guy was making a notification class) and its brilliant - check it out http://stackoverflow.com/questions/5626946/call-notification-from-broadcastreceiver – Garbit Aug 15 '11 at 11:51
  • This was really helpful. I don't know, what an Intent created with an empty construcor should do, but it works perfectly on both 4.0 + and 2.2 for me :) Thanks! – Balázs Édes Aug 06 '12 at 15:11
9

The last parameter in setLatestEventInfo() is a PendingIntent and not an Intent. If you need the notification to not do anything when tapped is to pass an empty PendingIntent which is done as follows: PendingIntent.getActivity(context, 0, null, 0).

Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
  • Bot this and the accepeted answer work just fine but this one is more simple, no need to create an empty `Intent` when you can just pass `null`. – rfgamaral Sep 28 '11 at 20:50
  • 1
    Passing `null` causes force close in android 4.1 – Darcy Oct 11 '12 at 02:39
1

Interesting question and I would like to see if this works. I did a little digging and found a lot of ppl asking the same question. cbursk seems to have found a hack to get this intended functionality, which is to pass a Dialog to the notification intent instead of an Activity. I'm guessing the Dialog does nothing, or is programmed to dismiss itself right away, not sure. But I'm currently looking at this thread and going to test it out.

Community
  • 1
  • 1
Matt K
  • 6,620
  • 3
  • 38
  • 60