1

I want to call onNewIntent() method when I press fcm notification.

I read some Q&A about this, and I think i made my code simple.

but it doesn't work. I don't know what is wrong :(

here is my code.

FCM :

private void sendPushNotification(String message) {

    System.out.println("received message :" + message);
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

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

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.untitled)
            .setContentTitle("This is title")
            .setContentText(message)
            .setPriority(Notification.PRIORITY_MAX)
            .setVibrate(new long[]{0,500})
            .setContentIntent(pendingIntent);


    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wakelock.acquire(5000);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

onNewIntent() :

@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    setIntent(intent);
    Bundle extras = intent.getExtras();
    if(extras != null){

          Toast.makeText(getApplicationContext(),"Hello!",Toast.LENGTH_LONG).show();

    }
}

AND MANIFEST :

    <activity android:name=".MainActivity"
        android:launchMode="singleTop">

And there are only two error in logcat :

07-25 18:38:18.216 25648-25648/? E/Zygote: v2

07-25 18:38:18.221 25648-25648/? E/Zygote: accessInfo : 0

이준호
  • 25
  • 7
  • Try this.... Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); startActivity(intent); – Mohd Saquib Jul 25 '17 at 09:09
  • Thanks for comment! but that doesn't work for me T.T – 이준호 Jul 25 '17 at 09:17
  • post a screenshot of your logcat error please – Nenco Jul 25 '17 at 09:30
  • thanks for reading. i post error in logcat since I started app until active Mainactivity by pressing noti. – 이준호 Jul 25 '17 at 09:45
  • Use FLAG_ACTIVITY_REORDER_TO_FRONT , please read this doc.. https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT – Mohd Saquib Jul 25 '17 at 09:52
  • Thank you! That link makes me understand about what I studying. But it doesn't work to me. Thanks for comment – 이준호 Jul 25 '17 at 12:13
  • What happens when you select the `Notification`? You can add logging to all the lifecycle methods like `onCreate()`, `onResume()`, `onPause()`, etc. to see what happens when you click the `Notification`. – David Wasser Aug 17 '17 at 07:26

1 Answers1

2

Reposting a solution I found here: click on notification to go current activity

Add these lines:

intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

I have no idea why they must be set, but if you don't set them onNewIntent will not be called.

Mars
  • 2,505
  • 17
  • 26