embo
embo

Reputation: 8859

Notification: pass data

I'm trying to pass data from notification to activity:

btnShedule.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

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

            Intent notificationIntent = new Intent(Oconf.this, Oconf.class);
            notificationIntent.putExtra("test", 122);

            PendingIntent contentIntent = PendingIntent.getActivity(Oconf.this, 0, notificationIntent, 0);

            Notification notification = new Notification(R.drawable.notify_deal, "text", System.currentTimeMillis());
            notification.setLatestEventInfo(Oconf.this, getString(R.string.notify_events), "text", contentIntent);

            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(0, notification);
        }
    });

Then, onResume:

public void onResume() {

    String q = getIntent().getStringExtra("test");
    if (q == null) {
        Log.e(TAG, "null!");
    }
    else {
        Log.e(TAG, q);
    }

    super.onResume();
}

I got "null!". Where is error?

Upvotes: 2

Views: 4179

Answers (2)

Ania
Ania

Reputation: 31

In my case onNewIntent() was never called when I tapped on notification. However I added the following code in the onCreate() method:

String m = getIntent().getStringExtra("put_extra_string");

and I got the string I was trying to pass to the notification's intent.

Upvotes: 3

embo
embo

Reputation: 8859

:)

protected void onNewIntent(Intent intent)

Upvotes: 2

Related Questions