Preserve back activity while clicking notification - android

My problem is like that: When I'm pressing the notification i got, some activity is open, lets say activity number 3. But when I press the back button, I navigated by the android to the menu. What I want to do is to preserve the correct navigation experience of the user, and when I'm pressing the back button it will take me to activity number 2, and when I'm pressing there on the back button, it will take me to activity number 1, and so on.
Another problem I have is that, when I'm opening activity number 2, I need to set there some extra data in the intent, but I don't know how to set it.
Summery of my problem: preserve the correct navigation of the user and set some extras for the activity that will be open by the back button click.
Here is my notification code:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Question")
.setContentText("" + CurrentQuestion.getAuthor().getUserName() + ": " + CurrentQuestion.getQuestion() + "");
Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(openHomePageActivity);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
Thanks!

Looks like you want to synthesize a back stack: http://developer.android.com/training/implementing-navigation/temporal.html#SynthesizeBackStack (see "Create back stack when starting the activity")
and also http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse ("Preserving Navigation when Starting an Activity" as a "Regular activity")

Anyone in same scenario can use this solution that I use in my apps.
Override onBackPressed (in your activity routed from notification) and check if your app is in stack or not. if not then start your root activity. User can always navigate from your root activity. This is what I do in my apps
#Override
public void onBackPressed() {
if (isTaskRoot()) {
Intent intent = new Intent(this,YourMainActivity.class);
startActivity(intent);
super.onBackPressed();
}else {
super.onBackPressed();
}
}

Save the parameters you need in onSaveInstanceState & get them back in onRestoreInstanceState. that way it won't matter if your activity was paused by any other notification as well.

Related

PendingIntent doesn't hold the data assigned to Intent when setting up notification

I have a helper method that sets up and returns a notification that user's credentials are expired and he needs to log back in. Upon pressing this notification, I expect that he gets a login activity without being able to go back to the home screen.
I was trying to use all the flags to force the stack to be cleared (to erase history of activities), but I can still press back on the login activity and go back to the home screen (like I was never logged out). So I tried to set some extra info for the login activity to recognize if back button can be pressed or not. Nothing works: history is available and none of the extra info that I pass is recognized. Can anybody help me understand where my problem is?
Here is the helper method (in the UiUtils class):
public static Notification getInvalidTokenNotification(Context context)
{
SharePrefHelper.setRememberMe(false);
Intent loginIntent = new Intent(context, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
loginIntent.putExtra("CAN_GO_BACK", false);
loginIntent.putExtra("TEST_VALUE", 12);
// Action on click:
PendingIntent loginPendingIntent = PendingIntent.getActivity(
context,
0,
loginIntent,
0
);
return new Notification.Builder(context)
.setContentTitle(context.getString(R.string.notification_token_invalid_title))
.setContentText(context.getString(R.string.notification_token_invalid_content))
.setSmallIcon(R.drawable.logo_wenow)
.setContentIntent(loginPendingIntent)
.setAutoCancel(true)
.build();
}
Here is the code to show notification:
#OnClick(R.id.debug_notification_test)
public void onClickTestNotificationBtn()
{
// Show notification:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.notify(UiUtils.NOTIFICATION_ID_INVALID_TOKEN, UiUtils.getInvalidTokenNotification(getApplicationContext()));
}
// Logout user:
SharePrefHelper.delUser();
}
Use PendingIntent.FLAG_UPDATE_CURRENT as the last parameter of this call:
PendingIntent loginPendingIntent = PendingIntent.getActivity(
context,
0,
loginIntent,
0
);
This will ensure that you replace the extras every time you create the Notification

Adding phone icon to status bar(Where to remove status bar?)

