简体   繁体   中英

Notification PendingIntent hard bug (or really simple one)

I'm trying to debug this for ages now and I just can't seem to find the problem:

I have a broadcast receiver, which receives the broadcast successfully. The notification has two actions ("buttons"):

firstIntent = null; 
secondIntent = null;

firstPendingIntent = null; //first "button" to join with the first intent
secondPendingIntent = null; //second "button" to join with the second intent

if(boolean){

//relevant

firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_that");

}else{

firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_that");

}

firstPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT);

secondPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, secondIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notification = new NotificationCompat.Builder(getApplicationContext())
                                    .setContentTitle(notification_title)
                                    .setContentText(notification_text)
                                    .setTicker("Notification!")
                                    .setWhen(System.currentTimeMillis())
                                    .setDefaults(Notification.DEFAULT_SOUND)
.addAction(R.drawable.abc_cab_background_top_holo_light, first_option, firstPendingIntent)
.addAction(R.drawable.abc_cab_background_top_holo_dark, second_option, secondPendingIntent)
                                    .setAutoCancel(true)
                                    .setSmallIcon(R.drawable.ic_stat_name)
                                    .build();

Whenever I debug in the broadcastReceiver , for some reason, action from extras always logs "do_that", even if I click the first or second button of the notification. Any reason for this? I cant really seem to understand why.

public class NotificationFunctions extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean feedback;

        String action = intent.getExtras().getString("action");

        Log.wtf("...", action); //logs do_that

    }}

Any reason for this?

Because firstPendingIntent == secondPendingIntent .

If there already is a PendingIntent matching your request, getBroadcast() returns the existing PendingIntent . FLAG_UPDATE_CURRENT says to replace the extras in the Intent wrapped inside the PendingIntent .

In one of your two getBroadcast() calls, replace 0 with some other number, to get distinct PendingIntent objects.

Also, I recommend that you replace getBaseContext() and getApplicationContext() with this . Only use getBaseContext() and getApplicationContext() when you know precisely why you are using them.

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