确定通知是否调用了Activity

dog*_*ose 7 notifications android android-pendingintent

我正在使用带有各种TabsActivitiy.从应用程序的不同部分创建通知,以告知用户某些内容已更改.当用户点击通知时,我现在设法调用活动.但是,如何在运行时或通过单击通知确定活动是以"正常"方式创建的?

(根据单击的通知,我想转发到另一个选项卡而不是显示主选项卡.)

Intent intent = new Intent(ctx, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);

        // TODO: Replace with .Build() for api >= 16
        Notification noti = new Notification.Builder(ctx)
                .setContentTitle("Notification"
                .setContentText(this.getName())
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pendingIntent)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_LIGHTS)
                .setAutoCancel(true)
                .getNotification();

        NotificationManager notificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Hide the notification after its selected
        notificationManager.notify(this.getId(), noti); 
Run Code Online (Sandbox Code Playgroud)

这成功调用了我的MainActivity.但是,当Activity被触发时,是否有一些方法被调用pendingIntent

想在主要活动中定义这样的东西:

onTriggeredByNotification(Notification noti){
     //determinte tab, depending on Notification.
}
Run Code Online (Sandbox Code Playgroud)

The*_*ard 20

从通知中传递布尔值,并在活动的onCreate方法中检查相同的值.

 Intent intent = new Intent(ctx, MainActivity.class);
 intent.putExtra("fromNotification", true);
Run Code Online (Sandbox Code Playgroud)

...

if (getIntent().getExtras() != null) {
  Bundle b = getIntent().getExtras();
  boolean cameFromNotification = b.getBoolean("fromNotification");
}
Run Code Online (Sandbox Code Playgroud)