Skip to content Skip to sidebar Skip to footer

App Is Reload If Click Firebase Notification When App Is In Background

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

Solution 1:

You need to pass the required activity as intent, not the MainActivity

Solution 2:

Give activity name to pending intent ....

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

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

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

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

Solution 3:

See below code. I am using that and it is opening my HomeActivity.

NotificationManagernotificationManager= (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
Notificationnotification=newNotification(icon, message, when);
//change HomeActivity.class which activity you want to open after clicking notificationIntentnotificationIntent=newIntent(context, HomeActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntentintent= PendingIntent.getActivity(context, 0,
        notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

change HomeActivity.class which activity you want to open after clicking notification.. hope it will work...

Solution 4:

/**
 * Method checks if the app is in background or not
 */publicstatic 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;
}

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.

privatevoidhandleNotification(String message) {
    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
        // app is in foreground, broadcast the push messageIntentpushNotification=newIntent(Config.PUSH_NOTIFICATION);
        pushNotification.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

        // play notification soundNotificationUtilsnotificationUtils=newNotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();
    }else{
        // If the app is in background, firebase itself handles the notification
    }
}

I hope it helps you.

Post a Comment for "App Is Reload If Click Firebase Notification When App Is In Background"