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.
Related
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());
In my program, just one notification shown. I want to show many notifications in the status bar. Here is my code.
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis(); // notification time
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, Goo.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
nm.notify(NOTIF_ID, notification);
I am working on an android app, and I am currently trying to create a notification, and once the user clicks on the notification, an activity should be launched. The notification is being created fine but the activity is not being started.
Below is the code for the notification.
private void showNotification(String username, String password)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(ns);
CharSequence tickerText = "Username Copied";
long when = System.currentTimeMillis();
int icon = R.drawable.icon;
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "BPM - Login Management";
CharSequence contentText = "Username Copied";
Intent intentNotification = new Intent(context, CopyPassword.class);
intentNotification.setAction(Long.toString(when));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
intentNotification.putExtra("password", password);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID, notification);
}
The notificiation is being generated from within a normal java class not an android activity in case this makes any difference.
Thanks for your help.
Remove the setAction this isn't needed.
Try:
Intent intentNotification = new Intent(context, CopyPassword.class);
intentNotification.putExtra("password", password);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Also ensure the CopyPassword Activity is declared in your AndroidManifest
I wrote a blog post about it: http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ :-D
I have spent a week to find a solution but with no success, so I need a help.
When my application goes to background then notification appears and displays status of activity.
It works perfectly if the application is started from launcher (with action = android.intent.action.MAIN and category = android.intent.category.LAUNCHER).
But if the application is started f.e. from the gallery using action android.intent.action.SEND or android.intent.action.SEND_MULTIPLE and category android.intent.category.DEFAULT, then notification starts new activity instead of resuming existing one.
The code for notification:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
Notification notification = new Notification(icon, null, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONLY_ALERT_ONCE;
Context context = this.getApplicationContext();
CharSequence contentTitle = "XXX";
CharSequence contentText = "XXX";
Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent activityIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, activityIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
In other words, I have an application "My Application" with activity "My Activity". If "My Activity" is started on the top of gallery and creates notification, then after pressing the notification "My application" is started instead of resuming the gallery with "My activity".
Do you have any idea how to heal it?
Intent myIntent = new Intent(this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
I've been attempting to get a notification of a successful upload from an ASyncTask to work all day. I'm not getting any errors from my current code but I can't get the notification to show in the notification bar (or anywhere else). I get no messages in LogCat and no notification appears in the Notification bar. This is my code:
Notification mNotification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "upload completed.";
CharSequence contentText = "upload completed.";
Intent notificationIntent = new Intent(context, CastrActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE);
mNotification.contentIntent = contentIntent;
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
This is called from the onPostExecute() method of an ASyncTask. I'm a bit confused on the PendingIntent part, to be honest. Any clarification of what I suspect to be incorrect code there would be greatly appreciated.
Even though your problem is solved, I'll just post how I solved my problem that the notification was not showing, perhaps it might help other people reading the answers:
In my notification building I was missing the icon. As soon as I added something like setSmallIcon(R.drawable.ic_launcher) the notification was shown.
I have created the class to show notifications:
public class NotificationData {
public static NotificationManager mNotificationManager;
public static int SIMPLE_NOTFICATION_ID;
private Context _context;
public NotificationData(Context context) {
_context = context;
}
public void clearNotification() {
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
}
public void SetNotification(int drawable, String msg, String action_string, Class cls) {
mNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(drawable, "Post Timer", System.currentTimeMillis());
long[] vibrate = { 100, 100, 200, 300 };
notifyDetails.vibrate = vibrate;
notifyDetails.ledARGB = 0xff00ff00;
notifyDetails.ledOnMS = 300;
notifyDetails.ledOffMS = 1000;
// notifyDetails.number=4;
notifyDetails.defaults =Notification.DEFAULT_ALL;
Context context = _context;
CharSequence contentTitle = msg;
CharSequence contentText = action_string;
Intent notifyIntent = new Intent(context, cls);
// Bundle bundle = new Bundle();
// bundle.putBoolean(AppConfig.IS_NOTIFICATION, true);
notifyIntent.putExtras(bundle);
PendingIntent intent = PendingIntent.getActivity(_context, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
}
How to use this class:
NotificationData notification; //create object
notification = new NotificationData(this);
notification.SetNotification(R.drawable.notification, "Notification Title", "Click to open", YourClassName.class);
Add permission android.permission.VIBRATE
Try this:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon; // icon from resources
CharSequence tickerText = "Any thing"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context21 = getApplicationContext(); // application Context
CharSequence contentTitle = "Anything"; // expanded message title
CharSequence contentText = (CharSequence) extras.get("message"); // expanded message text
Intent notificationIntent = new Intent(this, MainStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
/* long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;*/
notification.setLatestEventInfo(context21, contentTitle, contentText, contentIntent);
mNotificationManager.notify(Constants.NOTIFICATION_ID, notification);
Another thing to try is to make sure your manifest contains
<permission android:name="android.permission.STATUS_BAR_SERVICE" android:protectionLevel="signature" />
Also mine seemed to ignore successive notifications with the same NOTIFICATION_ID.
For me this kept happening and I had no idea why but the problem was that the icon I set was too large so it was giving me some random error.