Android Notification Click - don't backstack activity - android

I have a service in background which gives a notification.
I want the next thing:
If I click the notification and the app is not opened open the XActivity.
If the app is opened and the XActivity is created, go there and don't recreate the activity (because if this happen, on back key i will see the same activity again).
My notification code
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New posts!")
.setContentText("New funny posts had just arrived! Click here to see them!");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, XActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
I start the service from the XActivity. (just a activity name example)
Thank you.

i had the same problem, what i did was , i added this line android:launchMode="singleTop" to my activity in manifest, and it worked.

Problem for duplicate activity in your case is because of standard structure design of Activity in Android. For creating only single instance of activity (no duplication) you need to give launchMode property to your activity in your manifest file. You can use singleTop property for this behaviour. You can read more in provided link.
In Addition, for new data access you can use onNewIntent() for new intent coming to this activity and set intent using setIntent() and continue working with new data.
Hope this helps.
Thanks.

Related

Can't restart activity after clicking on notification

When app receives message from GCM, it shows notification.
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event_white_24dp)
.setContentTitle("TheBriefPost")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
When I click on notification, app opens activity but not restarting. What is the problem? How to solve it?
The answer should be related to the flags of the Intent/Pending Intent
According to PendingIntent in Official Docs
In the Pending Intent you sent a flag PendingIntent.FLAG_UPDATE_CURRENT
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this
new Intent.
I guess in your case you need to use PendingIntent.FLAG_CANCEL_CURRENT
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.
Accodring to Activity in Official Docs
In the Activity you set the flag Intent.FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at the top of the history stack.
I guess here you need to use Intent.FLAG_ACTIVITY_CLEAR_TASK
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.
app opens activity but not restarting
It should not be restarted. FLAG_ACTIVITY_SINGLE_TOP is "If set, the activity will not be launched if it is already running at the top of the history stack."
Why you want it restarted? To handle intent data? If so, implement onNewIntent() in your activity.

How maintain GCM push message in Android, its replaced by new one

My Android application receives push notification with some text messages.If I tap a push it redirects me to desired activity with latest push message (intent message) but I want to show my desired activity with corresponding push messages.
For example If I receives 10 push notifications and I tap 3rd notification, my code redirects me to the specified activity with 10th push notification's message, but I want to show 3rd intent push notification's message.
I know PendingIntent.FLAG_UPDATE_CURRENT replace the intent message, how can I redirect with corresponding message instead last message?
I have tried the following:
Intent intent = new Intent(this, TestActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("uid", uid);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext());
Notification notification = mBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(textMsg)
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(textMsg)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(textMsg))
.setContentIntent(resultPendingIntent).setContentText(textMsg)
.build();
NotificationManager notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(),
notification);
Use FLAG_ONE_SHOT instead and change the second parameter in getActivity which is set to 0. This answer will make it clear.
You should Change the pendingIntend's content for Different Messages.
Here is the snippet from PendingIntent document.
"A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it."
In simple words...try passing different id(Something like currentEpochTime) for each pendingIntent.

Return to previous app instead of home screen from Notification

I have an app that has an activity, say A. and I put a notification to the notification bar that starts A. That is, when user select the notification, A activity will be started. But when user presses back button, the system will go to Home screen, instead of the previous activity(maybe of another app). How can i just let the system finish A activity and go back to the previous activity instead of home screen?
My code for creating the notification:
Notification.Builder mBuilder = new Notification.Builder(this).setSmallIcon(R.drawable.icon).setContentText("title");
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
I seem to find the answer, add the following two lines:
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
and remove this line:
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
But actually I don't understand why.
Would anyone explain it or give a better answer?

PendingIntent opens main activity instead of desired activity

I am creating an android application that shows a notification in a Service.
I show the notification exactly as mentioned in documentation. It works fine and the PendingItent object starts the desired activity.
But after a while when I close and restart the application a couple of times, tapping on the notification will start the Main Activity (which is the parent of desired activity) instead of the desired activity.
The only way I found to fix that is to restart the device. (Clearing app's cache and uninstalling didn't help.
I googled my problem and also searched here a lot, but couldn't find anything relevant.
seems that I'm the only one having this problem.
Could anyone please help me with this problem?
Thanks in advance;
EDIT:
as I said, I do exactly like the documentation. But here is my code anyway:
Intent resultIntent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MyActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Body");
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(457, builder.build());

How do I make a Statusbar notification that does nothing and goes nowhere?

I have a working statusbar notification for Android already, but to create it I must set an Activity for it to open. I don't want any activity to open; however, this small project doesn't need an interface at all.
mNotificationManager = (NotificationManager) aContext
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
// Create the pending intent, which is basically NOW.
PendingIntent contentIntent = PendingIntent
.getActivity(aContext, 1, new Intent(aContext, BlankActivity.class),
PendingIntent.FLAG_CANCEL_CURRENT);
// Create notification
notificationBuilder = new NotificationCompat.Builder(aContext)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher) // required for launch
.setTicker("Downloading video...") // required for launch
.setWhen(System.currentTimeMillis()) // should be set for now.
.setContent(remoteView);
BlankActivity is what it sounds like -- an activity that has no content and closes upon being opened. But it still shows up on the list of recently opened windows. I cannot set this to null.
Is there a way I can avoid setting an intent for the status notification at all?
You don't need PendingIntent in that case. Just setting it null in setContentIntent() might help.
Another way can be
PendingIntent contentIntent = PendingIntent.getActivity(aContext(),0,new Intent(),PendingIntent.FLAG_CANCEL_CURRENT);
Try this.
Setting it to null does not work for early versions like SDK 2.3 which requires an intent to be set, otherwise android will choke at receiver's start with the exception:java.lang.RuntimeException: Unable to start receiver com.movistar.android.mimovistar.widgets.WidgetIntentReceiver: java.lang.IllegalArgumentException: contentIntent required
see this post

Categories

Resources