Adrian Ivasku
Adrian Ivasku

Reputation: 1118

Android notifications via service

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;

Upvotes: 1

Views: 501

Answers (1)

sakiM
sakiM

Reputation: 4932

  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.

Upvotes: 2

Related Questions