user2891491
user2891491

Reputation: 361

Android put put extra on notification intent

I'm having troubles with putExtras on my service class. First of all, I'm having a service, that receives new messages and creates a new notification

MyService.class

public void showMessage(String jsonStr)
{



        Intent notiIntent = new Intent(App.getContext(),MainActivity.class);
        notiIntent.putExtra("message-info", "info text");

        notiIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pintent = PendingIntent.getActivity(App.getContext(), 0, notiIntent,0);

NotificationCompat.Builder notificationBuilder =
                        new NotificationCompat.Builder(this)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(pintent);

NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                    notificationBuilder.setTicker("New Message");
notificationBuilder.setContentTitle("This is the title").setContentText("Message...");

mNotificationManager.notify("myApp", 1234, notificationBuilder.build());

}

MainActivity.class

@Override
public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    setIntent(intent);

    Intent temp = intent;
    Bundle extras = temp.getExtras();
    System.out.println("Log extra new intent: "+temp.getStringExtra("message-info"));

    if(extras != null)
    {
        System.out.println("Log extra new intent HAS EXTRA: "+temp.getStringExtra("message-info"));
    }
}

I only get null values. App.getContext() references to my App class that extends from Application.

Why doesn't MyActivity receive any extras?

Upvotes: 2

Views: 2485

Answers (3)

Max
Max

Reputation: 1548

In my case, removing this worked,

    notiIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

Upvotes: 0

Seyed Ali Mousavi
Seyed Ali Mousavi

Reputation: 21

I had the same problem! I was filled with request codes notification code :

stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(**post_id**,PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(**post_id**, mBuilder.build());

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006539

I only get null values.

You presumably already have a PendingIntent for that component, and it does not have the extras.

If you want to update a possibly-existing PendingIntent, add FLAG_UPDATE_CURRENT to your getActivity() call.

Upvotes: 2

Related Questions