Question

I'm using Google Play Services lib for GCM and all works great. Now I implement 2 buttons (actions) in my notificationCompat.Builder: - One open app. - Other one delete some unread messages: for this operation I post some dato to server.

How can I launch a post to my server on notification action click without open any activity in front of user?

Thank you very much!!!!!

Here my code:`Intent openIntent = new Intent(); Intent cancelIntent = new Intent();

    if (has1Id) {
        //go details!
        openIntent = new Intent(this, NotificationActivity.class);
        openIntent.putExtra("id", ids.get(0));
        //cancell intent: start async operation in background
        cancelIntent = new Intent(this, HomeActivity.class);
        cancelIntent.putExtra("id", ids.get(0));
    }else{
        //go home!
        openIntent = new Intent(this, HomeActivity.class);
        //cancell intent: start async operation in background
        cancelIntent = new Intent(this, HomeActivity.class);
        cancelIntent.putExtra("id", ids);
    }

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            openIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent cancelNotificationIntent = PendingIntent.getActivity(this, 0,
            cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    long[] vibrationPattern = {0, 200, 800, 200, 100, 500};
    Uri soundUri = Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.jingle1);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.ic_notification)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
    .setContentTitle(title) //set title
    .setContentInfo(numNotRead) //number of unread content
    .setAutoCancel(true)
    .setContentText(msgs.get(0)+"...")
    .setVibrate(vibrationPattern)
    .setSound(soundUri)
    .setLights(Color.rgb(255, 165, 00), 800, 800)
    .setContentIntent(contentIntent).addAction(R.drawable.ic_notification, getString(R.string.open_app), contentIntent)
    .setContentIntent(cancelNotificationIntent).addAction(android.R.drawable.ic_delete, getString(R.string.mark_read), cancelNotificationIntent);

    if (has1Id) {
        NotificationCompat.BigTextStyle bigTextStyle =new NotificationCompat.BigTextStyle().bigText(msgs.get(0));
        mBuilder.setContentTitle(getString(R.string.notification_new_content));
        //get only 1 long text: the description of content
        mBuilder.setStyle(bigTextStyle);
    }else{
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle(getString(R.string.notification_new_contents));
        for (int i=0; i < msgs.size(); i++) {
            inboxStyle.addLine(msgs.get(i));
        }
        mBuilder.setStyle(inboxStyle);

    }

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());`
Was it helpful?

Solution

You should use another class as the intent receiver for cancelIntent, like a BroadcastReceiver and handle the intent there with no UI.

public class MyIntentReceiver extends BroadcastReceiver {    
  @Override 
  public void onReceive(Context _context, Intent _intent) {
    if (_intent.getAction().equals(MY_INTENT)) {
      // TODO Handle a notification
    }
  }    
}

You also can use a Activity with no UI as the intent, but I think previous option is better.

More info: Android Activity with no GUI

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top