Android多个通知在点击时发送相同的数据

10

在Android中,当点击通知时采用相同的意图。 在安装主题后,我会发送通知。假设我安装了4个主题,那么4个通知会出现在通知窗口中,但是当我点击每个通知时,它将启动特定的活动,但这个意图对于每个意图来说都有相同的数据。

我的代码如下:

    @SuppressWarnings("deprecation")
void sendInstalledNotification(String fileName, String packageName) {
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    String name = "";
    try {
        name += fileName.substring(fileName.lastIndexOf(".") + 1);
    } catch (Exception e) {
        Log.e("NewThemeChooser", "Invalid Package name");
        e.printStackTrace();
    }
    name += " Installed";
    Notification notification = new Notification(R.drawable.ic_launcher_9, name , System.currentTimeMillis());

    Intent intent = new Intent(mContext , ThemeInfo.class);
    Bundle bundle = new Bundle();
    bundle.putString("apkid", packageName);
    bundle.putBoolean("isApplied", false);
    intent.putExtra("bundle", bundle);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
    notification.setLatestEventInfo(mContext, name, "Click to Apply Theme", pendingIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    Log.d("NewThemeChooser__:ThemeChangeReceiver" , "hascode : " + packageName.hashCode() + " installed " + packageName);
    notificationManager.notify(packageName.hashCode(), notification);

}

我正在ThemeInfo活动的onCreate中打印意图数据,如下:

    Bundle bundle = getIntent().getBundleExtra("bundle");
    apkid = bundle.getString("apkid");
    isApplied = bundle.getBoolean("isApplied", false);

    System.out.println("NewThemeChooser__:bundle apkid "  +  apkid );

我在日志中得到的结果是

D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : -186637114 installed com.test.theme.MiCrease
D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : 2106806482 installed com.test.theme.iPhone
D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : -1413669305 installed com.test.theme.Simpsons
D/NewThemeChooser__:ThemeChangeReceiver( 4423): hascode : -2146296452 installed com.test.theme.AnnaTheme
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
I/System.out( 4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease
2个回答

23

我也遇到过同样的问题,问题在于Android聪明了一些,在给你相同的 PendingIntent 而不是新的。根据文档

人们经常犯的一个错误是创建多个 PendingIntent 对象,这些对象只有它们的“extra”内容不同,期望每次都得到不同的 PendingIntent。 这不会发生。用于匹配的 Intent 的部分是由 Intent.filterEquals 定义的相同部分。 如果您使用两个 Intent 对象,这两个对象根据 Intent.filterEquals 是相等的,则您将为它们都获得相同的 PendingIntent

按照以下方式修改您的代码,以提供唯一的 requestCode:

// ...
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, packageName.hashCode(), intent, 0);
// ...

这将确保使用唯一的PendingIntent,而不是相同的。

请注意,hashCode()可能不是唯一的,因此如果可能,请使用另一个唯一的整数作为requestCode


1
实际上我早就得到了这个修复,但是没有更新。谢谢 @Oleg - Sharanabasu Angadi

0
使用了这段代码,对我来说很有效。
      int requestCode = new Random().nextInt();
      PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, 
      notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接