Multiple Notification android app - android

I'm building an app in which the user puts multiple alarms at specific date and time and takes a notification for each alarm, when the time comes. For each notification there is a different id number. My problem is that when I put 2 alarms, the newer will work but the older one won't. It's like the second alarm kills the first..According to some tutorials and forums,given that I use unique id's that shouldn't happen, right?
Here is my code of Notification Service:
DataBase db = new DataBase(getApplicationContext());
String message = db.getTask(id);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(R.drawable.ic_launcher, "You have message", System.currentTimeMillis());
notification.setLatestEventInfo(this, "You 've got something to do", message, pendingIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(id, notification);
}
Database is using the id's to get the right messages for each notification..And it works fine..So, I think there is no problem with the id. Any ideas?? Could anything be wrong with my manifest file??

For anyone who may have the same problem, I found the solution. Actually the problem was at my alarm manager code. I didn't set the ids as a parameter in the pending intent, like I do in the above code. Now it works fine.

Related

Get all Notifications in onResume

I don't have any experience with notifications and after a long time now, I'm feeling really stupid, that I can't make it:
I'm getting push notifications from my server. On click of one, I'm getting the inormations with
getIntent().getBundleExtra("extra");
in the onResume method.
But if I'm getting a lot of notifications and don't open my application between this, then I'm just getting the last, if I click on the notification. Where are the notifications before? The best would be, if I'm getting a list of bundles or something like this. How can I do that?
Thanks for help, I'm really depressed :)
EDIT:
For better understanding: After receive a GCM I'm make a notification like this:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, title, System.currentTimeMillis());
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (extras != null && launchIntent != null) {
launchIntent.putExtra("extra", extras);
}
PendingIntent intent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
Example: I get a GCM and set a notification. If I don't open my application, but get the next GCM, then my first notification will replaced by the new one. This could be many times. After a few notifications i click on it. If the app starts I will get all the notifications, which has been sent. At the moment I just get the last one. How can I get all?
mNotificationManager.notify(0/* 0 is your notification id */, notification);
creates a notification with id 0. Now when you receive another notification and you notify it with the same notificaionId (i.e 0). Hence the latter pending Intent replaces the previous pendingIntent.
Check the explanation for notify() in the docs =>
http://developer.android.com/reference/android/app/NotificationManager.html
The best option would be to save your data locally in some DB, preferences or the traditional way
File and whenever you receive a push notification from GCM save it locally and then notify.

How to disable multiple push Notification icon in Android, Am using Parse.com

com push notification for Android app, All is working well but lets say if i send 5 push notification a day, i dont want user to see 5 notification icons at once on his phone. Its best if he sees only one.So is their any way where old notification is deleted and only latest is shown.
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
The method that displays the notification has an int parameter which represents the notification identifier. If you use a constant identifier, each new notification will replace the previous one.
you have to set the notification ID to be the same value. So then they get replaced as each one arrives.
int notificationId = 1 ;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.gcm_logo);
builder.setContentTitle("Test Title");
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());
So you can maintain notification Id for different type of notifications. If you keep same notification ID for each notification then it would be updated in the same .
If you have any extras in the pending intent and you want to update those extras with the latest ones then generate pending intent with :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(),
PendingIntent.FLAG_ONE_SHOT);

How do I keep my Android notification displayed until my app is closed?

I've been developing for Android for awhile but this is my first shot at notifications. I've got my notification setup as described in the Android SDK tutorial, but I can't figure out how to keep the notification displayed until my app is closed. I want to disable that little minus sign at the end of the notification. I don't want my notification to disappear when a user clicks it. I would think there would be a notification flag... but I can't seem to figure this out. I'm developing on Android SDK 2.2. I know this is a simple question, and I apologize if this answer is already on here... I wasn't able to find exactly what I was looking for.
// Create notification manager
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, "Ready", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Make a notification
notification.setLatestEventInfo(getApplicationContext(), "Ready", "Select to manage your settings", contentIntent);
mNotificationManager.notify(0, notification);
You want FLAG_ONGOING_EVENT. Also try removing FLAG_NO_CLEAR and FLAG_AUTO_CANCEL if they are part of the defaults.

