Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data with a PendingIntent

I am attempting to raise a notification that a message has arrived. I have added an action expecting an icon (smallredball) to show on the notification. I expect that if the user hits smallredball, main activity will startup and the activity, checking the extras bundle, will see the orders to do something different than if it were just started up normally.

The notification shows on the target phone (running KitKat) along with the text but the smallredball icon never shows. When the user touches the notification the activity executes with no extra. EDIT: THE ACTIVITY IS NOW GETTING THE EXTRA BUNDLE.

This is the code sending the notification:

private void raiseNotification( String username, String mesText) 
{
    DebugLog.debugLog("GCMIntentService: Attempting to Raise Notification ", false);
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("whattodo", "showmessage");
    intent.setAction(Long.toString(System.currentTimeMillis())); //just to make it unique from the next one
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);


    b.setContentTitle("New SafeTalk Message")
    .setSmallIcon(R.drawable.note24x24)
    .setContentText("From " + username + "  " + mesText)
    .setTicker("New SafeTalk Message")
    .setContentIntent(pIntent)
    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
    .setAutoCancel(true)
    .addAction(R.drawable.smallredball, "Read Now", pIntent);

     NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
     mgr.notify(0, b.build());      
}

this is a code snippet from the activity:

        Bundle extras = getIntent().getExtras();
        if (extras == null)
        {
            GlobalStuff.mpBad.start();
        }
        else
        {
            String myOrders =   extras.getString("whattodo");

        if (myOrders.equals("showmessage"))
            GlobalStuff.mpBeep.start();

        }

Why isn't the icon showing in the notification? Since I setAutoCancel to true, I expected that simply touching the notification would make it just go away. But instead it runs the app providing no extra bundle? Thanks, Dean

like image 507
Dean Blakely Avatar asked Aug 07 '14 00:08

Dean Blakely


1 Answers

This topic is covered in an existing question

Since the points that will solve this problem and similar problems I have had, are spread around a bit in that topic, here is my two point cheat sheet:

Point 1: use code like the following to create a pending intent. The choice of flags in the last argument is important:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Point 2: pending intents are stored in a global system table, and only certain parts of the intent they are created from are part of the "key" that is used to look things up in this table. Extras are not part of the key, so if you want two intents to map to two different pending intents, make sure they are different in some other way, for example having different actions, data, or types.

This example changes the action:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("whattodo", "showmessage");
// add this:
intent.setAction("showmessage");

(The action can be anything as long as it is different than what you use with the same class elsewhere. )

There is a good explanation in the latest version of the Javadoc for pending intents., especially this quote I pulled out:

... 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.

like image 116
x-code Avatar answered Oct 01 '22 17:10

x-code