从 Activity 获取通知 Intent

get notification Intent from Activity

代码:

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());

}

在这种情况下,如何以及何时从 MainActivity 上的意图中获取 notificationIntentBlahHello

这个意图:

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

将前往或打开您的MainActivity.class。如果您以这种方式启动 Activity,则可以在 onCreate() 方法中调用以下内容:

Intent intent = getIntent();

或者您可以覆盖 onNewIntent() 方法:

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