PendingIntent not working when adding extras in intent - android

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!

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.

In activity get old data in bundle

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);

Clicking on Big View Notification doesn't launch activity

Clicking on the notification doesn't launch any activity. I followed the developers code but something isn't working. Any suggestions?
Intent dismissIntent = new Intent(getActivity(), Activity1.class);
dismissIntent.setAction("action1");
PendingIntent piDismiss = PendingIntent.getService(getActivity(), 0, dismissIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent notifyIntent = new Intent(getActivity(), Activity2.class);
notifyIntent.setAction("action2");
PendingIntent piNotify = PendingIntent.getService(getActivity(), 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Alert")
.setContentText("Alert text")
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Big text message"))
.addAction(R.drawable.icon1,
"Dismiss", piDismiss)
.addAction(R.drawable.icon2,
"Notify", piNotify);
NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
I also added android:exported="true" to the activity in the Manifest
I think the problem is that you are calling
PendingIntent.getService()
instead of
PendingIntent.getActivity()
as the doc say:
PendingIntent.getService :
Retrieve a PendingIntent that will start a service, like calling Context.startService(). The start arguments given to the service will come from the extras of the Intent.
PendingIntent getActivity : Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

Intent data not updated in activity while launched from andorid push notification click

I am using Google cloud messaging for my android application.GCM part is running successfully.Now I want to show the message in a UI activity when the user taps on the notification icon in the status bar.
I am launching my ResultShowActivity from the notification using the following code.
Intent notificationIntent = new Intent(context, ResultShowActivity.class);
notificationIntent.putExtra("Message",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setSound(alarmSound)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
The message is being sent properly,and shown correctly when the GCM notification appears but when I capture the extras of intent in ResultShowActivity it is always showing the old message.
I am using following code to receive message from intent in ResultShowActivity.
String GCMMsg = showViewIntent.getStringExtra("Message");
I have tried removing the in ResultShowActivity by using
intent.removeExtra("Message");
or
intent.replaceExtras(extras);
Can anyone please suggest how to retrieve the updated message from intent.Thanks for any help regarding this.
Please use following code
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(this,iUniqueId,notificationIntent, 0);
Instead of following line of code
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
Hope this will help
You need to add Intent.FLAG_ACTIVITY_SINGLE_TOP to intent flag and PendingIntent.FLAG_UPDATE_CURRENT to pending intent flag.
Intent intent = new Intent(this,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Important: Make sure to implement onNewIntent(Intent intent). This function will be called when your activity is in foreground.

how to start aplication or bring to front when user clicks on a notification click on android

I'm working whit push notifications.
When a user clicks on a notification I want to start the application in case it is not started, or bring it to front in case it is started.
Thanks
this is the complite code I found the answer here Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, noti);
The important things are the flags on the Intent, this will open the app or bring it to front if is opened, or do nothing if you click on the notification while you are browsing the app
implement pending intent.
Code
Intent pi = new Intent();
pi.setClass(getApplicationContext(), youactivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = mMessage;
// construct the Notification object.
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis());
manifest
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity>
Just set the intent for the notification, this is covered in details in the official API guide.
You do that by creating a PendingIntent.
For example, this will launch a MainActivity when notification is clicked.
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Notication title")
.setContentText("Content text")
.setContentIntent(pendingIntent).build();
I don't know whether you're talking about Google Cloud Messaging or not. But if it is a normal Notification then while creating your notification you've to provide Pending Intent. Just put your desired class in Pending intent so that when user clicks on that notification you'll be driven to your so called Application. A snippet :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
"Your application activity".class), PendingIntent."Flag you want");
After this use this intent while creating notification. It's method is setContentIntent("above intent") and fire your notification with NotificationManager!

Categories

Resources