PendingIntent opens main activity instead of desired activity - android

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());

Related

Handling FCM Notification while running and killed

I am searching for a pattern for handling fcm notifications while the app is running, in background or killed.
Our app features a login process and therefor when the app is not launched, the notification needs to open the launcher, but when the app is running i want to just push an activity when the user clicks the notification.
So the best way i guess would be an ContentIntent that gets either delivered to the top most Activity in the Stack or delivered to the Launch-Activity.
My current code always relaunches the app with a TaskStackBuilder no matter if the app is running or not.
A sample of my code:
resultIntent = new Intent(this, MyProfileActivity.class);
resultIntent.putExtra(ActivityUtil.KEY_IMAGE_URL, SharedPref.getUserImage(this));
resultIntent.putExtra(KEY_NOTIFICATION_ID, notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SetDetailReworked.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "...")
.setLargeIcon(getBitmapFromUrl(images[UI.GetDensityInt(this)]))
.setSmallIcon(R.drawable.bar_icon)
.setContentTitle(getString(R.string.push_notification_Title))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(resultPendingIntent);
Notification not = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID, not);
Try to set your MyProfileActivity's launchMode as singleInstance in androidManifest,
<activity
android:name=".MyProfileActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
and it will not relaunches your background activity when user clicks the notification. Instead, MyProfileActivity's onNewIntent will be fired.

Do something when my app's notification is triggered

I'd like an activity of my app to listen when a specific notification is received and do something then.
The notification is triggered from within the app itself. I'm kind of new to Android development (and to development in general) so here's some pseudocode for what I want to do:
Activity{
notificationListener(){
if(notification is received){
//Do something
}
}
Any help would be much appreciated.
After checking the API of Notification, there seems no way to listen to sending Notification. After thinking deep, you would understand why we can't. AFAYK, it is we that write codes to send Notification but not the system, right? We surely know when we send Notifications, right? So, if you want to know when Notification is sent, why not do something, like sending BroadcastReceiver or starting Service to let other codes noticed? It all depends on our code-writers. And here is how to customize a 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());

Android Notification Click - don't backstack activity

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.

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?

Send an Android Notification before an Activity has been launched

EDIT: The problem below disappeared with no changes in code. The notifications started appearing properly upon later reboots. This is at most an intermittent issue.
I have an application that starts up a background service when the phone boots. After a certain event is detected, I would like to send a notification to the user, allowing the user to elect to start my MainActivity if the user taps on the notification.
The problem is that Android seems to disallow sending notifications if no Activity has yet been launched. The code below is in a custom android.app.Application class, and works only if the Activity has already been launched.
Is there any way to send a Notification before an Activity has been launched?
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Event Detected")
.setContentText("Tap to launch MainActivity")
.setSmallIcon(R.drawable.ic_launcher);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
Here is the error:
05-20 09:09:42.084: W/ContextImpl(801): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1505 com.android.server.StatusBarManagerService.sendNotification:981 com.android.server.StatusBarManagerService.addNotification:670 com.android.server.NotificationManagerService$7.run:2142 android.os.Handler.handleCallback:733

Categories

Resources