close activity before opening it on clicking notification - android

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

Related

Keep my main Activity when starting from Notification Intent

In my App, I have MainActivity with bottom navigation. When I receive a new notification, when I click on it (to open it), I don't want to recreate my MainActivity if it already exists. So I tried to add flags, but it doesn't work at all...
Could you help me guys?
Intent newsFeedDetailsActivityIntent = NewsFeedDetailsActivity.newInstance(mNewsFeedId);
Intent mainActivityIntent = MainActivity.newInstance(MainActivity.createBundle(navItem, currentMs));
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder builder = TaskStackBuilder.create(MyApplication.getInstance())
.addNextIntentWithParentStack(mainActivityIntent)
.addNextIntent(newsFeedDetailsActivityIntent);
PendingIntent pendingIntent = builder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT);
// Set pending intent to notification
notification.setContentIntent(pendingIntent);

Open Activity on Notification Click - Intent flag issue

I am creating an Android app. I have set up the notification system to open a specific activity/class on notification click. When my App is totally closed and I send a notification and when I click on it opens the correct activity like this example:
App is not running > Notification comes in > Click on the notification > Activity_T opens.
BUT, when my app is running and I get a notification and when I click on it to open the Activity_T it doesn't it loads MainActivity instead.
The flags I use are the follow
Intent intent = new Intent(mContext, (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("tt", title);
intent.putExtra("bd", body);
mContext.startActivity(intent);
I want to open Activity_T on notification click either the app is running or not. How to force open Activity_T whenever I click on the notification.
I send notification from receiver onReceive method. When click on notification,I open an activity,even the app was killed. I used like that
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, "")
.setSmallIcon(R.drawable.my_ic)
.setContentTitle(title)
.setContentText(msg)
.setContentIntent(contentIntent);

Android : Pending Intent Not Firing if the application is not in foreground

I am trying to open an activity when user clicks on Notification. I had issued following Intent as pending Intent
Intent caseIntent = new Intent(mContext, LoginActivity.class);
caseIntent.putExtra("priority", priority);
caseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, caseIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT);
But when I click on the notification, if the app is not in forground the active which I have specified is not opening at all.
Is something wrong with this Intent?

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!

Categories

Resources