In the android, FCM notification comes and app is in the background state and when tapped, it redirects to Default Activity (LoginActivity) but I want to navigate to specified activity (HomeActivity).
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "CHANNEL_1");
notification/*.setSound(soundUri)*/
.setSmallIcon(R.drawable.ic_photo_camera_black_24dp)
.setColor(getResources().getColor(R.color.colorAccent))
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message);
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
0);
notification.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification.build());
you will need to pass an activity parameter from the api/json part. from your app side you will check for the activity nave and than open accordingly
see this solution https://stackoverflow.com/a/39665485/6442746 for more insights.
You should send a format is Data Message to handle notification in onMessageReceived when the app is background or foreground
When received data in onMessageReceived(), you can show notification and direct to your activity.
Related
In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not.
Like As When i Click on onGoing Notification "Connected as a Media device"
You can define actions you want to happen when interacting with your Notification by adding a PendingIntent.
In the following example a PendingIntent is created to launch the (current) activity.
That intent is then added to the notification in the content section. Once this notification is shown, when you click the content section, the intent is fired and the app gets started or brought back to top.
private static final int NOTIFICATION_ID = 1;
...
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
nb.setSmallIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(getText(R.string.app_name))
.setContentText("Click to launch!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent) // Here the "onClick" Intent is added
.setOngoing(false);
nm.notify(NOTIFICATION_ID, nb.build());
In this case the notification is dismissable. If you set .setOngoing(true) you need to remove it by calling .cancel(NOTIFICATION_ID) on an instance of the NotificationManager.
See also this introduction on how to Build a Notification.
Is it possible not launch new activity if we receive a push notification while the app is running?
My activity works with fragments and I want to do transition to a determinate fragment when the notification is received. My activity have data that I need to show the fragments. The problem is that when I receive the push notification while the app is running the method onDestroy is called and here I clear the data and then the app crash because the data are null. How can I do to not create new activity when the app receive a push notification while is running? In case the app is running I want that if you click the notification do a transition fragment, not create again the activity.
Thanks in advance.
First of all, I think that you mean "notification" to be a "message", but not android.app.Notification class.
And second, I don't think it's a best practise to raise new GUI when receiving a message, which would interrupt the user interaction. For details, please refer to: http://developer.android.com/training/notify-user/index.html and http://developer.android.com/design/patterns/notifications.html.
At last, if you really wanna do what you stated in your thread, I wonder why the data used to generate the show-data fragment is held in the activity. Try holding the data in an android.app.IntentService object, and then generate transfer the data to new activity, and then use android.app.Fragment.setArguments method to transfer the data from activity to fragment.
I think that this code will help you. This which you need is PendingIntent, it make transaction to desired activity.
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String title,
String message) {
//get the default notification sound URI
Uri uriNotificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//make intent to the application. if the application is opened just maximize it.
Intent homeIntent = new Intent(context, 'your desired activity');
homeIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
homeIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("eCommCongress")
.setContentText(message)
.setLights(Color.GREEN, 1500, 1500)
.setSound(uriNotificationSound)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(counter, mBuilder.build());
counter++;
}
It is perfectly possible and I do such a thing in one of my apps. First, you need to declare your activity as android:launchMode="singleTop",
Then, when you build you must configure your pending intent not to fire a new instance of your activity:
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent("YOUR ACTION HERE");
intent.setClassName(this, MainActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(idNotificacion,notification);
Now all you have to do is to override your onNewIntent inside your Activity and do whatever you want with your fragment:
#Override
protected void onNewIntent(Intent intent) {
MiLog.i(getApplicationContext(),"IntentShit","new intent received");
MiLog.i(getApplicationContext(),"IntentShit","Action: "+intent.getAction());
if(intent.getAction()!=null && intent.getAction().equals("YOUR ACTION HERE"){
//DO your stuff here
}
}
You should also take a look at this page for more info:
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
I'm having this weird issue with notifications in Android. I'm creating notifications this way:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("data", value);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification updateComplete = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msg)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.icon_notifications)
.build();
notificationManager.notify(100, updateComplete);
When the app is running (or in teh background) and hte user clicks on the notification, everything works. onNewIntent is called and the data is in the extras. But when the app is not running or in the background, then onNewIntent() is NOT called. I tried to get the intent from onCreate() using getIntent(), but it never has extras. Is there anyway to get the extras when the app is not already running?
My activities are all singleTop.
Try set some Action on intent:
notificationIntent.setAction(Long.toString(System.currentTimeMillis()));
Then get Extras in onCreate.
You may also try replace PendingIntent.FLAG_UPDATE_CURRENT with 0.
My app is getting called by an intent that is passing information(pendingintent in statusbar) but when I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there...
is there any way to clear the intent? or check whether it has been used before?
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("reportID", reportID);
intent.putExtra("message", message);
intent.putExtra("toast_show", true);
intent.putExtra("detail_open", true);
intent.putExtra("SplashActivity", true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
// Intent.FLAG_ACTIVITY_NEW_TASK
/* get the system service that manage notification NotificationManager */
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(message)
.setContentTitle(title)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(message)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/* Create notification with builder */
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:
Does anyone knows how to keep the app in the "recent app" list after being started form a notification?
Update this line and try
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
What you need is the PendingIntent.FLAG_CANCEL_CURRENT :
If the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it.
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This will ensure that your latest data will pe present on your Intent.
I have a notification that have 2 actions. One intent open an Activity, the other open browser web.
my code:
Intent intent1 = new Intent(context, NotificationClickActivity.class);
PendingIntent pIntent1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
String url = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse(url));
PendingIntent pBrowserIntent = PendingIntent.getActivity(context, 1, browserIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Build notification
Notification noti = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(shortDescription)
.setStyle(new Notification.BigTextStyle().bigText(longDescription))
.setSmallIcon(R.drawable.small_defaulticon)
.setAutoCancel(true)
.setContentIntent(pIntent1)
.addAction(R.drawable.playstore_icon_32, "Dettagli", pIntent1)
.setContentIntent(pBrowserIntent)
.addAction(R.drawable.small_defaulticon, "Vai al sito", pBrowserIntent).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
If i click on the notification it goes away, but if I click on one of the 2 Actions it open the right intent but notification doesn't cleared.
I try also .setDeleteIntent(myPendingIntent) but nothing... I wonder where I am going wrong...
A screen of my notification...
Thank you!
A notification is never dismissed when an action button is pressed. It just sends you associated intent. If you want it to dismiss, you need to call NoficationManager.cancel(int id) to cancel it yourself. You will normally do it in the same method, which handles action button intent.
Notification.FLAG_AUTO_CANCEL flag you tried to use is only applied to notification body and not to action buttons.
use cancel() in the activity that started by your action to dismiss the notification
try this
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL);
and try to send uniqueId in notificationManager.notify(uniqueId, noti);