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();
}
Related
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!!!
I have tried setAutocancel as well as FLAG_AUTO_CANCEL but none of these work and i have two buttons inside notification which open two diff activities.. kindly help me
public void createNotification(View view) {
Intent yesIntent = new Intent(this, MyDialog.class);
yesIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
yesIntent.putExtra("ACTION", 1);
PendingIntent yespIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, 0);
Intent noIntent = new Intent(this, MyDialog.class);
noIntent.putExtra("ACTION", 0);
PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder (this)
.setContentTitle("New Project Approval")
.setContentText("Project Description")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.bell)
.addAction(R.mipmap.approve_ic, "Approve", yespIntent)
.addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, noti.build());
}
You have to cancel your notification manually in your activity. Pass the notification ID to your PendingIntent to retrieve it in your Activity and cancel the corresponding notification via the NotificationManager like :
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(yourNotificationId);
A good and full example : How to dismiss notification after action has been clicked
Previously I callied the NotificationManager outside the onCreate() method, which wasn't working. But following the StackOverflow threadr suggested by #bubu I resolved it by calling NotificationManager inside onCreate();
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?
I use the following code to show the notifications
private void SendNotification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage,
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notif_id", "5100");
//Setting the single top property of the notification and intent.
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify((int)System.currentTimeMillis(), notification);
}
Each time a new notification comes, the previous one should be removed and the new one should be shown.
Any clues will be helpful.
Thanx in advance
o.
You are generatting a notification with different id each time (System.currentTimeMillis()), change it for a single ID.
notificationManager.notify(0, notification);
Post a notification to be shown in the status bar. 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.
NotificationManager.html
There is one way that you should clear all Notification of your Application before you post a newer one.
Give it a shot like,
notificationManager.cancelAll();
To clear a particular Notification you can do it like,
notificationManager.cancel(notification_id);
I want to put an icon in the status bar when ever my application is running, including when it is running in the background. How can I do this?
You should be able to do this with Notification and the NotificationManager. However getting a guaranteed way to know when your application is not running is the hard part.
You can get the basic functionality of what you are desiring by doing something like:
Notification notification = new Notification(R.drawable.your_app_icon,
R.string.name_of_your_app,
System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR
| Notification.FLAG_ONGOING_EVENT;
NotificationManager notifier = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notifier.notify(1, notification);
This code must be somewhere where you are sure will get fired when your application starts. Possibly in your application's custom Application Object's onCreate() method.
However after that things are tricky. The killing of the application can happen at anytime. So you can try to put something in the onTerminate() of the Application class too, but it's not guaranteed to be called.
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
will be what is needed to remove the icon.
For new API you can use NotificationCompat.Builder -
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title");
Intent resultIntent = new Intent(this, MyActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(NOTIFICATION_ID, notification);
It will show as long as your application is running and someone manually closes your application. You can always cancel your notification by calling -
mNotifyMgr.cancel(NOTIFICATION_ID);
Take a look at the Dev Guide "Creating Status Bar Notifications".
One way to achieve the goal of keeping the icon there only when the application is running is to initialize the notification in onCreate() and call cancel(int) in your onPause() method only if isFinishing() returns true.
An example:
private static final int NOTIFICATION_EX = 1;
private NotificationManager notificationManager;
#Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle,
contentText, contentIntent);
notificationManager.notify(NOTIFICATION_EX, notification);
}
#Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
notificationManager.cancel(NOTIFICATION_EX);
}
}
It really works.
I created a method out of the example above:
private void applyStatusBar(String iconTitle, int notificationId) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(iconTitle);
Intent resultIntent = new Intent(this, ActMain.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationId, notification);}
It should be called like: applyStatusBar("Statusbar Test", 10);