【问题标题】:Android GCM notification doesn't redirects to intended activity when app is in background当应用程序处于后台时,Android GCM 通知不会重定向到预期的活动
【发布时间】:2016-11-29 06:13:14
【问题描述】:

您好,我创建了如下通知意图

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK );
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(getNotificationIcon())
        .setContentTitle(title)
        .setContentText(text)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);


int notification_id = (int) System.currentTimeMillis();
notificationManager.notify(notification_id, notificationBuilder.build());

如果应用程序关闭,一切都会完美运行。但如果应用程序在后台运行,它只会将上次打开的活动从后台弹出到前台。我想转到数据中收到的确切页面。 有人帮忙,我尝试了FLAGS的多种组合。

【问题讨论】:

  • 试试这个intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  • 为要在点击通知时启动的活动添加清单代码。
  • 请发布您的清单

标签: android android-intent android-notifications android-pendingintent


【解决方案1】:

只是一个解决办法试试这个

PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

而不是

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

  • 工作。但 Studio 在Must be on or more of:PendingIntent....... 行中显示警告
  • 你应该在 Intent.FLAG_ACTIVITY_NEW_TASK 的地方传递 PendingIntent.FLAG_ONE_SHOT 因为这个 Intent Flag 不推荐用于 PendingIntent.getActivity 方法。
  • Intent.FLAG_ACTIVITY_... 传递给对PendingIntent.getActivity() 的调用是错误的。此调用接受 PendingIntent 标志,而不是 Intent 标志。
  • 感谢@DavidWasser 为有用的提示投票
【解决方案2】:

不是这个

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK );

按你的意图试试这个

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

FLAG_ACTIVITY_SINGLE_TOP 如果设置了这个标志,如果 Activity 已经在历史堆栈的顶部运行,则不会启动它。

【讨论】:

  • 不工作..通过设置上述标志,当前打开的活动被显示。我想要的是每次点击通知时都会调用 onCreate 应用程序是在后台还是在前台.l
  • 你是否在你的 android 清单中添加了 android:exported="true" 在你应该在点击通知后打开的活动标签中?
【解决方案3】:

AS @rahul 指出,如果您将PendingIntent.FLAG_UPDATE_CURRENT 更改为Intent.FLAG_NEW_ACTIVITY_TASK,它会按我的要求工作,但我发现的一个问题是,如果您在通知栏中有多个通知,那么只有一个操作会起作用。当点击其余的时,它将从那里清除,没有任何操作将起作用。以下是正确的解决方案-

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(getNotificationIcon())
                        .setContentTitle(title)
                        .setContentText(text)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

【讨论】:

  • 像这样调用setFlags() 两次会用第二次调用覆盖标志。您最终将只设置FLAG_ACTIVITY_NEW_TASK,这不是您想要的。要么使用addFlags() 而不是setFlags(),要么将两个标志都放在setFlags() 的参数中,如下所示:setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
【解决方案4】:

如果您想使用Intent.FLAG_ACTIVITY_CLEAR_TASK,您还必须使用 Intent.FLAG_ACTIVITY_NEW_TASK。它甚至在文档中这样说。

【讨论】:

    【解决方案5】:

    为清除现有任务和开始新任务添加标志。

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    

    【讨论】:

    • 如果指定FLAG_ACTIVITY_CLEAR_TASKFLAG_ACTIVITY_CLEAR_TOP 是不相关且多余的
    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多