0

I am trying to modify existing notifications in android.

What I have in my app
When a notification is already in system tray and another notification appears, the second one overwrites the first notification content.

What I am looking for

If second Notification arrives then instead of overwriting the first I need to change title to show 2 New Messages and go on incrementing as notifications arrive.

What it should look like

Code Implemented

      Bitmap icon = BitmapFactory.decodeResource(ctx.getResources(),
                R.drawable.icon);
        Intent launchActivity = new Intent(ctx, CordovaApp.class);

        launchActivity.putExtra("heading",newsHeader);
        launchActivity.putExtra("content",newsText);
        PendingIntent pi = PendingIntent.getActivity(ctx,0, launchActivity, PendingIntent.FLAG_NO_CREATE);
        ParseAnalytics.trackAppOpened(launchActivity);
        if(pi==null){
            Log.d(TAG, "Pending Intenet is null.");
        }else{
            Log.d(TAG, "Pending Intenet is not null.");
        }

        Notification noti = new NotificationCompat.Builder(ctx)
        .setContentTitle(newsHeader)
        .setContentText(newsText)
        .setSmallIcon(R.drawable.icon)
        .setLargeIcon(icon)
        .setContentIntent(pi)
        .setAutoCancel(true)
        .build();

        NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0, noti);

Update

I implemented the solution mentioned below by @yogendra and now I am getting two separate notifications. Instead of getting stacked. Below is updated code

Notification noti = new NotificationCompat.Builder(ctx)
            .setContentTitle(newsHeader)
            .setContentText(newsText)
            .setSmallIcon(R.drawable.icon)
            .setGroup(GROUP_KEY_EMAILS)
            .setLargeIcon(icon)
            .setContentIntent(pi)
            .setLights(Color.parseColor("green"), 5000, 5000)
            .setAutoCancel(true)
            .setPriority(2)
            .setTicker("Notification from App")
            .setGroupSummary(true)
            .build();
            NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
            int timeSeconds = (int)System.currentTimeMillis()%Integer.MAX_VALUE;
            Log.i(TAG,"Timing function called "+timeSeconds);
            nm.notify(timeSeconds, noti);
Incpetor
  • 1,293
  • 6
  • 25
  • 46
  • you should change the `ID` for that `nm.notify(count++, noti);` – M D Mar 24 '15 at 11:01
  • Thanks @MD..can you please post some relevant code. Also, how do I know if notification is already present. So, that I can print the custom message instead of Actual title. – Incpetor Mar 24 '15 at 11:12
  • See the Mr. Smith answer's and i will give you the idea. – M D Mar 24 '15 at 11:13

2 Answers2

0

See your code

 nm.notify(0, noti);

where

notify(int id, Notification notification)

Here 0 is the ID of notification which is to be managed with respect to each notification. If you want to show a different notification your notification id should be unique each time. If you try to post a notification with the same notification id your previously displayed notification will be replaced with the latest notification.

Solution

Now you need to display a custom notification with a custom layout and update the counter each time .

Source code to create a Custom Notification.

Community
  • 1
  • 1
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

create global variable in your class :

private int count = 0;
    private ArrayList<String> notificationList = new ArrayList<String>();
    private String GROUP_KEY_EMAILS = "email";

/call method createNotification when you need to create notification and pass message what you need to show on message./

private void createNotification(String notificationMassage) {


    notificationList.add(notificationMassage);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);

    // Create builder
    Builder summaryNotification = new NotificationCompat.Builder(this)
            .setContentTitle(notificationList.size()+" new messages")
            .setSmallIcon(R.drawable.settings)
            .setLargeIcon(largeIcon)
            .setGroup(GROUP_KEY_EMAILS)
            .setGroupSummary(true)
            .setAutoCancel(true);




    // Create style 
    InboxStyle nStyle = new NotificationCompat.InboxStyle();
    nStyle.setBigContentTitle(notificationList.size()+" new messages");
    nStyle.setSummaryText("Summery Text...<you can set as blank>");

    for (String Str : notificationList) {
        nStyle.addLine(Str);
    }
    summaryNotification.setStyle(nStyle);
    mNotificationManager.notify(0, summaryNotification.build());
    count++;
}

/** please clear notification array list after tap on notification.
*/

For more detail please refer below link:

https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup

https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup

Yogendra
  • 4,817
  • 1
  • 28
  • 21