Android - notification manager, having a notificat

2019-01-11 06:02发布

问题:

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);

回答1:

You may pass the parameter

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

instead of

null

on

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



回答2:

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).



回答3:

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.