I just want to add phone icon to status bar like in the picture.
Nothing more, just this small icon should be shown in the status bar.
How Can I do this?
Can I get this icon anyway by calling some methods? or should I draw this icon with any tool? Then How to add this icon?
EDIT:
I finally show a notification by using this code:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_phone_white_36dp)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, SecondActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(SecondActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(2, mBuilder.build());
But I am using two activity in my application. Removing is problem now. I want to remove this notification when user exit the program so where to cancel notification?
mNotificationManager.cancel(2);
I tried in onDestroy: It is not working, it is not being removed when user exit the program.
#Override
protected void onDestroy() {
super.onDestroy(); // Always call the superclass method first
mNotificationManager.cancel(2);
}
I tried in onStop: It is working but when user pass another application, this notification is removed. I do not want like this.
#Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
mNotificationManager.cancel(2);
}
Finally where to cancel?
This is a notification icon, which is displayed when notification is received. It is attached with the notification at the time of creating notification. You can add any type of icon while sending notification. In your status bar, notification has phone icon attached with it, which is being displayed. You can find more info about notifications here.
You can download the icon from here https://material.io/icons/#ic_phone
Then to add it on your app you have to create a notification https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Current Activity finished when I build a notification

I want to start a notification in current Activity (ChatActivity),when I touch the notification ,I want to enter the ChatOneActivity.
However I don't want the current Activity (ChatActivity) finished when I'm in the ChatOneActivity(Because I'm receiving the data).And when I press the back button,I want to stay in the ChatActivity.
(The point is I do not want the ChatActivity finish ,no matter which activity I am current in).
So what should I do?
Here is the code
private void showNotification(String id, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.sym_action_email)
.setContentTitle("You have a new message")
.setContentText(message);
Intent intent = new Intent(ChatActivity.this,ChatOneActivity.class);
intent.putExtra("toId", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
Now I enter the ChatOneActivity,when I press the back button,I return to the desktop. means that the ChatActivity has already finished,which I don't want
I believe you can't do what you want.
First of all, take a look to this link https://developer.android.com/guide/components/tasks-and-back-stack.html worth a read.
You are opening ChatOneActivity from ChatActivity and it will be saved in the Back Stack, Android system can kill your activity when memory is needed but it will be recreated.
Also, you are adding ChatOneActivity as a parent in the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
If you are in the parent activity and the back button is pressed, is reasonable that the app will close.
If you want to open Activity2 from Activity1 and when you press back return to the Activity1 you shouldn't have to do anything, the Android Back Stack will manage the behavior.

strange behavior when starting activity from notification

I am using Parse to send notification to Android phones and when I receive push I will generate a notification using the below function.
private void generateNotification(Context context, String title, Intent intent, boolean hasSound) {
int NOTIFICATION_ID = ("onccent" + System.currentTimeMillis()).hashCode();
PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle(context.getString(R.string.app_name)).setContentText(title);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
if (!hasSound) {
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
}
The intent is a intent of a activity displaying messages and let's call it Activity B. I will use Activity A to represent the home activity.
The problem is: when A exists, all things work well. When I click a notification, the stack becomes
A -> B1
And click another:
A -> B1 -> B2
This is what I expect. However, when I kill the whole app and then click on the first notification. It will open a B but the following notification will only bring my app to front without open a new B.
It seems something will not work without A. I manage to open A first if A does not exist in the stack so that define A as the parent activity of B. The notification works but it seems all previous activity goes away.
How can I solve this? In short, I just want that a new activity will open when I click on the notification.

Android - Send notification with id

Please excuse my English, I'm French :
I've got a question about android notifications...
There is an inbox on my app and user is notified when he received a new notified.
When he click on the notification, I want to open the right activity with the right conversation so I've to pass an ID.
I've made a lot of search but I can't find...
Coul'd you help me please ?
Here my code :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, LandingActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
Thanks.
In this case, you could save the ID of the conversation in the Intent that will launch your app. You do this by using extras:
resultIntent.putExtra("conversationID", "234553");
Then, in your MainActivity, you handle the case where the user has clicked on your notification:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("conversationID");
//Display conversation
}
I don't know if I get your question right: If you want to start different Activities according to something you received, you have to make different Intents for those cases.
If you always want to open your LandingActivity and pass data to it, add them as extras to your Intent, see How do I pass data between Activities in Android application?.

Categories

Resources