I got some issue regarding about the notification intent. I have 2 activity (A & B). Activity A is the main activity of my application. The first activity that user will go through. As for activity B is when the user enter by clicking on a button at Activity A.
The thing is when I clicked on my notification and it will direct me to Activity B. Then direct me back to Activity A onBackPressed. But when I close the application and open it back through the multi-tasking option, it resume my application at Activity B. I wanted the application to start back at Activity A instead after closing it.
The order should be
Activity A --> Activity B.
Notification onClick -> Activity B (onBackPressed) -> Activity A --> Close.
Re-open the app / open with multi-tasking feature --> Activity A
Please do let me know if there is any other information that I can provide for a proper understanding to my question.
GCM_Intent.class
Notification note = new Notification(icon, msgTopic ,System.currentTimeMillis());
Intent i=new Intent(this, Activity_B.class);
i.putExtra("topicId", topicId);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi=PendingIntent.getActivity(this, CommunitiesappConstant.NOTIFICATION_ID, i, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
note.setLatestEventInfo(this, topicName ,msgInfo+message, pi);
note.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
note.ledARGB |= 0xff0000ff;
note.ledOffMS |= 1000;
note.ledOnMS |= 300;
mgr.notify(CommunitiesappConstant.NOTIFICATION_ID, note);
Activity B.class
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Bundle bundle = getIntent().getExtras();
topicId = bundle.getString("topicId");
}
#Override
public void onBackPressed() {
super.onBackPressed();
Log.e(TAG, "onBackPressed");
Intent i = new Intent(Activity_B.this, Activity_A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
Notification is a message you can display to the user outside of your application's normal UI.This is perfect code to use of notification.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.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(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
Related
code create notification from BroadcastReceiver
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(G.context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(G.context.getString(R.string.app_name))
.setContentText("text")
.setAutoCancel(true)
.setColor(Color.MAGENTA);
Intent intent = new Intent(G.context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(G.context);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) G.context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, mBuilder.build());
If you are currently in MainActivity.java and then click on notification and go to B Activity the just simply call this.finish() or onBackPressed() in your back press control onClick() method. If your are opening your B Activity from any where by clicking one notification then Call below method in your B activity.
#Override
public void onBackPressed() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
Other way you can do like this
You Dont need do anything. By default when you move from Activity A to Activity B, Android adds the Activity A to the backstack. When you press the back button from Activity B or finish it, it automatically restore the Activity A from backstack.
If you wish to finish your Activity B programmatically call activity's finish() method when you want to do it.
For more detail Check this
I have an application,I am sending notification with this code
private void notBuild() {
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("Message")
.setAutoCancel(true)
.setSmallIcon(R.drawable.twitter)
.setContentText("user01 sent a message")
.setNumber(15);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.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(this);
// 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_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
This is starting mainactivity and it's okay.But I want to run a method in the main activity.
Example:You will see in the setContentText user01 sent a message,when user press the notification I want to run startChat(user01) method in the main activity.How can I do it ?
You can put data in the resultIntent variable.
resultIntent.putExtra('key', 'useridhere');
Then, in your Activity (onCreate or OnResume) you can call
String userId = getIntent().getStringExtra('key', null);
if(userId != null)
{
//StartChat(....);
}
I like to launch one of my activities in my application on clicking the notification.
I designed a pending intent as discussed in the link Notifications.
But when i click the notification, an activity is launched but that is not the activity supposed to launch from my application. Just the activity that has same class name as my activity is launched. I set a break point in my activity and the break point is never hit.
What is wrong? NotificationListActivity is the one I like to launch. Now the activity titled with NotificationListActivity is launched, but not my activity.
Intent resultIntent = new Intent(thisclasscontext, NotificationListActivity.class);
resultIntent.putExtra("MOBILENUMBER", tel);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(thisclasscontext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(NotificationListActivity.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);
You can use the below as a reference it works
public void notification()
{
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(MainActivity.this, SecondActivity.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(MainActivity.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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
How do I launch an activity when the user clicks on the notification? I am having constant crashes. I can not get the click on the notification to work. There is an onclick method shown below. When a button called ButtonOne is pressed, it will launch a notification in the top menu bar of the screen. I wanted to user to be able to press the notification and have it launch the activity called MainActivity. It crashes and will not launch the page. There is probably something wrong in my code for notification that I put inside of the MainActivity class. What is wrong?
ButtonOne.setOnClickListener(new View.OnClickListener() {
private int mId;
// anonymous inner class override for on click
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, CoverFlowExample.class);
MainActivity.this.startActivity(myIntent);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(MainActivity.this, MainActivity.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(MainActivity.this);
// 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_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
});
Found out that the problem was the AVD android emulator not the code. For some reason it did not update the code from earlier version. Tested it again and now it runs with no errors.
I have two activities:
Activity A - list of items
Activity B - detail view of an item
Normally, a user opens the app and Activity A is launched. A user sees a list of items, clicks one, and Activity B is started to display the item detail.
Activity B can also be started directly from clicking on a notification. In this case there is no back stack.
How can I make it so that when Activity B is started directly from a notification, the user can click the Back button and go to Activity A?
You can add an Extra into the Intent launched by the notification to detect when the app has been launched in that way.
Then you can override the onBackPressed() Activity method and handle that scenario, e.g.
#Override
public void onBackPressed()
{
Bundle extras = getIntent().getExtras();
boolean launchedFromNotif = false;
if (extras.containsKey("EXTRA_LAUNCHED_BY_NOTIFICATION"))
{
launchedFromNotif = extras.getBoolean("EXTRA_LAUNCHED_BY_NOTIFICATION");
}
if (launchedFromNotif)
{
// Launched from notification, handle as special case
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mActivity.startActivity(intent);
finish();
}
else
{
super.onBackPressed();
}
}
You should take care of this when you receive the Notification.
I have a similar situation solved:
Intent intent = new Intent(context,ListDetail.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ListDetail.class);
stackBuilder.addNextIntent(intent);
PendingIntent contentIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mBuilder = new Notification.Builder(context);
mNotifM.notify(NotificationId.getID(), mBuilder.setStyle(new Notification.BigTextStyle(mBuilder)
.bigText(bigText)
.setBigContentTitle(title)
.setSummaryText(summaryText))
.setContentTitle(title)
.setSmallIcon(icon)
.setContentText(summaryText)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setTicker(bigText)
.build());
You need to set in your Manifest the hierarchy of the Activities:
<activity
android:name=".ListDetail"
android:label="Detail"
android:parentActivityName=".List" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".List" />
</activity>
I have tried one sample.Please go through with this link
https://github.com/rajajawahar/NotificationBackStack
Activity you want to launch..
Intent launchIntent = new Intent(context, SecondActivity.class).putExtra("Id", id);
Parent Activity, if back pressed
Intent parentIntent = new Intent(context, FirstActivity.class);
Add Both the activity in the taskbuilder
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
PendingIntent resultPendingIntent = stackBuilder.addNextIntentWithParentStack(parentIntent).addNextIntent(launchIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(context);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, notificationCompatBuilder.build());
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationCompatBuilder.setAutoCancel(true).
setContentTitle("First Notification").
setContentText("Sample Text").
setSmallIcon(R.mipmap.ic_launcher).
setContentIntent(resultPendingIntent);
mNotifyMgr.notify(id, notificationCompatBuilder.build());
catch the back-button-key event with onKeyDown()-method and let the user go to activity A. Don't forget to return true to prevent the event from being propagated further.
http://developer.android.com/reference/android/app/Activity.html#onKeyDown(int, android.view.KeyEvent)