在多个通知实例中设置/获取额外内容

时间:2013-09-24 14:57:46

标签: android android-intent notifications

我有一个Android通知,他将ID发送到目标Activity。 为此,我使用了一个与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或更长时间时,问题就出现了。当我在活动的onCreate方法中使用“getExtras”时,它总是具有相同的Id(最后一个通知实例发送的最后一个ID)

我曾尝试更改Intent的旗帜,但我找不到合适的组合

有人可以帮我这个吗?

我怎么做,比方说,3个通知实例,当我点击通知时,向活动发送了3个不同的ID?

这是我的完整代码:

//通知管理员类

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());
}

// ConfirmationActivity Class

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");
    }
}

1 个答案:

答案 0 :(得分: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,但事实上它是出于这些目的所需的。