spyker10
spyker10

Reputation: 129

how i can intent from notification?

Why is Intent from notification null?

Intent resultIntent = new Intent(context, myclass.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Bundle extras=new Bundle();
        extras.putString("key","value");
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
            NotificationCompat.Builder n = new NotificationCompat.Builder(context)
                    .setContentTitle("test")
                    .setContentText(text)
                    .setSound(sound)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .setExtras(extras)
                    .setSmallIcon(android.R.drawable.ic_media_play);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService((context.getApplicationContext().NOTIFICATION_SERVICE));
            notificationManager.notify(0, n.build());
    }

but in onStart() of myclass when i call getintent().getExtras() the result is null, why? how can I getExtras() from notification Intent ?

Upvotes: 0

Views: 84

Answers (4)

Rahul Subedi
Rahul Subedi

Reputation: 125

This works for me, you can get idea from it:

 Intent notificationClickIntent = new Intent(context, NotificationAppListing.class);
            notificationClickIntent.putExtra("notificationType", notificationTypeOne);
            notificationClickIntent.putExtra("notificationUrl", notificationUrl);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(Search.class);
        stackBuilder.addNextIntent(notificationClickIntent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(mId, notificationBuilder.build());

Upvotes: 2

Krishna kottayada
Krishna kottayada

Reputation: 13

you need to place

resultIntent.putExtras(bundle)

right after bundle is assigned with key value pair
then you can get the values

Bundle extras = getIntent().getExtras();
if (extras != null) {
String yourValue = extras.getString("key");    
}

Upvotes: 1

snachmsm
snachmsm

Reputation: 19263

you should add your extras to your resultIntent, not NotificationCompat.Builder (and not resultPendingIntent)

Bundle extras=new Bundle();
extras.putString("key","value");
resultIntent.putExtras(bundle);

or simply:

resultIntent.putExtra("key","value");

Upvotes: 1

Sourabh Bans
Sourabh Bans

Reputation: 3134

i think u forgot to call.

resultIntent.putExtras(extras);

before calling it.

Upvotes: 2

Related Questions