pressing on notification opens the same activity twice - android

If i have an activity that closed by a user (user pressed on home so the app is still in app stack)
and then he gets a notification, when he presses on it i start an activity
and now the same activity is opened twice.
How can i prevent this from happening?
My CODE:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setLights(GCMSIntentService.PURPLE, 500, 500)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());

android:launchMode="singleTask"
in manifest Or,
use it as flag for your intent.

You need to add FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP,FLAG_ACTIVITY_NEW_TASK flags to your notification intent.So change
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
to
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);

by android:launchMode="singleTask", if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance and adding flags to the intent
FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP
refer task and back stack

Here's the error.
notify functions id has to be changed everytime. You're giving it 1 everytime. thats the error.
mNotificationManager.notify(1, mBuilder.build());
give a different id everytime. it will work like a charm.
For example, use sharedPrefences to provide different id's everytime.
check this,
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setAutoCancel(true)
.setLights(GCMSIntentService.PURPLE, 500, 500)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
SharedPreferences sp = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
myIntValue = sp.getInt("notificationid", 0);
mNotificationManager.notify(1, mBuilder.build());
SharedPreferences sp1 = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp1.edit();
editor.putInt("notificationid", myIntValue+1);
editor.apply();

Related

How to close app by clicking a button in the notification?

I try to close my app after button click.
I know that i need to use pending intent and intent to do that, but i don't know how can i do it and which flags or actions can I use.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "")
.setSmallIcon(R.mipmap.ic_launcher_notification)
.setContentTitle("Close your app")
.setContentText("Your app is still running...")
.addAction(R.drawable.ic_exit,"Close App",
PendingIntent.getBroadcast(this, 0, intent, 0))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
This is not working...

Launch activity on click notification

I want the Main Activity to be launched when the Notification is clicked. This is even when the user is not in the app. How do I do so?
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());
First of all you need PendingIntent :
Intent intent=new Intent(mContext, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notificationIntent= PendingIntent.getActivity(mContext,requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Then in your NotificationCompat.Builder add this: .setContentIntent(notificationIntent)
For unique request code use this : int requestCode=(int)System.currentTimeMillis();
You can use this in Non Activity classes too, e.g: Service, BroadcastReceiver . Then your app will be launched even it is in closed state.
//add intent and PendingIntent for open activity
Intent attendanceIntent = new Intent(getApplicationContext(), Details.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
attendanceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentIntent(pendingIntent)
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());

Android notifications does not disappear after click

I have a piece of code on my android studio project:
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
// .setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("New updates from StoriesCity!")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
It's working fine and i see the notification, but when i click on it it does not go away..
Maybe it's because i'm using .notify ?
Please help me modify the code to makeit disapeair on click.
I'm a beginner :)
Use:
.setAutoCancel(true);
setAutoCancel
Set flag FLAG_CANCEL_CURRENT instead of 0
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
Also
.setAoutoCancel(true)

Notification disappear after activity finish

I have an activity named A which start from a broadcast receiver. Activity A triggered a notification but it disappear automatically when activity destroy(finish). But I want to keep this notification until the user click or manually clear the notification.
How activity start from broadcast receiver
Intent i = new Intent(context,A.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
i.putExtras(intent.getExtras());
context.startActivity(i);
Notification
Intent notificationIntent = new Intent();
notificationIntent.setClass(context,B.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_small)
.setContentTitle(status)
.setTicker(status)
.setAutoCancel(false)
.setContentText(message)
.setDefaults(Notification.DEFAULT_SOUND)
.setLargeIcon(
Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.build();
manifest options
<activity android:name="com.example.activity.A"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>
Note: I also tried singleInstance but no luck.
Edit(Fixed)
I made a silly mistake. I called clearAll() instead of cancel a specific notification in onDestroy() function.
I think you are cancelling all the notification in your app when the activity is destroyed.
remove autocancel() totally and try this,
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MenuActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon).setContentTitle(" ").
setStyle(new NotificationCompat.BigTextStyle().bigText(bundle.get("").toString())).
setContentText(bundle.get("").toString());
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Calling app from notification is recreates the activity

I want to resume main activity when user call app from notification but this code is recreating the main activity.How can I stop it ?
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("My notification")
.setAutoCancel(true)
.setSmallIcon(R.drawable.twitter)
.setContentText("Hello World!")
.setNumber(15);
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
Also I placed android:launchMode="singleTop" to my main activity in my manifest file.How can I resolve this ?
For my app I use this code to do this:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
And in the manifest I set the lunch mode of the MainActivity as singleTask
android:launchMode="singleTask"

Categories

Resources