Below function displays a notification which pending intents. When the screen is unlock and receive notification, below method will create a notification.
When user tap on notification, it opens first ChildActivity, On backpress, it closes child activity and opens MainActivity->subActivity. on backpress closed subActivity and shows MainActivity.
Subactivity will be started based on the Intent action of MainActivity
In the unlock screen, below method gives above result and its perfect as per my requirement.
public void showNotification(Context context, String extraUri){
final Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra(MainActivity.INTENT_EXTRA_ACTIVE, 1232123);
// this will help determine to start subactivity from mainActivity
mainActivityIntent.setAction(MainActivity.MAIN_ACTIVITY_CHANGED);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addNextIntent(mainActivityIntent);
final Intent intent = new Intent(context, ChildActivity.class);
intent.putExtra(ChildActivity.EXTRA_URI, extraUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
taskStackBuilder.addNextIntent(intent);
final PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Message Body")
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Problem
while screen is locked and tap on the notification, it ask to unlock the screen, on unlock, it starts the app. but
very first it shows SubActivity, than on back press it shows ChildActivity and on backpress of ChildActivity it shows MainActivity.
So the order of the Activities are changed.
the correct order is
MainActivity->SubActivity->ChildActivity (TopMost) (happens in unlocked screen)
Thanks in Advance.
Related
Currently I have this:
Intent resultIntent = new Intent(context, DigestPager.class);
and this is opening DigestPager activity (which is not launcher activity) when clicking on a single notification.
But when there are multiple notifications and they are grouped into one and collapsed, clicking on it is opening launcher activity.
Is there some simple way to open a different activity?
Intent resultIntent = new Intent(context, DigestPager.class);
resultIntent.putExtra("digestId", digest.getDigestId());
PendingIntent pendingIntent = PendingIntent.getActivity(this, digest.getDigestId(), resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(digest.getDigestId(), mBuilder.build());
Create a handler activity that opens everytime you click on a notification and based on the flags set to the notification, you can navigate to the required activity/fragment.
Hope this helps.
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.
I am creating notifications with following code:
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(messageBody)
.setAutoCancel(true)
.setSmallIcon(R.drawable.top_icon)
.setContentText(senderName)
.setTicker(senderName+" ")
.setSound(soundUri);
Intent resultIntent = new Intent(this, LoginActivity.class);
resultIntent.putExtra("gcm_username",senderName);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NotificationID.getID(senderName), mBuilder.build());
When user clicks the notification I am catching it with following code in loginActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
gcmUserId= getIntent().getStringExtra("gcm_username");
startAction(gcmUserId);
....
My problem starts here.
Scenario:
1)When app is closed user receives notifications from 2 different users
2)User clicks first notification and app starts then startAction method calls.
3)Then user clicks the second notification
But when user clicks the second notification app has already started so startAction won't be able to call again because it is in the onCreate method.How can catch second notification ?
You can handle it inside onNewIntent(). You would need to override that method inside your activity. The docs say:
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
In my Android app I send Notification like that:
Intent intent = new Intent(ctx, MyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, myActivityId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int icon = R.drawable.ic_launcher;
Notification n = new Notification.Builder(ctx)
.setContentTitle(messageTitle)
.setContentText(messageSubtitle)
.setSmallIcon(icon)
.setContentIntent(pIntent)
.setAutoCancel(true).getNotification();
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(myActivityId, n);
Now when I tap on notification I go to MyActivity activity. I usualy send couple notification at once. When I tap on first notification and then immediately tap on second notification I will have 2 views in my views stack. I mean when I tap back on my phone from second notification activity I'm back to first notification activity. I want to second notification activity replace first notifitaction activity that when I tap back I go to view which was displayed before I started taping on notifications. How can I do that?
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.