My pending intent not working as to be expected to work.
Issue is: When tapping on Notification it redirects to Splash Activity that is my launcher Activity of application, following is my code for creating notification.
Intent intent = new Intent(this, QuickTasks.class).putExtra("NOTIFICATION", 1);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Suvi")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
Please help me in this regard i have used many of resources available here, but none of worked. Any help could be appreciable.
Generally, when you want to start an activity from a notification, there is two cases:
Regular Activity:
You're starting an Activity that's part of the application's normal workflow.
Special Activity:
The user only sees this Activity if it's started from a notification. In a sense, the Activity extends the notification by providing information that would be hard to display in the notification itself.
Please refer to the following link.
Preserving Navigation when Starting an Activity
I hope this helps you.
Related
whenever i attempt to update the notification or Intent a new one is created instead which i don't want. I think the problem is that the Intents are not being identified whenever i'm trying to update it. The link to the code is below:
https://gist.github.com/Kerron-Hutton/03799ac497128919225bbbd4efaffa25
To update a PendingIntent, you should use the same REQUEST_CODE that you used before to create it.
PendingIntent pIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
To cancel/delete an existing PendingIntent, you can use PendingIntent.cancel() method.
PendingIntent.getBroadcast(context, REQUEST_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT).cancel();
To update a Notification, you can use NotificationManager.notify() method with the NOTIFICATION_ID that you want to update.
NotificationManager notificationManager= (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Notification id should be unique within your application. If a
notification with the same id has already been posted by your
application and has not yet been canceled, it will be replaced by the
updated information.
To cancel/remove an existing Notification, use NotificationManager.Cancel() method with the NOTIFICATION_ID that you want to cancel.
notificationManager.cancel(NOTIFICATION_ID);
Hope this will help~
My application contains multiple activities. I have implemented push notifications and also shown the notification in bar. My issue is, when i click on notification is take me to the specific activity that i has specified.
Intent intent =new Intent(GcmService.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(GcmService.this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
getApplicationContext())
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
notificationBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify((int) when, notificationBuilder.build());
I want if my activity is in background, and user click on the notification app resume the current activity that is in background and show dialog box.
And if my application is closed. open the Launching activity and then show the dialog box.
If you want to continue again when you click the notification(in this case your application still running on background) than you can using this method :
http://developer.android.com/training/basics/activity-lifecycle/stopping.html
I've tried it and it works.
You can link your notification to a DispatcherActivity.
If you have open Activities on the backstack, finish the DispatcherActivity suddenly in onCreate(). If not, forward to your launching activity and finish the DispatcherActivity too.
To track your active activities on backstack use this How to know Activity Count of my application? suggestions.
I am in trouble with notification intents. I have a service(Service checks for messages and creates notification) which creates notifications. And application has an action bar and via slide menu users can navigate between activities.
http://i.stack.imgur.com/YJXe6.png
When user clicks notification it opens a new activity on the current activity.(Like an independent new instance). I want to open them in the same instance as if user navigating manually(Like clicking A when on B activity ie)
My current activity launchMode is standard(Although I tried singleTop and singleTask and flags)
Current Notification code :
Intent i = null;
i = new Intent(this, MessagesListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,i, 0);
Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(msg);
notificationBuilder.setSmallIcon(R.drawable.speech_bubble_orange);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = notificationBuilder.build();
notificationManager.notify(Constants.UNREADMESSAGESNOTIFICATIONID,notification);
Thanks for your help.
I solved the problem. With this combination new launched activity clears stackback. I also did not change the launchmodes (still standard)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
In your manifest, you have to tag your activity as single instance, and keep the singleTask
android:launchMode= "singleTask" | "singleInstance"
in addition, you may have to remove the single top flag from your intent.
If I understood correctly, I think what you are looking for is the TaskStackBuilder. It allows you to create a backstack to provide proper navigation to the Activity being launched by the PendingIntent.
See the docs here for more information.
I am developing a music player however when I click the notification which is created, a new version of this activity is displayed and not the one displaying the currently playing song and seekbar.
The current code I'm using for the notification is
Intent i = new Intent(this, AmplitudeMusicPlayer.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0 , i, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder noti = new NotificationCompat.Builder(this);
noti.setContentTitle("Now Playing")
.setContentText(songTitle)
.setSmallIcon(R.drawable.default_art)
.setContentIntent(pIntent);
notificationManager.notify(0, noti.build());
Which launch mode are you declaring for your activity?
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
The "standard" and "singleTop" modes differ from each other in just
one respect: Every time there's a new intent for a "standard"
activity, a new instance of the class is created to respond to that
intent. Each instance handles a single intent. Similarly, a new
instance of a "singleTop" activity may also be created to handle a new
intent. However, if the target task already has an existing instance
of the activity at the top of its stack, that instance will receive
the new intent (in an onNewIntent() call); a new instance is not
created. In other circumstances — for example, if an existing instance
of the "singleTop" activity is in the target task, but not at the top
of the stack, or if it's at the top of a stack, but not in the target
task — a new instance would be created and pushed on the stack.
In my application I notify the user with notifications, if something special happens:
public void triggerNotification(String msg) {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent contentIntent = new Intent(this, ABC.class);
Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
notification.setLatestEventInfo(this, "ABC", msg, PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationCounter, notification);
notificationCounter++;
}
If the user clicks on the Notification, the onCreate() method is called. But I want that a specific method in my app is called, or if the app is not in the foreground, that it is brought back to the foreground.
I know there are lots of tutorials that explain how to handle notifications, but I just don't understand them completely and wasn't ever able to implement the things like I'd like to.
To bring your app to the foreground if it is running already you need to set different flags on your intent:
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.
The recommendation to use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP only partially solves the problem. The activity in the Android manifest should also have these settings applied so that launching the activity from the home screen has the same behavior. Without these properties multiple instances of the activity can be launched.
<activity android:name="foo"
android:clearTaskOnLaunch="true"
android:launchMode="singleTop"
android:label="#string/app_name">
I've discovered that if you use Intent contentIntent = new Intent(this, ABC.class); this calls onCreate(); regardless of the flags set.
Use Intent contentIntent = getIntent(); to skip onCreate(); and that moves to onStart();