意图额外创建Android的通知时PendingIntent使用FLAG_UPDATE_CURRENT时,重复意图、通知、FLAG_UPDATE_CURRENT、Android

2023-09-12 08:51:10 作者:痴情少年丶Toile

我要创建几个通知,会启动一个活动(或刷新),以显示产品说明。

 的通知的通知=新的通知(R.drawable.applicationicon,
            Resources.getString(NewSaleNotification,上下文),
            System.currentTimeMillis的());
//隐藏其选中后通知
notification.flags | = Notification.FLAG_AUTO_CANCEL;

意向意图=新的意图(背景下,MainApplication.class);
intent.putExtra(saleid,saleid);

//以确保该活动将不会重新启动
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(背景下,0,意图,
            PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(背景下,SaleTitle,SaleMessage,pendingIntent);
notificationManager.notify(saleid,通知);
 

当我创建了PendingIntent,我有4个选择:FLAG_CANCEL_CURRENT,FLAG_NO_CREATE,FLAG_ONE_SHOT和FLAG_UPDATE_CURRENT

的最后一项的定义(http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT)就是我想要做的,但它并不如它应该工作,如果我创建2个通知,它们都具有相同的saleid额外这是最新的一个。我怎样才能使多个通知不同的saleid额外的?

解决方案   

但它并不如它应该工作

是的,确实如此。

  

如果我创建2个通知,它们都具有相同的saleid额外这是最新的一个。

android Notification

这是precisely什么 说是应该发生的文档。

  

我怎样才能使多个通知用型动物saleid额外的?

选项#1:将不同的操作字符串中的每一个你的意图的。这会让他们不同(从 filterEquals()的角度来看),并给他们分开 PendingIntents 。但是,因为你是在指定的组件中的意图 MainApplication.class ),该操作不会影响如何意图路由。

选项2:使用不同的请求code (第2参数)在 getActivity()呼叫。虽然这被记录为当前未使用,它并导致不同的 PendingIntent 对象返回。然而,由于该行为是未记录,它可能会在未来改变

I want to create several notifications that launches an activity (or refresh it) to display a product description.

Notification notification = new Notification(R.drawable.applicationicon,
            Resources.getString("NewSaleNotification", context),
            System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MainApplication.class);
intent.putExtra("saleid", saleid);

// to be sure the activity won't be restarted
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, SaleTitle, SaleMessage, pendingIntent);
notificationManager.notify(saleid, notification);

When I create the PendingIntent, I have 4 choices : FLAG_CANCEL_CURRENT, FLAG_NO_CREATE, FLAG_ONE_SHOT and FLAG_UPDATE_CURRENT.

The definition of the last one (http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT) is what I want to do but it doesn't work as it should. If I create 2 notifications, they both have the same 'saleid' extra which is the latest one. How can I make more than one notification with different 'saleid' extra?

解决方案

but it doesn't work as it should

Yes, it does.

If I create 2 notifications, they both have the same 'saleid' extra which is the latest one.

This is precisely what the documentation says is supposed to happen.

How can I make more than one notification with differents 'saleid' extra?

Option #1: Put a different action string in each of your Intents. This will make them different (from the standpoint of filterEquals()) and give them separate PendingIntents. However, since you are specifying the component in the Intent (MainApplication.class), the action will not affect how the Intent is routed.

Option #2: Use a different requestCode (2nd parameter) on your getActivity() calls. While this is documented as "currently not used", it does result in different PendingIntent objects being returned. However, since this behavior is undocumented, it may change in the future.