【问题标题】:App is reload If click firebase notification when app is in background应用程序重新加载如果在应用程序处于后台时单击 Firebase 通知
【发布时间】:2017-06-28 07:24:07
【问题描述】:

当应用程序正在运行并且如果我点击它时通知会发出警报,通知会转到那里(onMessageReceived 正在运行)

但是当应用程序在后台并且如果我点击它时通知会发出警报,应用程序会再次重新加载(最初例如启动屏幕),而不是去那里并且onMessageReceived 没有运行

如何解决?

ps。对不起,我的英语不好。

【问题讨论】:

  • 你解决了吗?我遇到了同样的问题

标签: android firebase notifications firebase-notifications


【解决方案1】:

您需要将所需的活动作为意图传递,而不是 MainActivity

【讨论】:

  • Intent intent = new Intent(context, TargetActivity.class);
【解决方案2】:

为待定意图提供活动名称 ....

      Intent intent = new Intent(this, NotificationCheck.class);       
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("App Name")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

【讨论】:

    【解决方案3】:

    见下面的代码。我正在使用它,它正在打开我的 HomeActivity。

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    //change HomeActivity.class which activity you want to open after clicking notification
    Intent notificationIntent = new Intent(context, HomeActivity.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);
    

    更改 HomeActivity.class 单击通知后要打开的活动..希望它会起作用...

    【讨论】:

    • 我的应用程序在后台时,如果我点击它,通知是警报,onMessageReceived 没有运行
    • 你能把你的代码放在这里吗.. bocaz 没有代码我们无法理解这个问题。
    【解决方案4】:
    /**
     * Method checks if the app is in background or not
     */
    public static boolean isAppIsInBackground(Context context) {
        boolean isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;
            }
        }
    
        return isInBackground;
    }
    

    您可以检查应用程序是否在后台,然后在 MyFirebaseMessagingService 类中您可以使用此方法处理消息并处理通知。

    private void handleNotification(String message) {
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
    
            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        }else{
            // If the app is in background, firebase itself handles the notification
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 推送方法isAppIsInBackground的类在哪里
    • 将此方法放在 NotificationUtils 类中,然后您可以在 MyFirebaseMessagingService 类中使用它。
    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 2018-02-07
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多