Android - Track Notification onclicked - android

I want to start a activity when users clicked on notification but there are no onclicked event for notification (?), so how can we know when the notification is clicked?
Thanks!

http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Look into PendingIntent setup there.
The PendingIntent will get called when you click the notification.

Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notificationIntent will be called when notification is clicked.

Related

android - How to show a dialog when click on notification

I get notification in my application through BroadcastReceiver, The question is , How can I parse data to an activity and make a dialog with the received data ?
this is my code but when I click on the notification , nothing happens :
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification myNotification = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setLargeIcon(remote_picture)
.setContentTitle(onvan).setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setContentIntent(pending).build();
notificationManager.notify(1, myNotification);
I've some variablese like link , msg, onvan that contains my data and I need to send these variables to an activity and make a dialog .
How can I do so ?
Try this:
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
just make the PendingIntent open up one of your Activities and have your Activity be complete transparent and just open a Dialog.
EDIT: IF you want to open an Activity from notification click event:
Assuming that notif is your Notification object:
Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
For more detail visit here. Android - Prompt Dialog window when touch on Notification
You can send data from your notification to another Activity using method putExtra and put this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));
inplace of this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);`

Save to DB after clear the push notification android

I am working with push notification and I have one problem.
When I click in the received notification my code launch a new intent with an activity.
I use:
Intent i = new Intent(getApplicationContext(), DemoActivity.class);
i.putExtra("msg",msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
ITS OK FOR ME
But if I want only save this notification in SQLite how can I pass the message?
I know that with:
Intent bd = new Intent(getApplicationContext(), AbaseDatos.class);
bd.putExtra("msg",msg);
PendingIntent contentIntentbd = PendingIntent.getActivity(this, 0,
bd, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setDeleteIntent(contentIntentbd);
Call a new activity when clear the notification, but the new activity open the layout.
I dont want any new activity be launched. Its possible launch a javaclass with an Intent?
Any idea?
You can launch a service with an intent.
http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context%2C%20int%2C%20android.content.Intent%2C%20int)

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!

Android notification click

I am running a service which is popping notification on getting a new task. If I close my app it's running in the background and pop up a notification. What I would like to do is, when I click on that notification, open the app. Can I do that ? How ?
You can do this by adding an intent to your notification:
Intent notificationIntent = new Intent(context, YourAppActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, "headline", "body msg", contentIntent);
notifcationMgr.notify(id, notification);
Just replace YourAppActivity with the activity you want to launch.

how to capture the clear event when clear button is pressed in notification bar in android

HI,
in Android when the clear button is pressed on the notification screen i want to capture that in my application and do some stuff. please help me in how to achieve this.
Add a deleteIntent to your Notification.
in my code i already have contentIntent and now after adding the deleteintent , when i m pressing the clear button contentintent things are getting executed.
Intent noti=new Intent(context, MyService.class);
noti.putExtra("key1", true);
contentIntent = PendingIntent.getService(context, 0, noti, 0);
Intent stopIntent = new Intent(context, Myervice.class);
notificationIntent.putExtra("Key2", true);
PendingIntent deleteIntent = PendingIntent.getService(context, 0, stopIntent, 0);
now when i m pressing the clear key i m getting the key1 and key 2 both

Categories

Resources