【问题标题】:Android click on notification does not open the attached ActivityAndroid点击通知没有打开附加的Activity
【发布时间】:2015-08-06 00:12:53
【问题描述】:

当我单击状态栏中的通知时,我想打开一个活动。我在 StackOverflow 上看到了这个答案,但这些答案都不适合我。 此问题仅在 Lollipop 设备上可见,重现的最佳方法是: 1.启动应用程序。 2.后台应用程序。 3. 接收推送通知。 4.点击通知,尝试3-4个推送通知。

以下是我的代码:我还尝试了以下方法:

  1. 添加 Intent.ACTION_MAIN
  2. Intent.CATEGORY_LAUNCHER 到意图
  3. PendingIntent.FLAG_UPDATE_CURRENT。
  4. android:exported="true" 在我的 AndroidManifest.xml 中

但没有任何效果。我只在 Lollipop 设备上看到这个问题。 请注意,我间歇性地看到这个问题。这是否与意图被缓存而不被传递有关。请帮忙。

PendingIntent pendingIntent = getPendingIntent(context, payload);
Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_stat)
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(payload.getMessage())
            .setTicker(payload.getMessage())
            .setContentIntent(pendingIntent)
            .setSound(soundUri);

private PendingIntent getPendingIntent(Context context, NotificationPayload payload) {
    int requestID = (int) System.currentTimeMillis();
    Intent intent = new Intent(context, DeepLinkActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage(BuildConfig.APPLICATION_ID);
    intent.putExtra(NotificationHelper.KEY_NOTIFICATION_PAYLOAD, payload);
    return PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
}

【问题讨论】:

  • 您是否将pendingIntent 设置为您的通知构建器?
  • 我也有同样的问题.. 到目前为止没有运气

标签: android android-activity push-notification android-pendingintent


【解决方案1】:

尝试在意图中添加 Intent.FLAG_ACTIVITY_CLEAR_TASK 标志

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

这似乎解决了我的问题

【讨论】:

    【解决方案2】:

    您是否在通知的 setContentIntent 方法中设置了待处理的 Intent?

    试试这个,它适用于所有 api 版本

            Intent intent = new Intent(this, TestActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
    
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(getResources().getString(R.string.notification_title))
                    .setContentIntent(pendingIntent);
    
      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    

    【讨论】:

    • 好的,我在 kitkat 上测试过,效果很好。我只在棒棒糖设备上看到这个问题。 @androholic 在棒棒糖上对你有用吗?
    • 我已经编辑了我的答案以包含我刚刚在棒棒糖上测试的代码,它工作正常。另外,删除 requestId。 0 值代替待处理意图本身中的 requestId 就足够了。试试上面的代码。
    • 我正在使用 ParsePushBroadcastReceiver。我发布的所有代码都发生在接收者的重写 getNotification 方法中,该方法的返回类型为 Notification。我没有看到调用notificationManager.notify() 的方法。可能听起来很傻,但请帮助我如何在此设置中使用 NotificationManager。
    • 您有两种选择: 1. 在 getNotification() 方法中返回 notificationBuilder.build() 并根据您的方便使用通知。 2. 使用上述代码,在getnotification()方法中使用通知管理器显示通知,返回null。
    • 这对我不起作用。您是否还为 AndroidManifest.xml 中的活动设置了exported="true"?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多