Android - notification manager, having a notificat

2019-01-11 05:44发布

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

3条回答
手持菜刀,她持情操
2楼-- · 2019-01-11 06:39

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.

查看更多
祖国的老花朵
3楼-- · 2019-01-11 06:41

You may pass the parameter

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

instead of

null

on

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

查看更多
Summer. ? 凉城
4楼-- · 2019-01-11 06:42

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

查看更多
登录 后发表回答