Android的多发的通知上点击相同的数据 [英] Android Multiple Notification sending same data on clicking

查看:142
本文介绍了Android的多发的通知上点击相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中采取同样的意图点击通知。 我安装了这个主题后发送通知。想想我安装4个主题和4通知出现在通知窗口,但是当我点击每一个通知,将推出perticular活动,但其目的是为每个意图相同的数据。

我的code是这样的

  @燮pressWarnings(德precation)
无效sendInstalledNotification(字符串文件名,字符串的packageName){
    NotificationManager notificationManager =(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    字符串名称=;
    尝试 {
        名+ = fileName.substring(fileName.lastIndexOf()+1。);
    }赶上(例外五){
        Log.e(NewThemeChooser,无效的包名);
        e.printStackTrace();
    }
    名+ =已安装;
    通知通知=新的通知(R.drawable.ic_launcher_9,姓名,System.currentTimeMillis的());

    意向意图=新的意图(mContext,ThemeInfo.class);
    束束=新包();
    bundle.putString(apkid的packageName);
    bundle.putBoolean(isApplied,假);
    intent.putExtra(包,包);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,意向,0);
    notification.setLatestEventInfo(mContext,姓名,点击应用主题,pendingIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    Log.d(NewThemeChooser __:ThemeChangeReceiver,有code:+ packageName.hash code()+安装+的packageName);
    notificationManager.notify(packageName.hash code(),通知);

}
 

和我在ThemeInfo活动的onCreate打印意图数据作为

 捆绑包= getIntent()getBundleExtra(捆绑)。
    apkid = bundle.getString(apkid);
    isApplied = bundle.getBoolean(isApplied,假);

    的System.out.println(NewThemeChooser __:捆绑apkid+ apkid);
 

结果我得到的日志是

  D / NewThemeChooser __:ThemeChangeReceiver(4423):已code:-186637114安装com.test.theme.MiCrease
D / NewThemeChooser __:ThemeChangeReceiver(4423):有code:2106806482安装com.test.theme.iPhone
D / NewThemeChooser __:ThemeChangeReceiver(4423):有code:-1413669305安装com.test.theme.Simpsons
D / NewThemeChooser __:ThemeChangeReceiver(4423):已code:-2146296452​​安装com.test.theme.AnnaTheme
我/的System.out(4423):NewThemeChooser __:束apkid com.test.theme.MiCrease
我/的System.out(4423):NewThemeChooser __:束apkid com.test.theme.MiCrease
我/的System.out(4423):NewThemeChooser __:束apkid com.test.theme.MiCrease
我/的System.out(4423):NewThemeChooser __:束apkid com.test.theme.MiCrease
 

解决方案

我有同样的问题,而问题是,Android正在成为一个有点太聪明,给你相同的 PendingIntent 的S,而不是新的。从 文档

  

一个常见的​​错误的人做是创建多个 PendingIntent 对象意图 s表示只有改变他们的额外的内容,希望得到一个不同的 PendingIntent 各一次。这不会发生。的的部分意图时使用的匹配是相同的由 Intent.filterEquals 定义。如果使用两个意图对象是等价按 Intent.filterEquals ,那么你会得到相同的<$ C C> PendingIntent $对他们俩的。

修改您的code如下,以提供一个独特的请求code

  // ...
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,packageName.hash code(),意向,0);
// ...
 

这将确保一个独特的 PendingIntent 时,而不是同一个。

注意散code()可能不是唯一的,所以如果可能使用另一种独特的整数为请求code

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

解决方案

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.

这篇关于Android的多发的通知上点击相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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