My android notification disappears after closing my app :/ Why? I want to create a persistant notification that will disappear after clicking on it, not after closing the application that creates this notification. Someone can help?
MainActivity class:
private void createNotification() {
Intent intent = new Intent(this, eTutorActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("xxx")
.setContentText("xxx")
.setTicker("xxx")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setLargeIcon(icon)
.setOngoing(true)
.setAutoCancel(true)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
MainActivity class onCreate action:
protected void onCreate(Bundle savedInstanceState) {
..
createNotification()
..
}
EDIT:
Nexus 7 Android 5.1 is working correctly
Nexus 5 Android 6.0.1 is working correctly too
Xioami Redmi Note 2 Android 5.0.2 with MIUI 7.3.2.0 is NOT working and notification disappears after killing application.
Use this :
noti.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(0, noti);
hope it helps!!!
Related
Hello I am developing an app in which i want to give push notifications to user when Sms receives. Now the problem is that the notification icon still does not remove when app open by user through launcher icon
Here is my code:
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.appicon);
notify.setContentTitle(title);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
Notification notification = new Notification();
notification.defaults |= Notification.DEFAULT_VIBRATE;
//notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
I tried like this too:
notificationManager.cancel(0);
and also tried this:
notificationManager.cancelAll();
but both these doesnot work. They doesnot allow notification to occur. Maybe they are cancelling the push notification before its creation.
Please Help!
Just cancel all the notifications inside the main Activity's onCreate() method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
That is because you asked
Remove notification icon when user open app through launcher Icon
But best way is to put it in onResume() so that when the app is in background you want to show the notification and remove it when user brings it to foreground.
#Override
protected void onResume() {
super.onResume();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
I am trying to launch an activity, when push notification is clicked.
Interestingly, when the app is closed, and no activity is in background, and I recieve a push, I click on it and the required activity is opened.
But when any activity of the app is opened, and I recieve a push, clicking on it does nothing.
I have tried diff combinations. Also put exported = true on the launching activity.
Here is my code :
private static void showNotification(Context context,String title,String text,Intent openIntent)
{
PendingIntent pi = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_ONE_SHOT);
// Resources r = getResources();
final Notification notification = new NotificationCompat.Builder(context)
.setTicker(title)
.setSmallIcon(R.drawable.notif_icon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.notif_icon))
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pi)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
.setAutoCancel(false)
.build();
// also tried .setAutoCancel(true) above instead of cancel
notification.flags |= Notification.FLAG_AUTO_CANCEL;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
notificationManager.notify(0, notification);
}
},1000);
}
Now, what combination should I try ? Remove the flags CANCEL_CURRENT or FLAG_ONE_SHOT ? Tried already. Even without these two flags there was no result on clicking of notification.
I am running the app on android 5.1 / 5.0 . I have read that there are problems in the notification on these android versions. So I already tried the solutions like android:exported=true and FLAG_CANCEL current. But to no effect.
Now how to make the notification click work and open the intended activity ?
Any help is appreciated.
Thanks
Use FLAG_UPDATE_CURRENT like this. its work for me
PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
You need to broadcast data from your FirebaseMessageingService class
public void onMessageReceived(RemoteMessage remoteMessage)
and receive the broadcast where-ever you want to show the data.
/*I tried this code on asus zenfone 2 (android 5.0), it is working without any problem please try this.*/
private void pushNotificationWithClickAction(String mainTittle,String subTittle,Intent intent){
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle(mainTittle)
.setContentText(subTittle)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent).build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
i want to create a notification that user can't close it by drag to left or right.
user must click o that to close notification
my code is :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.ic_tt)
.setLargeIcon( BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_tt))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setOngoing(true).build();
n.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
but it dosnot work
any idea ?
You need to use both functions to retain the notification,
nBuilder.setOngoing(true);
nBuilder.setAutoCancel(true);
It can be cancelled only when user taps on it.
Find setAutoCancel in this Managing Notifications.
Hope this helped!
I have a class that implements IntentService that retrieve some data from a webservice, and if a condition is verified, show notification on notification bar's device.
This is the code:
public BackgroundService() {
super("SERVICENAME");
}
#Override
protected void onHandleIntent(Intent intent) {
if (verifyConditions()) {
showNotification();
}
}
private void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.notification_icon);
// .setContentIntent(pendingIntent);
Intent notificationIntent = new Intent(this.getApplicationContext(),
SplashScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
// set sound
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setContentIntent(contentIntent);
Notification notification = builder.build();
// Hide the notification after it's selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
Now if user tap on notification, SplashScreen activity is launched (it's first activity of my app). this is ok if user has close the application, but if application is in foreground, tapping on notification cause restart of app.
There's a fast way to check if app is in foreground, and if yes, launch a different activity thant SplashScreen?
Hope this will help. You check your app is running foreground or not and according to that change intent for PendingIntent visit StackOverFlow : check android application is in foreground or not?
The following is the function responsible for handling a push notification received by my app. It is supposed to redirect the user to a certain Activity in the app after he/she clicks clicks on the notification.
When I try running the app on my Android 2.3.6(API 10) device,the notifications are handled perfectly.
However,when I try running it on an AVD( Andriod 4.3 (API 18)),there is no effect- the default android logo is displayed as icon instead of ic_launcher.png, the user is not redirected to the app on click,the notification does not autohide on clicking.
Please note,that I am using if statements to check API version of the phone on which the app is installed so as to ensure that push notifications are not affected by deprecated classes.
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
public static void createNotification(Context context, String payload) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11) {
Intent intent = new Intent(context, tabswipe.MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(context)
.setContentTitle("New mail from ggg")
.setContentText("zzzz")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Call", pendingIntent)
.addAction(R.drawable.ic_launcher, "More", pendingIntent)
.build();
notificationManager.notify(0, n);
}
if (currentapiVersion < 11) {
Intent intent = new Intent(context, tabswipe.MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"New Update from VIIT Perception", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL; // Hide the notification after its selected
notification.defaults |= Notification.DEFAULT_LIGHTS; //adding LED lights to notification
notification.setLatestEventInfo(context, "Updates from VIIT Perception", payload, pendingIntent);
notificationManager.notify(0, notification);
}
}