Android Delayed Notification

I am trying to create a Notification using Android's Notification Manager, however, the trick is that I want the notification to show up 30 days in the future. In my code I'm doing this:
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
long when = System.currentTimeMillis() + (30 * 24 * 3600 * 1000);
Notification notification = new Notification(R.drawable.some_image, "A title", when);
notification.setLatestEventInfo(getApplicationContext(), "You're late", "Some description", contentIntent);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFY_ATTEND_ID, notification);
However, the notification is still showing up instantaneously. From what I read, the "when" parameter to the Notification constructor is only used to sort the notifications in the StatusBar. Is there anyway to make the notification show up in at a future date/time? Thanks in advance.
Is there anyway to make the notification show up in at a future date/time?
No.
As Falmarri suggests, you will need to handle this yourself, though I disagree with his approach. You will need to use AlarmManager. However, I am skeptical that AlarmManager will work for 30-day durations, though you can try it. You may need to use AlarmManager for a daily/weekly task to schedule that day's/week's notifications via separate alarms. You will also need to reconstitute this roster of alarms on a reboot, since they get wiped, as Falmarri suggests.

How to check which notifications are active in status bar in Android Dev?

I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).
How can I do this? (Thanks in advance).
Sample code is greatly appreciated.
I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see
the notification in the status bar?).
How can I do this?
You can't, sorry. Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.
EDIT:
NotificationManager.getActiveNotifications() was added in API 23 if you don't want to use the NotificationListenerService
Just to put all together. This is how it works
To build a notification,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSmallIcon(R.drawable.myicon).build();
To make a notification sound call setSound() of Notification,
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setSmallIcon(R.drawable.myicon).build();
To cancel the notification after user selected and launched the receiver Intent, call setAutoCancel(),
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setAutoCancel(true)
.setSmallIcon(R.drawable.myicon).build();
To make sound/vibrate only once for a particular notification use Notification.FLAG_ONLY_ALERT_ONCE. With this flag, your notification will make sound only once till it gets cancelled and you can call notify() as many times as you want with the notification id. Note that if you call cancel() or if user cancelled the notification or auto cancelled, notify() call will make the notification sound again.
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound
Finally to put the notification on notification panel,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, n);
Note that notification_id here is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).
To cancel a particular notification,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
You can cancel() a notification even if it doesn't exist or you can call notify() as many times as you want with the same id. Note that calling notify with different id will create new notifications.
So, regardless of whether the notification exist or not, if you call notify() again with the correct notification_id with the Notification.FLAG_ONLY_ALERT_ONCE flag set, you can keep your notification alive without disturbing the user with repeated sounds.
You need to set an id for each notification you make.
so you make a notification ..
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);
to clear it..
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0);
pendingIntent.cancel();
and to check if active..( existAlarm returns null if no pending intent available)
public PendingIntent existAlarm(int id) {
Intent intent = new Intent(this, alarmreceiver.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
return test;
}
So everything comes down to initialize an ID for each notification and how you make it unique.
A new method is introduced to the NotificationManager class in API 23:
public StatusBarNotification[] getActiveNotifications()
There exists a flag for that.
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
FLAG_ONLY_ALERT_ONCE:
...should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
Although, the notification will blink when it is sent again, but there won't be any sound or vibration.
It's possible now to check notifications outstanding in android 4.3 upwards
See here:
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
It seems that from Android M (API 23) it is possible to get your process like that, without using NotificationListenerService nor requiring additional permissions:
notificationManager.getActiveNotifications()
As of Android Marshmallow (API 23), you can recover a list of active notifications posted by your app. This NotificationManager method is getActiveNotifications(). More info here: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()

Categories

Resources