Bri*_*anM 129 android android-notifications android-notification-bar android-pendingintent
我通过以下代码在BroadcastReceiver中创建通知:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.ic_stat_notification;
CharSequence tickerText = "New Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,200,200,200};
notification.vibrate = vibrate;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(context, NotificationActivity.class);
notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int mynotification_id = 1;
mNotificationManager.notify(mynotification_id, notification);
Run Code Online (Sandbox Code Playgroud)
当我点击通知时,它会打开NotificationActivity,在Activity中我可以从Intent-Bundle中检索foo_id(例如1)
但是,如果触发另一个通知并再次单击它,则活动仍会从Intent-Bundle接收"旧"值(1).我试图用clear()清除包,但收到的效果相同.我觉得我的代码错了......
Inc*_*App 257
您正在为待处理的强制发送相同的请求代码.改变这个:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)
至:
PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)
如果发送相同的参数,则不会创建意图.它们被重用了.
Chr*_*phK 132
或者,您可以使用以下代码生成PendingIntent:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
来自以下文件PendingIntent.FLAG_UPDATE_CURRENT:
如果描述的PendingIntent已经存在,那么保留它,但用这个新Intent中的内容替换它的额外数据.如果您要创建只有附加内容更改的意图,并且不关心接收到您之前的PendingIntent的任何实体将能够使用您的新附加功能启动它,即使它们未明确赋予它,也可以使用此功能.
小智 40
您传递的是相同的ID.在这种情况下,从这个时间做出一个独特的id:
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
Run Code Online (Sandbox Code Playgroud)
并把它作为这个:
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),iUniqueId, intentForNotification, 0);
Run Code Online (Sandbox Code Playgroud)
对于长时间寻找最佳方法的任何人,您都需要传递PendingIntent.FLAG_UPDATE_CURRENT作为最后一个参数,如下所示
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
您甚至不需要提供新的唯一ID。
您需要下次再做,而不是第一次
| 归档时间: |
|
| 查看次数: |
25401 次 |
| 最近记录: |