我閱讀了用於創建通知的文檔。因爲他們已經使用TaskStackBuilder
:PendingIntent for TaskStackBuilder
- 爲活動創建單獨的任務。
- 添加活動的使用addParentStack父()
- 添加一個意圖
- 最終創建的PendingIntent。
之後他們沒有使用StackBuilder
對象設置在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());