4

I know this question seems duplicate, but for my requirement, I have searched many posts online, but nothing worked for me.

My requirement

I'm using the Firebase to get the push notifications. When app was opened means everything working fine but my problem is, app is in background/closed if any push notification came means i want to open the particular activity or fragment when clicking on that notification, but it always opening the launcher/main activity.

For this, I have tried the following scenarios

Scenario 1

Used the bundle to transfer notification data from FirebaseMessagingService to the launcher/Main activity and based on the bundle data i'm redirecting to the particular activity/fragment

Scenario 2

By using Sharedpreference

Scenario 3

By using intent.putExtra

following the above scenarios everything working fine when app was opened but if my app was closed means it always redirecting to the main/launcher activity

My code

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("reqTo", messageBody);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0, notificationBuilder.build());

So, does anyone know how to open the particular activity/Fragment when clicking on the Firebase push notification when the app is closed.

AL.
  • 36,815
  • 10
  • 142
  • 281
Bahu
  • 1,516
  • 2
  • 28
  • 49

2 Answers2

2
public void showNotificationMessage(final String title, final String message, Intent intent) {
    // Check for empty push message
    if (TextUtils.isEmpty(message))
        return;


// notification icon
final int icon = R.drawable.logo;

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                mContext,
                0,
                intent,
                PendingIntent.FLAG_CANCEL_CURRENT
        );

final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        mContext);

final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + mContext.getPackageName() + "/raw/notification");


    showSmallNotification(mBuilder, icon, title, message, resultPendingIntent, alarmSound);
    playNotificationSound();

}



private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {

            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            inboxStyle.addLine(message);

            Notification notification;
            notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setContentIntent(resultPendingIntent)
                    .setSound(alarmSound)
                    .setStyle(inboxStyle)
                    .setSmallIcon(R.drawable.logo)
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                    .setContentText(message)
                    .build();

            NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(Config.NOTIFICATION_ID, notification);
        }


private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
        bigPictureStyle.setBigContentTitle(title);
        bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
        bigPictureStyle.bigPicture(bitmap);
        Notification notification;
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setSound(alarmSound)
                .setStyle(bigPictureStyle)
                .setSmallIcon(R.drawable.logo)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                .setContentText(message)
                .build();

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);
    }

You can check app closed or not like this

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
  • hope it helps. you have to pass intent in this function – Zaki Pathan Feb 16 '17 at 08:48
  • 1
    Bro i'm facing same problem :-( – Bahu Feb 16 '17 at 10:08
  • I think we have to go in chatting – Zaki Pathan Feb 16 '17 at 10:10
  • When app is in foreground everything fine but i'm not able to open the particular activity/fragment when app is closed or in background. This is the code i'm using https://www.dropbox.com/s/6rphjkh6k6w27u8/FireBase.zip?dl=0. – Bahu Feb 16 '17 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135843/discussion-between-zaki-pathan-and-bahu). – Zaki Pathan Feb 16 '17 at 10:25
  • @ZakiPathan if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) doesn't work any more cause, the GET TASK permission is deprecated, or is there any other way around? – Dilip Poudel Oct 12 '17 at 10:56
1

This is very similar to my answer given here: https://stackoverflow.com/a/41441631/1291714

In summary though, you need to use Firebase Cloud Messaging and not Firebase Notifications in order to receive a message in the background and do custom processing. You need to send "data" messages to the client and then in your service, you will then always receive the callback, whether the app is in the foreground or background.

With Firebase Notifications, you will only receive the callback when the app is in the Foreground. When the app is in the background, the system will handle and display the notification for a user. You won't be able to customise this notification to open up a different intent.

Read more here: https://firebase.google.com/docs/notifications/android/console-audience

Community
  • 1
  • 1
riggaroo
  • 2,584
  • 20
  • 34