David Foie Gras
David Foie Gras

Reputation: 2080

get notification Intent from Activity

CODE:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;


Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("notification", "notificationIntentBlahHello");

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelID);

    notificationBuilder
            .setSmallIcon(R.drawable.ic_add_post)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setVibrate(new long[]{1000, 1000})
            .setLights(Color.BLUE, 1,1)
            .setPriority(NotificationManager.IMPORTANCE_HIGH)
            .setContentIntent(notifyPendingIntent);


    notificationManager.notify("do_not1", (int) (Math.random()*10000000)/* ID of notification */, notificationBuilder.build());

}

In this case, How and when to get notificationIntentBlahHello from intent on MainActivity??

Upvotes: 0

Views: 60

Answers (1)

Marton
Marton

Reputation: 121

This intent:

Intent notifyIntent = new Intent(this, MainActivity.class);

Will go to or open your MainActivity.class. If you start your Activity this way, you can call the following in the onCreate() method:

Intent intent = getIntent();

Or you can override the onNewIntent() method:

@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent); // Propagate or do something else with it
}

Upvotes: 1

Related Questions