3

I m currently looking into android notification. Is there any way to show all the notification from an application as a single one with the number in android notifications window. Clicking on this will redirect the user to a view in the application with separate notifications. Also Is there any way to set the activity for each notifications so that the user will be redirected to the appropriate activity based on the notification clicked.

Dijo David
  • 6,175
  • 11
  • 35
  • 46

1 Answers1

6

You could attach a custom layout to your notification, which contains a counter. And when event occurs increase the counter and update the notification. Similar like in example below:

   Notification notification = new Notification(R.drawable.logo, name,    System.currentTimeMillis());

   RemoteViews contentView = new RemoteViews(appContext.getPackageName(), R.layout.custom_layout );

   contentView.setTextViewText(R.id.notification_text, Counter);

notification.contentView = contentView;

To attach an activity to the notification:

        Intent notificationIntent = new Intent(appContext, MyActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(...);
        notificationIntent.putExtras( bundle );
        notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);           

        PendingIntent contentIntent = PendingIntent.getActivity( appContext, (int)System.currentTimeMillis(), notificationIntent, 0 );
        notification.contentIntent = contentIntent;

Hope it will help.

Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236
ackio
  • 662
  • 1
  • 5
  • 12
  • 1
    Thanks ackio. I will check this. It will be more good if there is any default feature by android. I have gone through this :http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating They have not explained about updating the notification. – Dijo David Mar 17 '11 at 12:44
  • 1
    Updating simply means to call NotificationManager::notify with the same ID. If ID's are different the new notification will be added otherwise the existing notification is updated. – ackio Mar 18 '11 at 04:27
  • In that article, I've read this line "the Messaging application updates the existing notification to display the total number of new messages received". So I believe its possible. – Dijo David Mar 18 '11 at 06:51
  • Atlast I have decided to go with a counter for the notification. Last 5 notifications will be available in the notification manager. Thanks for helping me. – Dijo David Apr 12 '11 at 06:11