In activity get old data in bundle - android

I create notification and add Intent to start Activity.
Intent intent = new Intent(this, PriceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
bundle.putString(ID_ORDER, idOrder);
intent.putExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("GCM Notification");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("Text"));
mBuilder.setAutoCancel(true);
mBuilder.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(0, mBuilder.build());
In activity PriceActivity get old data. This data was sended in past attempts.
Code get data Bundle:
Bundle bundle = getIntent().getExtras();
final String idOrder = bundle.getString(GcmIntentService.ID_ORDER);
Log.i("idOrder", idOrder);
What is the problem?

Android is reusing the old PendingIntent. To update the "extras", you need to use PendingIntent.FLAG_UPDATE_CURRENT in:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Related

Send data to activity when clicked on notification?

i am opening a activity when notification is clicked . notification is activated from a service class .i want to send data to new activity opened from notification i am using intent1.putExtra("lable",lable); but in new activity it is giving me nullpointer exeption.
intent1 = new Intent(this.getApplicationContext(), simplestop.class);
intent1.putExtra("lable",lable);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("title" + "!")
.setContentText("Click me!")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
I use this code for send data to activity.
Intent intent = new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("from", "notification");
pIntent = PendingIntent.getActivity(this, 3, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
May be you have problem in setting flag=0 or try to change request code=0 to any other integer.
Sending data.
Intent intent = new Intent(getApplicationContext(), simplestop.class);
intent.putExtra("lable", lable);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.yourLayoutOfNotification);
contentView.setOnClickPendingIntent(R.id.IdOfTheItemYouClick, contentIntent);
Try to retrieve data something like this in your simplestop.class
if (getIntent().getDataString() == null) {
String lable = Objects.requireNonNull(getIntent().getExtras()).getString("lable");
}
And I think you need to call the onNewIntent at the simplestop.class
And there to retrieve data.
And try to create a Log if the data are retrieved.
Log.d("Data", lable);
And tell what is the Log showing.

Intent returns WRONG data

I have this function to create a notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setSound(alarmSound)
.setContentText(subtitle);
if(haveIntent) {
Intent intent = new Intent(context, PSBeaconMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("idToPass", beaconSettingID);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
mBuilder.setContentIntent(pIntent);
}
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
I send for now a hardcoded value for the beaconSettingID such as this:
https://scontent-amt2-1.xx.fbcdn.net/v/t35.0-12/28822214_1907084479363661_711965391_o.png?oh=b4c247836bcbd572d27f53738d2bdf6c&oe=5AA0C75E
This is my getIntent.getBundle:
if (bundle != null && bundle.containsKey("idToPass")) {
beaconUUID = bundle.getString("idToPass");
}
But I get back this:
https://scontent-amt2-1.xx.fbcdn.net/v/t35.0-12/28768405_1907084916030284_1955200830_o.png?oh=bcccf27352c90b248e82e08d902b8c15&oe=5AA1BA02
I don't understand? beaconID was the name of the "extra" key before. Since then I deleted the app, and changed it to idToPass. But I still get the old key, and more than that, a wrong value for it. Why is this happening?
Deleting an app doesn't mean that you deleted the previous notification.
Try to change
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
to:
PendingIntent pIntent = PendingIntent.getActivity(context, SOME_UNIQUE_ID, intent, 0);
In order to create new
Or to:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
To update previous notification
Hope it helps

Android AlarmManager putExtra to PendingIntent and NotificationCompat.Builder

I'm trying to putExtra to Activity which will be launched after clicking on notification, but instead of value that I set I'm getting default value. This is my code in AlarmReceiver:
Intent notifActiv = new Intent(context, NotificationActivity.class);
notifActiv.putExtra("ID", id);
PendingIntent pI = PendingIntent.getActivity(context, 0, notifActiv, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(string).setTicker("You got meeting today!")
.setContentText("Click here for more details");
mBuilder.setContentIntent(pI);
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
And this is my NotificationActivity where I'm trying to get Extras:
Intent intent = getIntent();
int id = intent.getIntExtra("ID", 0);
Could you please tell my where I'm doing something wrong?

Passing Data from Android Notifications

I have built a notification and am displaying it properly, but I can't figure out how to pass data to the activity. I pulled one string from the intent to display as the title of the notification, but I need to pull a 2nd string, and have the NotificationHandlerActivity process it.
//inside intentservice
private void sendNotification(Bundle extras) {
Intent intent=new Intent(this, NotificationHandlerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("link", extras.getString("link"));
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
long[] vibrate = {100L, 75L, 50L};
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)
.setContentTitle(extras.getString("title"))
.setOngoing(false)
.setAutoCancel(true)
.setVibrate(vibrate);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
//Inside NotificationHandlerActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
}
You should use the extras on your Intent. Because your intent is currently anonymous, you cannot do so. Extras are basic key-value stores. See below:
public static final String KEY_SECOND_STRING = "keySecondString";
...
...
String secondString = "secondString";
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, NotificationHandlerActivity.class);
intent.putExtra(KEY_SECOND_STRING, secondString);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
...
then, from your NotificationHandlerActivity, you can access that secondString from the intent.
#Override
public void onCreate(Bundle sIS){
super.onCreate();
String secondString = getIntent().getStringExtra("keySecondString");
...
}
use it like this :
Intent intent=new Intent(this, NotificationHandlerActivity.class);
intent.putExtra("key", "value");
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
long[] vibrate = {100L, 75L, 50L};
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)
.setContentTitle(extras.getString("title"))
.setOngoing(false)
.setAutoCancel(true)
.setVibrate(vibrate);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Apparently using 0 as the requestCode when calling getActivity() on the PendingIntent isn't the way to go. I updated it to just use System.currentTimeMillis() and that seemed to work. I'm assuming that the first time I built the notification, using 0 as the requestCode, the extra wasn't present.

PendingIntent not working when adding extras in intent

I have GCMIntentService implemented and whenever i get the push notification i need to show
the notification in notification menu and open an activity with some bundle values in the
intent.
I can see the notification in the notification menu but clicking on it just doesn't do anything. Following is the code which i am using :-
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent i = new Intent(this, ChatDetail.class);
Bundle b = new Bundle();
b.putString("my_id", "5356b178b130a74a57019fe9");
b.putString("you_id", youId);
i.putExtras(b);
// PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
// new Intent(this, MainActivity.class), 0);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i,PendingIntent.FLAG_CANCEL_CURRENT );
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.pool_my_ride_icon)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text))
.setContentText(text);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I can see the notification but when i click on it the notification menu slides up and doesn't
do anything doesn't opens any activity.
If i don't send any bundle in the extras then i can open the activity but when i send
bundle values then i can't open the activity.
Thanks in advance
I'd try to add the extras to the intent one at a time :
i.putExtra("my_id", "5356b178b130a74a57019fe9");
i.putExtra("you_id", youId);
hey try this it worked for me
Intent i = new Intent(this, ChatDetail.class);
Bundle b = new Bundle();
b.putString("my_id", "5356b178b130a74a57019fe9");
b.putString("you_id", youId);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtras(b);
the important point here is to send the additional flags to actually want your intent to be delivered
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
Just let me know if it worked for you!

Categories

Resources