重新加载应用程序如果在后台运行时单击Firebase通知 [英] App is reload If click firebase notification when app is in background

查看:59
本文介绍了重新加载应用程序如果在后台运行时单击Firebase通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When app is running and notification is alert if I click it ,notification is go to there (onMessageReceived is running)

但是当应用程序在后台并且如果我单击它时通知警报,应用程序将重新加载(首先是启动屏幕,如启动屏幕),而不是去那里并且 onMessageReceived 未运行

but when app is in background and notification is alert if I click it, App is reload again (at first such as splash screen) ,not go to there and onMessageReceived is not running

如何解决?

ps.抱歉,我的英语不好.

ps. sorry my english is not good.

推荐答案

/**
 * 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类中可以使用此方法处理消息和处理通知.

You can check app is in the background or not and then in MyFirebaseMessagingService class you can use this method in for handle message and handle the notification.

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
    }
}

希望对您有帮助.

这篇关于重新加载应用程序如果在后台运行时单击Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