이준호
이준호

Reputation: 25

android notification pendingintent onNewintent not called

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

Upvotes: 1

Views: 2339

Answers (1)

Mars
Mars

Reputation: 2572

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.

Upvotes: 2

Related Questions