android -How to make notification disappear only when user clicked on it - android

this is my notification code:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Ticker Text";
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, FistActiivty.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
How can I make the notification disappears only when user clicked on it , I mean I don't want the notification disappear when user clicks on clear button
thanks

Use:
// This notification will not be cleared by swiping or by pressing "Clear all"
notification.flags |= Notification.FLAG_NO_CLEAR;
and also important:
.setOngoing(true) //put this inside your notification builder
or use: Notification.FLAG_ONGOING_EVENT; instead of .setOngoing(true)
This should help you out.

public static final int NOTIFICATION_ID = 1;
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, "your activity name"); ex:mainactivity.class
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setAutoCancel(true); // this method is use for disappear notification after tap on it .
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Related

Launch Homescreen on Notification Click

Here is my Notification Builder :
public void CustomNotification() {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);
// Set Notification Title
String strtitle = getString(R.string.customnotificationtitle);
// Set Notification Text
String strtext = getString(R.string.customnotificationtext);
Intent intent = new Intent(this, Main2Activity.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.mipmap.ic_launcher)
// Set Ticker Message
.setTicker(getString(R.string.customnotificationticker))
// Dismiss Notification
.setOngoing(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Images
remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright, R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.title, getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text, getString(R.string.customnotificationtext));
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
Now the question is :
Is it possible to launch homescreen (device home) when the notification is clicked (or the user is taken back to the homescreen when the notification is clicked) ? If yes, then How?
Hey use this code to this will solve your issue mention your Activity in pendingIntent.
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse(
"android.resource://"
+ context.getPackageName()
+ "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
here this the complete tutorial http://androidexample.com/Android_Push_Notifications_using_Google_Cloud_Messaging_GCM/index.php?view=article_discription&aid=119&aaid=139

update notification tickerText

i created a notification inside my background service
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Intent i = new Intent(this, MapsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pending = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
final Notification notification = new Notification.Builder(this)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setTicker("text")
.setContentTitle("text")
.setContentText("text")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pending)
.setWhen(when)
.setAutoCancel(true).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1000, notification);
startForeground(1000, notification);
after doing stuff i want to update the tickerText , the contentTitle and the contentText of my existing notification. Is it possible to do?

android status bar notyfication icon

I'm trying to set an icon to the status bar, I can not view it as soon as you click on the button.
The problem there is the option to delete. I want to set up so you will not be deleted as long as those entering the application and delete
public void setNotificationToStatusBar(){
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent, 0);
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
}
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
/** I ADD THIS **/ n.flags=Notification.FLAG_ONGOING_EVENT;
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent,0);
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
This is how I create a notification. Using setOngoing(true) I make it persistent
http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing(boolean)
Intent intent = new Intent("MY_INTENT");
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),0,intent,PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Click here")
.setSmallIcon(R.drawable.img2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img1))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
If you want to keep your own way of creating the notification and not use the Notification.Builder, you could add this line of code to edit the flag field of the notification
notification.flags |= Notification.FLAG_ONGOING_EVENT;
you can do like this too
int icon = R.drawable.icon_notification;
String msg= "hey";
Intent notificationIntent = new Intent(context,Activity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentIntent(intent)
.setPriority(PRIORITY_LOW)
.setContentText(msg)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());

using NotificationManager into an android service

I have a service running in the background and I want to notify user if there is a new data, I've used a notificationmanager, and the notification appears, but when clicking on it it doesn't do anything (it is supposed to show an activity
I'm new in android and here is my code
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = newNotification(R.drawable.shoppingcarticon, "Nouvelle notification de ShopAlert", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this,GooglemapActivity.class);
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title", "This is the text", activity);
notification.number += 1;
notificationmanager.notify(0, notification);
your help will be appreciated.
You can use this code. Change Main.Class to your activity class, And Title and content text as you need them.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_action_search;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification); // Log.d("test", "Saving Data to File from Service.");
The following line:
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
should look this way:
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
since you're trying to start an Activity, not a Service. Hope this helps.

How to show an icon in the status bar when application is running, including in the background?

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);

Categories

Resources