1

When the user drags down all notifications and then tap on my app notification, then the app opens.

However, if a fresh notification appears on top on device screen and the user taps it, it does not open.

What could be the issue?

        Intent intent = Activity1.createIntent(app.getApplicationContext());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Activity2.createIntent(app.getApplicationContext(), new ArrayList<>(participants));
PendingIntent pendingIntent =
  PendingIntent.getActivity(app.getApplicationContext(), 1000, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Bitmap bm = BitmapFactory.decodeResource(app.getResources(), R.mipmap.appiconmain);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(app.getApplicationContext())
                .setSmallIcon(R.drawable.new_item_arrival, Notification.PRIORITY_HIGH)
                .setLargeIcon(bm)
                .setContentTitle(displayNotificationData.getTitle())
                .setContentText(displayNotificationData.getSubTitle())
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
GJain
  • 5,025
  • 6
  • 48
  • 82
  • Can you provide some details on your push notification implementation? Are you using gcm or fcm? Have you overridden `FirebaseMessagingService.onMessageReceived`? Are you sending standard "notification messages" or custom "data messages? – Abtin Gramian Oct 17 '16 at 01:26
  • @AbtinGramian I do not override FirebaseMessagingService. – GJain Oct 17 '16 at 02:08

2 Answers2

1

You need to look at overriding Activity's onNewIntent(Intent intent) event handler, on this method you should be able to perform whatever tasks you need

Additionally, according to the official docs you can still get the original intent through via getIntent(), therefore still inside onNewIntent() you should consider using setIntent (Intent newIntent) so you can use this new intent anywhere in the activity

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Take a look at this quote from a precise and concise answer:

For managing if the activity is already running you have two ways:

  • Add FLAG_ACTIVITY_SINGLE_TOP flag to the Intent when launching the activity, and then in the activity class implement onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.

  • Same as number one, but instead of adding a flag to the Intent you must add "singleTop" in your activity AndroidManifest.xml.

If you use intent extras, remeber to call PendingIntent.getActivity() with the flag PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every notification.

I hope this helps

Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
1

Use this :-

PendingIntent pendingIntent = PendingIntent.getActivity(app.getApplicationContext(), 1000, intent, PendingIntent.FLAG_UPDATE_CURRENT);

and overrride onNewIntent method in your MainActivity.

LoveAndroid
  • 186
  • 1
  • 11