Search code examples
javaandroidandroid-push-notification

Firebase push notification activity is not opening when app is in background?


Actually I'm implementing firebasePushNotification service which is working wihtout any issue . Now when I'm receiving push_notification and my app is in foreground by clicking the notification intent is being passed to the desired activity without any error. But when the app is in background or not opened clicking on the notification is not sending the intent to the desired activity and even it's not showing any error . I tried to refer some questions in SO about adding exported=true option in manifest.xml but it couldn't solve my issue.

Code of NotificationManager Class:

public void displayNotification(String title, String body,String post_owner_username,String post_owner_time,
                                String notif_commenter_username,String notif_comment_time,String follower_username,String type) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(ctx, Constants.CHANNEL_ID)
                    .setSmallIcon(R.drawable.new_icon)
                    .setContentTitle(title)
                    .setContentText(body);

    Intent i = new Intent(ctx, ProfileHolder.class);
    if (type.equals("likes")) {
        Bundle b = new Bundle();
        b.putString("post_owner_username", post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("what", "show_notification_post");
        i.putExtras(b);
        i.putExtra("Open", "starred");
    }
    else if (type.equals("comments")) {
        Bundle b = new Bundle();
        b.putString("post_owner_username",post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("notif_commenter_username", notif_commenter_username);
        b.putString("notif_comment_time",notif_comment_time);
        b.putString("what", "show_notification_post");
        i.putExtras(b);
        i.putExtra("Open", "starred");
    }
    else if (type.equals("comment_likes")) {
        Bundle b = new Bundle();
        b.putString("post_owner_username", post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("notif_commenter_username", notif_commenter_username);
        b.putString("notif_comment_time", notif_comment_time);
        b.putString("what", "show_notification_post");
        i.putExtras(b);
        i.putExtra("Open", "starred");
    }
    else if (type.equals("follow")||type.equals("follow_request")) {
        Bundle b = new Bundle();
        b.putString("searchUsername", follower_username);
        i.putExtras(b);
        i.putExtra("Open", "search_profile");
    }
   // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_ONE_SHOT);

    /*
    *  Setting the pending intent to notification builder
    * */

    mBuilder.setContentIntent(pendingIntent);

    NotificationManager mNotifyMgr =
            (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);


    if (mNotifyMgr != null) {
        mBuilder.setAutoCancel(true);
        mNotifyMgr.notify(1, mBuilder.build());
    }
}

Solution

  • If you are using firebase push notification

    You need to send a click_action attribute ...to open the desired activity when app is in background.

    So try ..

     Intent i = new Intent(click_action);
    

    click_action is the intent-filter mapping to the desired activity. click_action is mandatory

    instead of the Explicit intent call - explicit intent call in the above case will only work when the app is in foreground..