How to open specific activity from push notification in Android&FCM?

Viewed 2326

I receive a notification about a new message in chat using FCM while the application is closed. When I click on it, I want to open chat activity, but main activity opens.

Clicking on the push notification will open main activity. In it, I can get a type of notification from intent and open another activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    processNotification()
}

private fun processNotification() {
    val notificationCategory = intent?.extras?.getString("category") ?: return
    if (notificationCategory == "new_message") {
        startMessagingActivity()
        finish()
    }
}

This method seems to me dirty and not convenient. Is it possible to specify the activity that will open when you click on the push-notification?

EDIT: Steps to solve:

  1. Add a intent-filter to the activity you want to open:

    <activity android:name=".messages.chatroom.ChatRoomActivity">
        <intent-filter>
            <action android:name="*your-action-name*"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    
  2. Add 'click_action="your-action-name"' field to your FCM message.

Android will automatically open activity with action same as click_action

2 Answers

You will need to use Android Deep linking.

Your notification (FCM) should have a payload of some data (Message ID all depends on your design) for the given screen you want to consume in this case your chat Activity.

The chat Activity will need to have a host and schema declared in the manifest, below is the instructions and code for Deep Linking:

https://developer.android.com/training/app-links/deep-linking

Happy coding.

First of all you should have specific payload with your notification in your case it should be an ID for specific chat then you can use the ID to do what you want to do use this code

 type = remoteMessage.getData().get("type");
if(type.equals("1")) { // here you can just detremine the activity you want to open
              Intent intent;
              PendingIntent pendingIntent;
              NotificationCompat.Builder builder;
              if (notifManager == null) {
                  notifManager = (NotificationManager) getSystemService
                          (Context.NOTIFICATION_SERVICE);
              }

              intent = new Intent (this, Admin_page.class); // here chose the activity
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                  int importance = NotificationManager.IMPORTANCE_HIGH;
                  if (mChannel == null) {
                      NotificationChannel mChannel = new NotificationChannel
                              ("0", title, importance);
                      mChannel.setDescription (body);
                      mChannel.enableVibration (true);
                      mChannel.setVibrationPattern (new long[]
                              {100, 200, 300, 400, 500, 400, 300, 200, 400});
                      notifManager.createNotificationChannel (mChannel);
                  }
                  builder = new NotificationCompat.Builder (this, "0");

                  intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP |
                          Intent.FLAG_ACTIVITY_SINGLE_TOP);
                  pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
                  builder.setContentTitle (title)  // flare_icon_30
                          .setSmallIcon(R.drawable.logo_app).setTicker(title).setWhen(0)
                          .setContentText (body)  // required
                          .setDefaults (Notification.DEFAULT_ALL)
                          .setAutoCancel (true)
                          .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.logo_app))
                          .setContentIntent (pendingIntent)
                          .setSound (RingtoneManager.getDefaultUri
                                  (RingtoneManager.TYPE_NOTIFICATION))
                          .setVibrate (new long[]{100, 200, 300, 400,
                                  500, 400, 300, 200, 400});
              } else {

                  builder = new NotificationCompat.Builder (this);

                  intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP |
                          Intent.FLAG_ACTIVITY_SINGLE_TOP);
                  pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
                  builder.setContentTitle (title)
                          .setSmallIcon(R.drawable.logo_app).setTicker(title).setWhen(0)
                          .setContentText (body)  // required
                          .setDefaults (Notification.DEFAULT_ALL)
                          .setAutoCancel (true)
                          .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.logo_app))

                          .setContentIntent (pendingIntent)
                          .setSound (RingtoneManager.getDefaultUri
                                  (RingtoneManager.TYPE_NOTIFICATION))
                          .setVibrate (new long[]{100, 200, 300, 400, 500,
                                  400, 300, 200, 400})
                          .setPriority (Notification.PRIORITY_HIGH);
              } // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              Notification notification = builder.build ();
              notifManager.notify((int)date.getTime(), notification);
          }

hope that work for you

Related