首页 > 解决方案 > 点击 android 推送通知有时不会重定向到应用程序

问题描述

在点击推送通知时,用户通常会被重定向到应用程序,但有些用户在点击推送通知时不会被重定向到应用程序。

对他们来说,通知只是被忽略并且应用程序没有启动。

可能是这些用户很长时间没有打开应用程序,但这会影响重定向到应用程序的行为吗?我还尝试通过强制设备进入打盹模式来重现这一点,但我仍然被重定向到应用程序。

有人可以帮我解决这个问题,用户有时不会在点击推送通知时被重定向到应用程序。

这是我的 AndroidManifest.xml

  <service
        android:name=".screens.fcm.MyFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

这是我的 FirebaseMessagingService

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
            Intent intent = new Intent(this, HomeActivity.class);
            String notificationType = (remoteMessage.getData().get("notification_type") != null) ?
                    messageBody.getData().get("notification_type") : "";
            if ((notificationType.equalsIgnoreCase("main")))
                    intent.putExtra("type", "main");

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteMessage.Notification notification = remoteMessage.getNotification();
            String notificationTitle = notification != null ? notification.getTitle() : "";
            String notificationMessage = notification != null ? notification.getBody() : "";

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, "")
                            .setSmallIcon(R.drawable.notification)
                            .setContentTitle(notificationTitle)
                            .setContentText(notificationMessage)
                            .setAutoCancel(true)
                            .setGroup(GROUP_KEY_HOME_NOTIFICATIONS)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent)
                            .setPriority(NotificationCompat.PRIORITY_HIGH);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("",
                        "Channel1",
                        NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            }

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

标签: androidpush-notificationnavigationfirebase-cloud-messaging

解决方案


推荐阅读