1

我阅读了创建通知的文档。因为他们使用了TaskStackBuilder

  1. 为活动创建单独的任务。
  2. 使用 addParentStack() 添加活动的父级
  3. 添加意图
  4. 最终创建PendingIntent。

之后他们没有使用StackBuilderObject 来设置NotificationCompat.Builder对象。他们使用了PendingIntent对象。

上述所有信息(创建单独的任务、识别父活动、识别意图)是否都驻留在 PendingInent 中?

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
4

1 回答 1

0

当您在应用程序中使用通知时,会发生什么。您首先指定一个明确的意图(正常意图)。然后创建TaskStackBuilder对象以访问单击通知时不想启动的活动,然后使用TaskStackBuilder getPendingIntent () 初始化PendingIntent引用并将其传递给通知对象。

现在PendingIntent所做的是使用stackBuilder.getPendingIntent(int id, Flag)从TaskStackBuilder对象获取意图,并通过调用notification.setContentIntent(PendingIntent object)将挂起的对象传递给通知对象

要恢复代码中的错误,请执行以下操作:-

  1. 首先你应该创建明确的意图
  2. 然后创建TaskStackBuilder对象,然后添加parentStacknextIntent
  3. 创建PendingIntent对象,然后使用stackBuilder.getPendingIntent(int id, Flag)获取它的意图。
  4. 然后使用notification.setContentIntent(PendingIntent)将其传递给 Notification 对象。

我相信你会完成这个指令......

注意:- Notification 对象不接受Intent 对象,它需要PendingIntent 对象

于 2017-08-10T08:04:53.977 回答