The following code gives the same content after I send two notifications with different content say Content1 and Content2. The resulting activity always shows only Content2. What could be the reason for it?
public void onReceive(Context context, Intent intent) {
abortBroadcast();
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = intent.getStringExtra("NOTIFICATION_TITLE");
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = intent.getStringExtra("NOTIFICATION_TITLE");
CharSequence contentText = intent.getStringExtra("NOTIFICATION_DETAILS");
Intent notificationIntent = new Intent(context,CustomActivity.class);
notificationIntent.putExtra("TITLE", contentTitle);
notificationIntent.putExtra("DETAILS", contentText);
//notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(notifUUID.hashCode(), notification);
}
Got the answer! the fix was simple - just added:
notificationIntent.setAction(String.valueOf(notifUUID.hashCode()));
Any unique value (timestamp would have worked too) set as the intent action would work.
Related
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.
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'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.
I have 2 StatusBar Notification in my app. These notifications appear in the same time in app, and I can see only the last notification. And, if I press the Button Clear, both of them are cleared. How to work with more than one notification in android?
The second question is how to display the text of notification on multiple rows. I can see only the beginning of the text.
Here is my code :
for the first notification :
private void setNotifiy() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "You have a notification";
CharSequence contentTitle = "You are close to " + name_shop + "!!!";
CharSequence contentText = "So,you can go for shopping:)";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, ShopsOnMap.class);
System.out.println("DDDDd" + lat_choose + "!!!");
notificationIntent.putExtra("latshop", lat_choose);
notificationIntent.putExtra("longshop", long_choose);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
and for second notification :
private void hey(String list) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "Hey";
CharSequence contentTitle = "Shopping!!";
CharSequence contentText = "Today,you must go for shopping for your list ' "
+ list + "'" + "Don't forget!!!";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);
Context context = getApplicationContext();
Intent notifyIntent = new Intent(context, Lists.class);
PendingIntent intent =
PendingIntent.getActivity(Lists.this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, contentTitle, contentText, intent);
notificationManager.notify(NOTIFICATION_ID, notification);
notificationManager.notify(NOTIFICATION_ID, notification);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
I see your codes are using the same ID for both notifications.
As you might know, the answer is already on this documentation:
Creating Status Bar Notifications.
You may focus on two things covered on the documentation:
Notification ID. This is useful for creating a different notifications.
"Creating a Custom Extended View" section to create notification in multiple rows.
I am trying to program my notification to RESUME my app, instead of simply starting a new instance of my app... I am basically looking for it to do the same thing as when the Home button is long-pressed and the app is resumed from there.
Here is what I am currently doing:
void notifyme(String string){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = string + " Program Running..."; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = *********; // expanded message title
CharSequence contentText = string + " Program Running...";//expanded msg text
Intent notificationIntent = new Intent(this, Main.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.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
I am guessing that the new Intent line is where the problem lies... any help would be appreciated!
you need to set your flags
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Also, if you never ever want there to be a duplicate activity give it this attribute in the manifest
android:launchMode="singleTask"