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.
Related
I have a few questions.
1. when I send a notice that it replaces the old.
I need to increment a counter notification.
how to do that when you click on the notification to open certain fragment?
private static void generateNotification(Context context, String message,int type) {
String title = null;
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
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;
notificationManager.notify(0, notification);
}
how to do that when you click on the notification to open certain fragment?
You cannot open Fragment directly. You need to open Activity that then will use that Fragment
Calculate the number if notification come before user last open n and increase the counter by this way.
notification.number += count(here is number of notification);`
Thanks
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'm trying to send 2 strings using Notification, but I don't know how should I do it. I used putExtra for my strings, but on the other activity I get null. I don't know what to do on the other activity. I read about this on net, but I didn't undersand. Can anyone help me?
Here is my code :
private void setNotifiy(){
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "You have a notification";
//CharSequence NotificationContent = ;
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);
notificationIntent.putExtra("latshop",lat_choose);
notificationIntent.putExtra("longshop", long_choose);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
In the class ShopsOnMap I wrote just this :
String q = getIntent().getStringExtra("latshop");
System.out.println("!!"+q+"$$");
and I obtain q=null.
I need a simple example, or any idea that could help me. Thanks.
Use FLAG_UPDATE_CURRENT in your getActivity() call, instead of 0, for the fourth parameter, so your new Intent extras are applied to the PendingIntent.
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.
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"