1

I read the document for creating the notification. In that they have used TaskStackBuilder:

  1. to create seperate task for Activity.
  2. to add the parent of activity using addParentStack()
  3. to add an intent
  4. Eventually to create PendingIntent.

after that they didn't use StackBuilder Object to set in NotificationCompat.Builder object. They have used PendingIntent object.

Are all of the above informations (to create a seperate task, to identify the parent activity, to identify the intent) reside in 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());
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
saravanan
  • 5,339
  • 7
  • 45
  • 52

1 Answers1

0

When you use notifications in your app what happens is. You first specify an explicit intent(Normal intent). Then you create TaskStackBuilder object to access the activity you wan't to start when notification gets clicked then initialize PendingIntent reference with TaskStackBuilder getPendingIntent() and passes it to notification object.

Now what the PendingIntent do is get the intent from the TaskStackBuilder object using stackBuilder.getPendingIntent(int id, Flag) and passes pending object to notification object by calling notification.setContentIntent(PendingIntent object)

To recover mistakes in your code follow this:-

  1. First you should create explict Intent
  2. Then create TaskStackBuilder object then add the parentStack and nextIntent.
  3. Create PendingIntent object then get it's intent using stackBuilder.getPendingIntent(int id, Flag).
  4. Then pass it to Notification object using notification.setContentIntent(PendingIntent).

I am sure you will be done with this instruction...

Note:- Notification object does not accept Intent Object it requires PendingIntent object.