android - 单击通知时应用程序未启动或调用 onNewIntent

标签 android android-notifications

我使用 GCM 成功发送了通知,但是当我点击它们时,没有任何反应。这是我的监听器中的代码,我可以验证它是否正在执行,但是当我在 HomeActivityonCreateonNewIntent 方法上放置断点时> 他们永远不会被击中。此外,如果该应用程序未运行,我仍会收到通知,但该应用程序未启动。我尝试了 here 的建议和 here以及 Intent 标志的几种不同组合。我错过了什么吗?请询问您是否需要我发布更多代码。

来 self 的 GcmListenerService 的代码

    notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(appNameID))
            .setContentText(payload)
            .setSmallIcon(iconID)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), bigIconID));

    Intent resultIntent = new Intent(this, HomeActivity.class);
    resultIntent.putExtra(HomeActivity.NOTIFICATION_ID_KEY, notificationID);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    resultIntent.setAction(HomeActivity.NOTIFICATION_ACTION);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(resultPendingIntent);

    // Set Vibrate, Sound and Light
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;
    notificationBuilder.setDefaults(defaults);
    notificationBuilder.setAutoCancel(true);


    // Post a notification
    notificationManager.notify(NOTIFY_ID, notificationBuilder.build());

最佳答案

您是否在 list 中添加了 android:launchMode="singleTop"。并添加 intent.FLAG_ACTIVITY_SINGLE_TOP

已更新

android:exported="true" 添加到您的 list 中,这可能会解决您的问题。

关于android - 单击通知时应用程序未启动或调用 onNewIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463615/

相关文章:

android - Google Analytics XML 配置文件与 JSON?

android - Jetpack Compose 中的倾斜渐变背景

android - 在xml资源文件中默认键盘没有按键定义创建KeyboardView

android - 从最近的应用程序中刷出应用程序后服务代码未运行

android - java.lang.NoSuchMethodError : android. 应用程序通知$Builder.setColor

android - 是否可以使用云发送带有解析推送通知的附加数据?

Android画一个带边框的半圆

android - 警报管理器和广播接收器不停止

android - 在不使用自定义布局的情况下更改通知 addAction 图标和文本颜色

android - 在状态栏上按清除时通知文本会清除吗?