0

我有一个向目标 Activity 发送 Id 的 Android 通知。为此,我使用带有 putExtra 的 Intent,如下所示:

Intent resultIntent = new Intent(this.context, ConfirmationActivity.class);

resultIntent.putExtra("Id", idToSent);

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

resultPendingIntent = PendingIntent.getActivity(this.context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

问题是当我调用通知 2、3 或更长时间时。当我在Activity的onCreate方法中使用“getExtras”时,它总是有相同的Id(最后一个通知实例发送的最后一个Id)

我曾尝试更改 Intent's Flags,但找不到正确的组合

有人可以帮我弄这个吗?

例如,我如何拥有 3 个通知实例,当我单击通知时,将 3 个不同的 Id 发送到 Activity?

这是我的完整代码:

//通知管理器类

public void sendNearPromotionNotification(int idToSent){

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this.context);

    notificationBuilder.setContentTitle(context.getResources().getString(R.string.Title));
    notificationBuilder.setContentText(context.getResources().getString(R.string.Text));
    notificationBuilder.setTicker(context.getResources().getString(R.string.Ticket));
    notificationBuilder.setSmallIcon(R.drawable.icon);

    int apiLevel = Integer.valueOf(android.os.Build.VERSION.SDK_INT);

    PendingIntent resultPendingIntent = null;   

    if (apiLevel < Build.VERSION_CODES.JELLY_BEAN){     

        Intent resultIntent = new Intent(this.context, ConfirmationActivity.class);

        resultIntent.putExtra("Id", idToSent);

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        resultPendingIntent = PendingIntent.getActivity(this.context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    }else{

        Intent addWishListIntent = new Intent(this.context,  IntentServiceNotifications.class);

        addWishListIntent.putExtra("Id", idToSent);

        PendingIntent pendingIntent = PendingIntent.getService(this.context, 0, addWishListIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        notificationBuilder.addAction(R.drawable.icon2, "Add a WishList", pendingIntent);
    }

    notificationBuilder.setAutoCancel(true);

    notificationBuilder.setContentIntent(resultPendingIntent);

    NotificationManager notificationManager = (NotificationManager)this.context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(idToSent, notificationBuilder.build());
}

//确认活动类

public class PromotionConfirmationActivity extends FragmentActivity {

    private int id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setTheme(android.R.style.Theme_Translucent_NoTitleBar);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_transparent);

        Bundle extras = getIntent().getExtras();

        if(extras != null)
            id = extras.getInt("Id");
    }
}
4

1 回答 1

0

尝试这样的事情:

    private static class IDGenerator {

         private static final AtomicInteger counter = new AtomicInteger();

         public static int nextValue() {
             return counter.getAndIncrement();
         }
    }


    ...

    resultPendingIntent = PendingIntent.getActivity(this.context, IDGenerator.nextValue(), resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

该文档声称未使用作为第二个参数发送的 id,但实际上出于此类目的需要它。

于 2013-09-24T16:01:36.170 回答