Several issues with opening android notification - android

Hi all I have several isssues with my android notification. my code is the following:
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("at_now", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK|
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(name)
.setVibrate(new long[]{500, 500, 1000})
.setLights(Color.RED, 500, 1000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setStyle(new NotificationCompat.InboxStyle()
.setBigContentTitle(name)
.addLine(content_txt)
.addLine(mesagge)
).setContentText(content_txt);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Session.getInstance(getApplicationContext()).getSharedPreferences().edit().putBoolean("REFRESHNOW",true).commit();
But it has several bugs.
1.) It opens every time new instance of the application if i do something and i get notification. Later when I close the app which was opened after clicking the notification, the previous screen appears, even if it is the same activity.
2.) if i recieve notification when my application is not opened it works fine. after i close the application I get back to my system menu. it is ok!
3.)When i opened application by clicking to notification, and if i get after another notification, it is no way it opens MainActivity it just bring forward the application, even if i am not in the main activity.
4.) the notification message does not disappear after clicking on it.
I have added these lines to my manifest as well:
android:noHistory="true"
android:launchMode = "singleTop"
5.)at main activity after opening it from intent i have extras like these : . So even the extras which i put to the intent does not work.

In NotificationGcmIntentService class use below code to clear notification . NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setAutoCancel(true);

Related

how to handle FCM background state in the android

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.

In Android How to Restart App On Click Application "onGoing" Notification. if App is open or not

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.

Android notifications when app isn't in background

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.

android clear intent which send from notification

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.

Notification not automatically cancel when addAction

I have tested the new BigTextStyle Notification which display correctly. However when I add action to the notification, it never get cancel and the pull-down notification area is never close when click on the action. What did I do wrong?
Drawable d = getResources().getDrawable(android.R.drawable.ic_menu_add);
Bitmap b = ((BitmapDrawable)d).getBitmap();
Notification noti = null;
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
String s = "Name: John \n"
+ "Surname: Doe\n"
+ "Vehicle: Honda Jazz TA-1234\n"
+ "Note: Good drifter";
Builder builder = new Notification.Builder(MainActivity.this)
.setContentTitle("New challenger arrived!")
.setContentText(s)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(b)
.setTicker("New challenger arrived!")
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setNumber(5)
.addAction(
android.R.drawable.ic_menu_add,
"Accept",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_menu_delete,
"Refuse",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_dialog_map,
"Map",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, null));
noti = new Notification.BigTextStyle(builder)
.setBigContentTitle("New challenger arrived!")
.bigText(s)
.setSummaryText("You will never win against me").build();
NotificationManager nm = (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
I think you have to manually cancel the notifications if you take an action. This can be done by calling
notifMgr.cancel(0);
Clicking the notification itself should dismiss it, but for some reason actions don't seem to. The reason you pass 0 as the argument is becasue that's the ID you gave when you called:
notifMgr.notify(0, noti);
That's my updated answer, however, I had previously answered this:
It looks like you're trying to launch the same activity (from your PendingIntent) that you currently have displayed (I'm assuming this is a 1-activity app, with MainActivity containing the code above). Create another activity, build your PendingIntent so it launches that activity, then it should do as you wish. Alternatively, run your activity, notification appears, navigate to homescreen, or different app, then act on your Activity, your desired behavior should manifest.

Categories

Resources