Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a different activity other than MainActivity when clicking Firebase Notification

In my application i want open custom activity (not MainActivity) and putExtra to this activity when click on Firebase notification.
I write below codes, but when click on notification open MainActivity, But i want open my another activity (AuctionDetailActivity).

My NotificationManager class :

public class MyNotificationManager {

    private Context mCtx;
    private Uri soundUri;
    private static MyNotificationManager mInstance;

    public MyNotificationManager(Context context) {
        mCtx = context;
    }

    public static synchronized MyNotificationManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyNotificationManager(context);
        }
        return mInstance;
    }

    public void displayNotification(String title, String body) {

        soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent intent = new Intent(mCtx, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("fcm_notification", "Y");

        PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setSound(soundUri)
                .setAutoCancel(true)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setContentText(body)
                .setContentIntent(pendingIntent);

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

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

And MyFirebaseMessagingService class :

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        showNotify(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
    }

    private void showNotify(String title, String body) {
        MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
        //myNotificationManager.displayNotification(title, body);
        myNotificationManager.displayNotification(title, body);
    }
}

MainActivity codes:

@Override
protected void onResume() {
    super.onResume();
    String fcm_notification = getIntent().getStringExtra("fcm_notification");
    Log.d("FireBaseIntentLog", " FCM : " + fcm_notification);
    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            String value = getIntent().getExtras().getString(key);
            Log.d("FireBaseIntentLog", "Key: " + key + " Value: " + value + " FCM : " + fcm_notification);
        }
    }
}

How can i fix it?

like image 429
Jungle Avatar asked Oct 15 '25 18:10

Jungle


1 Answers

If you are sending the notification from Firebase console or inside the notification field using FCM API, the app behaves in two ways -

  • If your app is in foreground, the method onMessageReceived of your FCM service class will be called.
  • If your app is in background, nothing will happen inside your FCM service class. Rather, the notification will be handled internally by the FCM library itself and the notification with launcher activity in the intent will be shown.

And if you use FCM API to send notification and use the data field, the library does nothing itself and instead calls the method onMessageReceived regardless of whether your app is in foreground or background.

So in order to solve your issue, you can use one of the following two solutions:

  • Use FCM API to send notifications and use the data field instead of the notification field. Check the documentation to read more about FCM API.

  • In your launcher (main) activity, check for intent inside onCreate and if it is coming from notification, read the extras, finish the main activity and open your required activity.

Example for second case:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (checkIntent()) return;

    // other code.
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    checkIntent();
}

private boolean checkIntent() {
    // to receive the value, send the value as custom data from Firebase console.
    String value = getIntent().getStringExtra("your_key");

    if (value == null) return false;

    if (value.equals("something")) {
        // open one activity.

    } else if (value.equals("another_thing")) {
        // open another activity.
    }

    finish();
    return true;
}
like image 183
Nabin Bhandari Avatar answered Oct 17 '25 09:10

Nabin Bhandari