Search code examples
androidandroid-fragmentsandroid-push-notification

Android - redirect a fragment in main activity from notification


I have a loginActivity and MainActivity with multiple fragments and one of them is called NotificationFragment.

Requirement: Assuming user is already logged in and is currently on Home Fragment (inside Main Activity) and app is in background. Now they receive a push notification, user should be redirected to Notification Fragment (on Main Activity).

As of now, Login Activity is receiving intent data from notification. Login Activity is our launch activity currently.

Question: How should Login Activity (on back stack) inform Main Activity to redirect user from Home Fragment to Notification Fragment?

Manifest looks like this:

<activity
        android:name="com.X.LoginActivity"
        android:configChanges="orientation"
        android:icon="@mipmap/ic_launcher"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>

            <!-- Sets the intent action to view the activity -->
            <action android:name="android.intent.action.VIEW" />
            <!-- Allows the link to be opened from a web browser -->
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Allows the deep link to be used without specifying the app name -->
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>
    <activity
        android:name="com.X.MainActivity"
        android:configChanges="orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustPan" />

Solution

  • Try this

     private void sendMyNotification(String title,String message) {
    
        //On click of notification it redirect to this Activity
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.mipmap.ic_appicon);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"Default")
                .setSmallIcon(R.drawable.ic_noti_new)
                .setLargeIcon(icon)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
    

    it will trigger the main activity.

    Add

    android:launchMode="singleTask" in your mainactivity to avoid running mutiple instance of MainActivity.

     <activity
            android:name=".MainActivity"
            android:launchMode="singleTask" />