Save to DB after clear the push notification android - 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)

Related

close activity before opening it on clicking notification

I am opening an activity on clicking notification, that is working fine. But if that activity (which I am opening on clicking notification) is already open (user has opened it) then I want to close it before opening it as a result of clicking notification. So how can I do that. Please help.
Code:
Intent intent = new Intent(context, ViewReminders.class);
intent.putExtra("CALLER","GenNot");
intent.putExtra("ID",notification_id);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
Try with setting the following flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

NullPointerException after Notification is clicked in notification bar(PendingIntent)

I am trying to get my PendingIntent working but whenever I click the notification in the notification drawer, my app crashes.
So basically I have this java class called CustomerCurrentlyServing.class and when it is loaded, it retrieves all the strings passed from the previous activity as shown below:
queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO));
queueKey = getIntent().getExtras().getString(Constants.EX_MSG_QUEUE_KEY);
shopName = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_NAME);
shopKey = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_KEY);
customerid = getIntent().getExtras().getString(Constants.EX_MSG_CUSTOMER_ID);
I implemented a method for the notification as shown below:
public void showNotification() {
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
Resources r = getResources();
Notification notification = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.notification_title))
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(r.getString(R.string.notification_title))
.setContentText(r.getString(R.string.notification_text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
It is basically opening the same class when the user clicks on the notification in the notification drawer. However, it crashes whenever I click on the notification and the error is NullPointerException on the line:
queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO));
Any idea how to solve this error ? I have been trying to solve this error for a week and your help is greatly appreciated.
You should putExtra on the Intent you pass to PendingIntent
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);
here you are passing new instance of Intent
you should pass already created instance i.e.
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
and write all those putExtras before creating PendingIntent object.
In this part of the code you are adding the extras to the wrong intent , you are creating an intent then creating a new one that you pass to the pending intent!
What you should be doing is this:
Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey);
intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName);
intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);
And not creating a new intent like you did in your pending intent :
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);
intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);
If you do this you are putting the extras om the first intent then passing a whole new one without extras to your pending intent so when the user clicks on the notification and it opems up the activity the activity will try and get extras that dont exist thus giving a null pointer.

Click on notification and then go to next detail page where there is also the detail of that are present

I am the student of computer science last semester student.
I have problem:
I set my Android Application that receive notification from online database of job finder web application.When new entry occur then notification comes here on Android App.
So now i want if i click that notification then there will be a page open in which the detail of that job will present.
so please guid me i have only few days to submit my final year project.
I will be very thankfull.
Have a look at the Android Developers Guide. There they describe to set up a PendingIntent to react to the click on the notification.
PendingIntent resultPendingIntent;
...
mBuilder.setContentIntent(resultPendingIntent); // Code comes directly from this Developer Guide
You have to use Pending Intent to open page when click on Notification.
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, YourActivityToBeOpen.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
You can find more details here.
To create Notification :
NotificationManager mgr = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.stat_notify_chat,
"Android Example Status message!",
System.currentTimeMillis());
// This pending intent will open after notification click
Intent i = new Intent(this, NotifyMessage.class);
PendingIntent i = PendingIntent.getActivity(this, 0, i, 0);
note.setLatestEventInfo(this, "Android Example Notification Title",
"This is the android example notification message", i);
// After uncomment this line you will see number of notification arrived
// note.number=2;
mgr.notify(NOTIFY_ME_ID, note);
Intent i = new Intent(this, NotifyMessage.class);
PendingIntent i = PendingIntent.getActivity(this, 0, i, 0);
Its manage your navigation after click on notification. If you want no navigation then use :
Intent i = new Intent();
PendingIntent i = PendingIntent.getActivity(this, 0, i, 0);
I hope it will help.

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 - Track Notification onclicked

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.

Categories

Resources