1

I have a notifications system working that a service is scheculing then and the notifications are working fine.

I have some notifications that require user input/action.

Here is the code where I build my notification:

public int onStartCommand(Intent intent, int flag, int startId)
{
    super.onStartCommand(intent , flag, startId);

    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Context context = this.getApplicationContext();
    notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent mIntent = new Intent(this, MainActivity.class);
    pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN ) {           
        builder = new Notification.Builder(this)
                .setContentTitle(Utils.Title)
                .setContentText(Utils.Body)
                .setSmallIcon(R.drawable.ic_launcher)
                .addAction(0, Utils.button1Value, pendingIntent)
                .addAction(0, Utils.button2Value, pendingIntent)
                .setSound(uri)
                .setContentIntent(pendingIntent)
                .build();

    }

    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(Utils.NotificationID, builder);     

    return START_STICKY;

My question is: How do I get what button in the notification user clicked? In other words, how can I create some sort of button listener for those 2 actions in the notification?

Also, how can I stack my notifications so they don`t appear all separated? I have read on Google API that I need to use .setGroup, but that is not working. Can anyone share some example of notification stacking ?

********** Added *************

final static String GROUP_KEY = "myGroup"; // added String to identify the group

public int onStartCommand(Intent intent, int flag, int startId)
{
super.onStartCommand(intent , flag, startId);

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Context context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN ) {           
    builder = new Notification.Builder(this)
            .setContentTitle(Utils.Title)
            .setContentText(Utils.Body)
            .setSmallIcon(R.drawable.ic_launcher)
            .setGroup(GROUP_KEY)  // added group for Stacking
            .addAction(0, Utils.button1Value, pendingIntent)
            .addAction(0, Utils.button2Value, pendingIntent)
            .setSound(uri)
            .setContentIntent(pendingIntent)
            .build();

}

notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder);    // ID is always the same because the notifications are all stacking into one, tried with unique ID not working either

return START_STICKY;
Adrian Ivasku
  • 1,118
  • 3
  • 16
  • 31

1 Answers1

2
  1. For button listener problem:

Notification action will trigger that pendingIntent, so just put your custom intent into it. Each text should have a different pendingIntent so that you can distinguish it.

public int onStartCommand(Intent intent, int flag, int startId) {
    //...... 
    Intent mIntent1 = new Intent(this, MainActivity.class);
    mIntent1.setAction("clickAc1");
    PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, mIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent mIntent2 = new Intent(this, MainActivity.class);
    mIntent2.setAction("clickAc2");
    PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, mIntent2, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent it = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        builder = new Notification.Builder(this)
                .setContentTitle("Title")
                .setContentText("Text")
                .setSmallIcon(R.drawable.your_logo)
                .addAction(0, "ac1", pendingIntent1)
                .addAction(0, "ac2", pendingIntent2)
                .setSound(uri)
                .setContentIntent(pendingIntent)
                .build();

    }

and when user click the text ac1, then you can handle it in MainActivity(or other context you want.

//MainActivity
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d("probe","onNewIntent:"+intent.getAction());
    //response your click event by case intent.getAction(), 
    //it will be `clickAc1` or `clickAc2`, the string you write down in intent.setAction
}

If you use custom RemoteViews, you can follow this:

    Intent intent = new Intent("Your Custom intent string, like com.yourpackage.ClickButtonXXX");
    pendingIntent = PendingIntent.getService(getApplicationContext(),
        yourRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mRemoteViews.setOnClickPendingIntent(R.id.yourViewId, pendingIntent);

then your can response it with override Service.onStartCommand(Intent intent, int flags, int startId), case intent.getAction(); to distinguish different button.

  1. Stacking messages:

I found this in this notification history article:

.setGroup(GROUP_KEY_MESSAGES)  // added group for Stacking
.setGroupSummary(true)
.setContentTitle(count+" new notes")
.setStyle(new NotificationCompat.BigTextStyle().setSummaryText(count+" more"))
.build();

count++;

If you use Notification, change NotificationCompat.BigTextStyle() to Notification.BigTextStyle().

I think this may be what you want.

The roll-up notification cheats a little bit.

sakiM
  • 4,832
  • 5
  • 27
  • 33
  • Thanks for the answer, for the 2 question i`l investigate the link and your advice. For the 1 question, I don`t fully understand how to apply this to my example. Can you please apply it to my notification code ? – Adrian Ivasku Apr 13 '16 at 10:59
  • ummm, I used custom notification layout so implement in this way. Your `addAction(0, Utils.button1Value, pendingIntent)` won't work because the first parameter need icon resource id. Seems you need a [custom notification](http://stackoverflow.com/questions/27673943/notification-action-button-not-clickable-in-lock-screen) for future. – sakiM Apr 13 '16 at 13:28
  • Yea, I have added 0 for the icon because I don`t need an icon for my 2 actions, just the text and it is displaying perfect. I have seen the link with the RemoveViews&custom layout and probably I'l try that. But that is an alternative as I understand it. What is the "official" way with the setAction options? There must be because otherwise this option would not be supported. 2) For set grups I have followed the documentation and it is not working. I'l post the modified code in my updated question with setGroup for you to see it. As I have been reading, that documentation is for wearable devices – Adrian Ivasku Apr 13 '16 at 20:06
  • The first question I have found out the way, updated. But the second question if use system number style seems a little strange... – sakiM Apr 14 '16 at 03:24
  • You are the best! Button "listeners" for action work, tested Thank you! I will investigate the stacking now. – Adrian Ivasku Apr 14 '16 at 18:54