【问题标题】:Notification Click not launch the given Activity on Nexus Phones通知单击未在 Nexus 手机上启动给定的活动
【发布时间】:2014-02-10 14:07:44
【问题描述】:

我正在使用此代码显示本地通知,当通知出现时,然后单击通知要启动 ListActivity,但在 Google nexus 设备上@9​​87654321@ 未在单击通知时启动,但在其他设备上此代码是运行良好。

    Intent notificationIntent = new Intent(context,
            ListActivity.class);
    notificationIntent.putExtra("clicked", "Notification Clicked");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |   Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
    PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager nM = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);

    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.app_icon);
    notify.setContentTitle("Hello World");
    notify.setContentText("");
    notify.setAutoCancel(true);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notify.setSound(alarmSound);
    notify.setLights(Color.BLUE, 500, 1000);
    nM.notify(reqCode, notify.build());

在activity未启动时添加logcat:

03-26 14:22:35.893: W/ActivityManager(515): Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515): Unable to send startActivity intent
03-26 14:22:35.893: W/ActivityManager(515): java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1186)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:741)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:3300)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:252)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:192)
03-26 14:22:35.893: W/ActivityManager(515):     at android.content.IIntentSender$Stub.onTransact(IIntentSender.java:64)
03-26 14:22:35.893: W/ActivityManager(515):     at android.os.Binder.execTransact(Binder.java:404)
03-26 14:22:35.893: W/ActivityManager(515):     at dalvik.system.NativeStart.run(Native Method)

【问题讨论】:

  • nexus 有哪个版本?奇巧(4.4)?
  • 好吧,这是个问题!!!看我的回答
  • 我还检查了nexus 5(Kitkat 4.4)和nexus 4(更新了os kitkat 4.4.2),两者都给出了相同的结果。
  • 是的,我说的是 Kitkat 版本的问题...!!!
  • 您可以尝试低于 4.4 的版本...例如 4.3 或 4.2 等我认为您没有看到我的答案,这是 Android os version kitkat 的问题

标签: android events notifications click


【解决方案1】:

这是 kitkat 4.4 报告的问题,单击通知时未打开活动,这是一个问题 url

http://code.google.com/p/android/issues/detail?id=63236

http://code.google.com/p/android/issues/detail?id=61850

建议的解决方法是取消现有的PendingIntent,或使用PendingIntent.FLAG_CANCEL_CURRENT

试试下面

Activity 中添加标志AndroidManifiest.xml

android:exported="true"

【讨论】:

  • 这些链接中的一个建议是,在目标 Activity 的“activity”标签中添加 android:exported="true" 解决了我在 KitKat 上的问题。谢谢你的链接。
  • PendingIntent.FLAG_CANCEL_CURRENT 解决了我的问题
  • 这解决了我在 kitkat 上的问题,但是,我只能在设备重启后打开活动,有其他人遇到过吗?即在重新启动设备之前,我按下通知并没有任何反应,但重新启动后,如果我按下按钮,它会打开活动
  • PendingIntent.FLAG_CANCEL_CURRENT 为我在运行 Android 4.3 的 Sony Xperia M 上修复了它。
【解决方案2】:

此问题在 Android Kitkat 4.4 和 Lollipop 中很常见。 请补充:

android:exported="true"

在 android manifest 中的 activity 标记中,应该在从状态栏点击通知后打开

【讨论】:

    【解决方案3】:

    您也可以添加PendingIntent.FLAG_ONE_SHOT 来解决此问题。

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    

    【讨论】:

      【解决方案4】:

      在清单中添加 android:exported = "true"

      <activity
                  android:name=".ListActivity"
                  android:label="ListActivity"
                  android:exported="true">
      </activity>
      

      ListActivity 是点击通知时打开的活动。

      【讨论】:

        【解决方案5】:

        我的上述代码适用于除 Kitkat 4.4 和 4.4 + 之外的所有操作系统版本 但我有解决方案,将接收器放在另一个进程中,它适用于所有大多数 Android 操作系统版本...

        像这样..

        活动 android:name=".NotifyReciever" android:process=":remote"

        我们可以在这里了解更多关于流程的信息......

        Should I use android: process =":remote" in my receiver?

        【讨论】:

          【解决方案6】:

          mass 提出的解决方案对我有用,而且非常简单,无需更改代码:

          在您的 AndroidManifest.xml 中,将以下属性添加到您的 &lt;activity&gt; 标签:

          android:exported="true" 
          

          它已在装有 Android 4.4.2 的三星 Galaxy Young 2 中运行。

          【讨论】:

            【解决方案7】:

            尝试替换这个

            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
            PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            

            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
            PendingIntent pIntent = PendingIntent.getActivity(context, reqCode, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
            

            它适用于所有版本,即使使用 Kitkat 也是如此。

            注意:活动将在现有活动的上下文之外启动,因此您必须在 Intent 中使用 Intent.FLAG_ACTIVITY_NEW_TASK 启动标志。

            【讨论】:

              【解决方案8】:

              如果 android:exported="true" 不适合您,只需从您的 LAUNCHER 活动重定向意图。

              在您的 LAUNCHER 活动中检查传入通知,如下所示:

               if(getIntent().hasExtra("type") && getIntent().getStringExtra("type").compareTo("notification")==0){
                      Intent redirect = new Intent(getIntent());
                      redirect.setClass(this,YourNotificationActivity.class);
                      startActivity(redirect);
               }
              

              如果您需要过滤意图,请使用:

               Intent incoming = getIntent();
               if(intent.hasExtra("type") && intent.getStringExtra("type").compareTo("notification")==0){
                  Intent outgoing = new Intent(this, YourNotificationActivity.class);
                  outgoing.putExtra("title", incoming.getStringExtra("title");
                  outgoing.putExtra("content", incoming.getStringExtra("content");
                  startActivity(outgoing);
               }
              

              【讨论】:

                【解决方案9】:

                希望对你有帮助。

                long uniqueId= System.currentTimeMillis();

                    /** This is the part to let your application recognise difference notification when the user click on the notification.
                     *  You could use any unique string to represent your notification. But be sure each notification have difference action name.
                     **/
                    taskDetailIntent.setAction("notifi" + uniqueId);
                
                
                    PendingIntent contentIntent = PendingIntent.
                            getActivity(this, 0, taskDetailIntent, PendingIntent.FLAG_ONE_SHOT);
                
                    builder.setContentIntent(contentIntent);
                
                    /** Set the unique id to let Notification Manager knows this is a another notification instead of same notification.
                     *  If you use the same uniqueId for each notification, the Notification Manager will assume that is same notification
                     *  and would replace the previous notification. **/
                    notificationManager.notify((int) uniqueId, builder.build());
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-08-21
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-04-28
                  相关资源
                  最近更新 更多