无法识别ID为项目点击的通知 [英] Not able to identify id for item clicked on notification

查看:279
本文介绍了无法识别ID为项目点击的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个通知,当我点击一个通知我需要通过它,我从web服务获得其他活动按发票ID和显示其详细信息。

I have multiple notifications, when I click on a single notification I need pass its invoice id which I'm getting from a webservice to other Activity and display its details.

我现在面临的问题是,低于code是给我的发票相同ID的所有通知,我知道什么是错的,但我无法弄清楚。

The issue I'm facing is that the below code is giving me same invoice id for all notifications, I know something is wrong, but I wasn't able to figure out.

请指出我的错误。

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 的基础上,我正在将数据传递到其他结果中的每个通知活动

I need to get different invm_id for each notification based on that I'm passing data to other result Activity.

推荐答案

您使用的是相同的PendingIntent 与所有的通知,即使你认为并非如此。这实际上是明确这里记载:

You are using the same PendingIntent with all your notifications, even if you think otherwise. This is actually clearly documented here:

如果创建的应用程序稍后重新检索同类
  的PendingIntent(相同的操作,目的相同的动作,数据,种类,
  和组件,以及相同的标志),它会收到一个的PendingIntent
  重新presenting同理,如果这是仍然有效的,并因此可以调用
  取消()将其删除。

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.

由于这种行为,就知道当两个意图是很重要
  认为是用于检索的PendingIntent的目的是相同的。
  一个常见的​​错误的人提出的是,创建多个的PendingIntent
  与意图的对象,只有在他们的额外的内容有所不同,
  期待每一次得到不同的PendingIntent。这不
  发生。

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.

要解决这个问题,你需要提供不同的,即要求code,所以INSEAD

To solve this you need to provide i.e. different requestCode, so insead of

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
             notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

你应该写

PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
             notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

这篇关于无法识别ID为项目点击的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