无法识别在通知中单击的项目的 ID
Not able to identify id for item clicked on notification
我有多个通知,当我点击一个通知时,我需要将从网络服务获取的发票 ID 传递给其他 Activity
并显示其详细信息。
我面临的问题是以下代码为所有通知提供了相同的发票 ID,我知道出了点问题,但我无法弄清楚。
请指出我的错误
public class SampleSchedulingService extends IntentService {
public SampleSchedulingService() {
super("SchedulingService");
}
List<GetReminder> reminderList;
int invoiceId=0;
String remMes;
InvoiceData1 data1;
int InvM_Id;
public static final String TAG = "Scheduling Demo";
public static int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
@Override
protected void onHandleIntent(Intent intent) {
// BEGIN_INCLUDE(service_onhandle)
// The URL from which to fetch content.
Log.d("MyService", "About to execute MyTask");
reminderList = WebService.invokeGetReminderWS("GetReminder", 41);
if(reminderList!=null){
for(int i=0;i<reminderList.size();i++) { sendNotification(reminderList.get(i).getRemMessage(),reminderList.get(i).getInvM_Id());
}
}
// Release the wake lock provided by the BroadcastReceiver.
SampleAlarmReceiver.completeWakefulIntent(intent);
// END_INCLUDE(service_onhandle)
}
private void sendNotification(String msg, int invM_id) {
try {
Intent notificationIntent = new Intent(this, Result.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
data1=WebService.InvoiceDetailForExeedDiscount1(invM_id);
notificationIntent.putExtra("invoiceList", data1);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.invoice_alert))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
NOTIFICATION_ID++;}
catch (IOException e) {
} catch (XmlPullParserException e) {
}
}
}
我需要为每个通知获取不同的 invm_id
,因为我正在将数据传递给其他结果 Activity
。
这里的标志FLAG_UPDATE_CURRENT:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
表示您创建的最后一个将替换所有之前创建的。为避免这种情况,您必须根据 IntentFilter 使用的标准使它们不同:
There are three Intent characteristics you can filter on: the action, data, and categories
您正在使用相同的操作和类别,并且未指定数据。它将不会 使用您放入 Extras 中的任何内容来过滤它们。我的建议是:
notificationIntent.putExtra("invoiceList", data1);
你把 data1
以某种方式变成一个 Uri,然后 Intent.setData()
来存储它。
您对所有通知使用相同的 PendingIntent
,即使您不这么认为。这其实很明显documented here:
If the creating application later re-retrieves the same kind of
PendingIntent (same operation, same Intent action, data, categories,
and components, and same flags), it will receive a PendingIntent
representing the same token if that is still valid, and can thus call
cancel() to remove it.
Because of this behavior, it is important to know when two Intents are
considered to be the same for purposes of retrieving a PendingIntent.
A common mistake people make is to create multiple PendingIntent
objects with Intents that only vary in their "extra" contents,
expecting to get a different PendingIntent each time. This does not
happen.
要解决此问题,您需要提供不同的 requestCode,因此
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
你应该写
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
我有多个通知,当我点击一个通知时,我需要将从网络服务获取的发票 ID 传递给其他 Activity
并显示其详细信息。
我面临的问题是以下代码为所有通知提供了相同的发票 ID,我知道出了点问题,但我无法弄清楚。
请指出我的错误
public class SampleSchedulingService extends IntentService {
public SampleSchedulingService() {
super("SchedulingService");
}
List<GetReminder> reminderList;
int invoiceId=0;
String remMes;
InvoiceData1 data1;
int InvM_Id;
public static final String TAG = "Scheduling Demo";
public static int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
@Override
protected void onHandleIntent(Intent intent) {
// BEGIN_INCLUDE(service_onhandle)
// The URL from which to fetch content.
Log.d("MyService", "About to execute MyTask");
reminderList = WebService.invokeGetReminderWS("GetReminder", 41);
if(reminderList!=null){
for(int i=0;i<reminderList.size();i++) { sendNotification(reminderList.get(i).getRemMessage(),reminderList.get(i).getInvM_Id());
}
}
// Release the wake lock provided by the BroadcastReceiver.
SampleAlarmReceiver.completeWakefulIntent(intent);
// END_INCLUDE(service_onhandle)
}
private void sendNotification(String msg, int invM_id) {
try {
Intent notificationIntent = new Intent(this, Result.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
data1=WebService.InvoiceDetailForExeedDiscount1(invM_id);
notificationIntent.putExtra("invoiceList", data1);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.invoice_alert))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
NOTIFICATION_ID++;}
catch (IOException e) {
} catch (XmlPullParserException e) {
}
}
}
我需要为每个通知获取不同的 invm_id
,因为我正在将数据传递给其他结果 Activity
。
这里的标志FLAG_UPDATE_CURRENT:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
表示您创建的最后一个将替换所有之前创建的。为避免这种情况,您必须根据 IntentFilter 使用的标准使它们不同:
There are three Intent characteristics you can filter on: the action, data, and categories
您正在使用相同的操作和类别,并且未指定数据。它将不会 使用您放入 Extras 中的任何内容来过滤它们。我的建议是:
notificationIntent.putExtra("invoiceList", data1);
你把 data1
以某种方式变成一个 Uri,然后 Intent.setData()
来存储它。
您对所有通知使用相同的 PendingIntent
,即使您不这么认为。这其实很明显documented here:
If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.
Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen.
要解决此问题,您需要提供不同的 requestCode,因此
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
你应该写
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);