【问题标题】:Sending parameters from a notification to a different activity?将通知中的参数发送到不同的活动?
【发布时间】:2014-12-29 01:10:15
【问题描述】:

对于此处回答的问题,我有一个后续问题:如何将参数从通知点击发送到活动?

所以,我有 3 个活动,A、B 和 C。当用户进入活动 C 时,警报(计时器)启动。X 时间后,状态栏中会出现通知。以下是我启动通知的方式。 “MainActivity.class”是活动 A。但是,以这种方式启动通知会在用户关闭应用程序时在任何活动中重新启动应用程序。因此,如果他们在应用关闭时处于活动 C 并单击通知,则应用将恢复到活动 C。

Intent notificationIntent = new Intent( this, MainActivity.class );
notificationIntent.setAction( Intent.ACTION_MAIN );
notificationIntent.addCategory( Intent.CATEGORY_LAUNCHER );
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
notificationIntent.putExtra( NotifyService.NOTIFICATION_INTENT, true );

PendingIntent contentIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );

Notification notification = new Notification.Builder( this )
.setContentTitle( title )
.setContentText( text )
.setTicker( ticker )
.setSmallIcon( R.drawable.ic_launcher )
.setWhen( time )
.setContentIntent( contentIntent )
.setAutoCancel( true )
.setDefaults( Notification.DEFAULT_ALL )
.build();

我在活动 A、B 和 C 的清单中有这个:android:launchMode="singleTop"。我在每个活动中也有以下代码。

@Override
public void onNewIntent( Intent intent )
{
    super.onNewIntent( intent );

    Bundle extras = intent.getExtras();

    if( extras != null )
    {
        Boolean startedFromNotification = extras.getBoolean( NotifyService.NOTIFICATION_INTENT );

        if( startedFromNotification )
        {
            // Do something
        }
    }
}

现在,如果用户在关闭应用程序时处于活动 A 中,那么当他们单击通知时会在该活动中调用 onNewIntent(按预期工作)。但是,如果他们在活动 B 或 C 中,不仅不会调用这些活动中的 onNewIntent,而且我放在那里的额外内容 (NotifyService.NOTIFICATION_INTENT) 也会丢失。

当应用程序恢复到活动 B 或 C 时,是否有将通知意图附加信息传递给应用程序?

【问题讨论】:

    标签: android android-intent notifications bundle lifecycle


    【解决方案1】:

    我通过这个问题得到了答案:FLAG_ACTIVITY_NEW_TASK 在 PendingIntent 中使用时未按预期运行

    首先,我改变了这一行:

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

    收件人:

    Intent notificationIntent = new Intent( this, NotificationActivity.class );
    

    我将 NotificationActivity.class 设为占位符活动,当应用程序从通知中恢复时,它将通知其他活动。这些代码行已被删除:

    notificationIntent.setAction( Intent.ACTION_MAIN );
    notificationIntent.addCategory( Intent.CATEGORY_LAUNCHER );
    notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
    notificationIntent.putExtra( NotifyService.NOTIFICATION_INTENT, true );
    

    还有,这一行:

    PendingIntent contentIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );
    

    改为:

    PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT );
    

    在 NotificationActivity 的 onCreate() 方法中,我在底部添加了这几行:

    STARTED_FROM_NOTIFICATION = true;
    finish();
    

    STARTED_FROM_NOTIFICATION 位于公共源文件中。然后,在 onStart() 方法中的每个活动中:

    if( STARTED_FROM_NOTIFICATION == true )
    {
        STARTED_FROM_NOTIFICATION = false;
        // Do something
    }
    

    我还从所有活动中删除了 android:launchMode="singleTop"。

    使用此方法,NotificationActivity 将被放置在现有活动堆栈的顶部,但会在显示之前完成。完成后,将调用其下方活动的 onStart() 并按需要进行操作。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多