简体   繁体   中英

Action button from notification bar not working Android

I'm creating a push notification on a service:

AppointmentService.java

Intent intent = new Intent(AppointmentService.this, ReAppointmentService.class);

intent.putExtra("id", id);

PendingIntent pIntent = PendingIntent.getService(AppointmentService.this, 0, intent, 0);

Notification n = new NotificationCompat.Builder(AppointmentService.this)
    .setContentTitle((String) snapshot.child("name").getValue())
    .setContentText("Hey!")
    .setSmallIcon(R.drawable.ic_launcher)
    .setAutoCancel(true)
    .addAction(R.drawable.ic_action_reply_light, "Hey yo!", pIntent)
    .build();

NotificationManager notificationManager = 
        (NotificationManager) HeyService.this.getSystemService(Activity.NOTIFICATION_SERVICE);

notificationManager.notify(getNotificationID(id), n);

ReAppointmentService.java @Override public IBinder onBind(Intent arg0) { return null; }

@Override 
public int onStartCommand(Intent intent, int flags, int startId)
{
    Bundle extras = intent.getExtras();

    String id = extras.getString("id");

    gc = new GlobalContents(this);

    Firebase send = new Firebase("https://appointmentapp.firebaseio.com/app/"+id);

    Firebase ref = send.push();

    ref.runTransaction(new Transaction.Handler()
    {
          @Override
          public Transaction.Result doTransaction(MutableData ref)
          {
              ref.child("date").setValue(String.valueOf((new GregorianCalendar()).getTimeInMillis()));
              ref.child("name").setValue(gc.getName());
              ref.child("from").setValue(gc.getID());
              ref.child("seen").setValue(false);

              return Transaction.success(ref);
          }

          @Override
          public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) {

          }
    });

    return START_NOT_STICKY; 
} 

The notification shows normally with the action button. But, when I click on the button, nothing happens (I've tried to put some logs on ReAppointmentService and it didn't work).

What is the problem?

The last parameter to PendingIntent.getService should almost always be FLAG_UPDATE_CURRENT - otherwise your PendingIntent will not be updated with new extras as per the documentation on PendingIntent:

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

  val intent = Intent(this, NotificationClickActivity::class.java)
    intent.apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent = PendingIntent.getActivity(this,0,intent,0)

 val notificationBuilder = NotificationCompat.Builder(this,channelId)
                .setSmallIcon(R.drawable.ic_notifications)
                .setContentTitle("Title: API LEVEL " + Build.VERSION.SDK_INT)
                .setContentText("UUID: " + UUID.randomUUID())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.ic_notifications,"Do Task",buttonPendingIntent)

        with(NotificationManagerCompat.from(this)){
            notify(1, notificationBuilder.build())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM