Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Multiple Notification sending same data on clicking

Notification in android taking same intent on clicking. I am sending notifications after installing the theme. Consider I install 4 themes and 4 notifications appear in Notification window, but when I click on each notification it will launch perticular activity but the intent is having the same data for each intent.

my code goes like this

    @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);

}

and I am printing intent data in onCreate of ThemeInfo activity as

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

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

The result I am getting in logs is

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
like image 413
Sharanabasu Angadi Avatar asked Jun 10 '13 12:06

Sharanabasu Angadi


1 Answers

I had the same issue, and the problem is that Android is being a little too smart and giving you the same PendingIntents instead of new ones. From the docs:

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. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

Modify your code as follows to supply a unique requestCode:

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

This will ensure that a unique PendingIntent is used, as opposed to the same one.

Note that hashCode() may not be unique, so if possible use another unique integer as the requestCode.

like image 96
Oleg Vaskevich Avatar answered Nov 12 '22 09:11

Oleg Vaskevich