【问题标题】:Click on notification not launching the app点击通知未启动应用程序
【发布时间】:2019-10-22 10:46:10
【问题描述】:

当应用程序未运行时收到推送通知并且用户按下通知时,它就会消失并且日志显示:

2019-10-22 12:42:45.747 23260-23260/de.app.test.staging E/FirebaseMessaging: Notification pending intent canceled

这是应该作为 Launcher Activity 启动的 SplashActivity:

    <activity
        android:name="de.app.test.login.SplashActivity"
        android:screenOrientation="portrait"
        android:exported="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

这可能是什么问题?

【问题讨论】:

  • 如您所见,尝试将导出添加为 true,但这没有帮助。
  • 分享待处理的意图代码
  • 这个问题是否只发生在 PIE 上。或者您可能没有在清单中配置 firebase 消息传递
  • 目前无法在 Pie 下面的设备上试用,白天会看到。

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

对于任何寻找答案的人来说,后端会将“click_action”发送到通知,因此该 Activity 没有意图过滤器。

对我来说,click_action 是“OPEN_ACTIVITY_1”,所以我只是向我的 SplashActivity 添加了一个意图过滤器,如下所示:

<intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

【讨论】:

    【解决方案2】:

    您需要在 notificationBuilder 实例中设置 pendingIntent 对象,例如

     mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
        Intent intent = new Intent(context, TargetActivity.class));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
    

    请注意,CHANNEL_ID 用于构建版本大于或等于 Oreo

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CHANNEL_ID = createNotificationChannel(CHANNEL_ID, "channelName");
        }
    
    
      @RequiresApi(Build.VERSION_CODES.O)
    private String createNotificationChannel(String channelId, String channelName) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
    
        String description = "description";
    
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
        channel.setLightColor(Color.BLUE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        mNotificationManager.createNotificationChannel(channel);
        return channelId;
    
    }
    

    【讨论】:

    • 当应用程序处于前台时,它可以工作。问题在于应用程序在后台(没有进程运行)。
    【解决方案3】:

    前台应用:

    您需要一个 PendingIntent 才能打开应用。

    试试这个:

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    
    Intent notificationIntent = new Intent(context, SplashActivity.class);
    
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
    

    如果 Activity 处于后台或关闭状态,则应用启动器活动的通知中心会显示通知消息。

    您可以使用 BroadcastReceiver 类。当你关闭应用程序时,广播可以监听这个动作。所以,如果你创建一个 BroadcastReceiver 类,你就不会遇到这个问题。

    public class ClosedBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(new Intent(context, HomePage.class));
            } else {
                context.startService(new Intent(context, HomePage.class));
            }
        }
    }
    

    更多信息:

    https://firebase.google.com/docs/cloud-messaging/android/receive

    【讨论】:

    • 当应用程序处于前台时,它可以工作。问题在于应用程序在后台(没有进程运行)。
    • 你是对的。答案已编辑。 @SavaDimitrijević
    • 感谢您的回答,但这就是问题所在。启动器活动根本没有启动,并且日志显示“Notification pending intent cancelled”
    • 我在哪里注册这个广播,为什么只有设备在奥利奥以上才注册?
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多