How to manage multiple notification in service using alarm manager - android

I have number of toggles like, 2 minute, 4 minute and 24 hours. if user select any one, the service will be start and set repeat time in alarm manager, the Notification will showing for particular selected time.
Now the problem is i want to show the number of title like for 2 minute reminder, 4 minute reminder and 24 hours reminder. how to manage these all the titles in local notification service and manage click event.
My code is How to keep service alive when remove app from stack
Please suggest to find solution.

In the pendingIntent for your AlarmManager you can put extras and you can pass title to your MyReceiver activity like following.
Intent myIntent = new Intent(NotificationDemo.this, MyReceiver.class);
myIntent.putExtra("title","2 minutes");
pendingIntent =
PendingIntent.getBroadcast(NotificationDemo.this, 0, myIntent, 0);

All the notifications will have a unique id so you can distinguish them on the basis of their id in the Receiver class. All these must be assigned a unique id when they are created to distinguish between them.
You can also define type of notification, so that you will be able to cast different types of notifications with different push messages and its parameters.
The answer to the question you asked in the comment that to set title of a notification :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setContentTitle("Title of Notification");

Related

Can AlarmManager schedule more than one alarm from the same code?

If I allow my user to schedule a notification using AlarmManager, and then they schedule another notification using the same code, will the first alarm be overridden? Or will both alarms be set?
When creating a pendingIntent you have to include an id:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ID , intent, Flags)
If the number in the ID location is always the same, then the alarm will be overridden. If it is the same then it will not be.
You can use the same code, as long as the ID is different. You can achieve this in many ways, like creating an ID based on the time you schedule the alarm for, as you may not need two alarms for the same time.

How to tell PendingIntent whether to create new or update existing broadcast

Imagine I am making a calendar. So people can schedule appointments, and people can update appointments. People can also cancel appointments.
I want to send people notification 15 minutes before their appointments. So I have the following method
public void scheduleNotification( Notification notification, long futureTimeInMillis, int id){
Context context =MyApplication.getContext();
Intent notificationIntent = new Intent(context,NotificationPublisher.class);
notificationIntent.putExtra(NOTIFICATION_ID,id);
notificationIntent.putExtra(NOTIFICATION,notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,futureTimeInMillis,pendingIntent);
}
My problem is with PendingIntent.getBroadcast(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT)
if a user has updated an existing appointment, I want the new notification to replace the old notification for that appointment
if a user creates a new appointment, I do not want the new notification to replace any existing notification.
So how do I tell PendingIntent whether to replace or create something new? Clearly NotificationPublisher is going to be the exact same class for all.
The second parameter of
PendingIntent.getBroadcast(context, 0 ,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
can be used to differentiate different PendingIntents. You should use a differnt value for each unique alarm.

How to get the ID of the unique pendingIntent that is called by AlarmManager?

I am building an alarm application. One of the features I am adding right now is the volume of the alarm. When the user is setting up the alarm, they can choose the volume of the alarm with a SeekBar. So, when the alarm goes off, the volume of it should be set to whatever the user set it to. To do this, I would need to know which specific alarm has been called. I have identified each PendingIntent with the id of the alarm from the database. My problem is I just don't know how to retrieve this id again once the alarm goes off.
As you can see, this is how I identify each the PendingIntent by passing in alarm.getID():
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(
ChangeAlarmActivity.this, (int)alarm.getID(), alarmIntent, 0);
So in my activity that is called when the alarm goes off I want to do something like
int id = pendingIntent.getID();
Alarm alarm = database.getAlarm(id);
int volume = alarm.getVolume();
How do I therefore get the unique identifier of a PendingIntent within an Activity that is called after the alarm is turned on? In other words, how do I know which alarm is being set off?
In first place, forgive me for my english, it's not very good.
I think that you most likely already found out a solution for this issue, but I had the same doubt and I couldn't figure out the answer easily. So after many researches I figure out a way. For retrieve a ID from the alarm fired is possible put a ID on intent:
// create intent
Intent intent = new Intent(context,MyClass.class);
// store id
intent.putExtra("id", yourId);
And after this, use this intent for create the PendingIntent, that going to setting up the alarm. When the alarm fire off, the method onReceive(context, intent) will receive the intent used for create the PendingIntent, thus the id can be retrieve on the method:
// retrieves id
long id = intent.getLongExtra("id", -1);
I hope that be useful!

There was already an alarm for this Intent scheduled,and I want add another one

I register a broadcastreceiver in AndroidMainfest.xml
And in my app, a function is that User can set a time and at this time the app will send a notification. I get the arguments User set ,and use alarmManager to set a task which will happened at the time user set.
But I find that GOOGLE API said that:
If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
So if I want set two or more task,the Intent will be replaced , and at end I can only get one notification,it's not the result I want.
And then I found the intent was identified by action, data, type, class, and categories,
but I can't change action(the intent's action is the intent-filter's action was registred in the AndroidMainfest.xml ),but at the time that I change the other arguments I can't even receive a broadcast.
I thought there are four ways to solve this problem,but I only made one..
create lots of broadcastreceiver and register these in
AndroidMainfest.xml,and in this way I could change the intent's
action
register the broadcastreceiver in the program ,but I didn't
make it
use service + Timer class ..
To make two intent different without change action.
Any help will be appreciated!!
Intent intent = new Intent("aaa"); //there was a broadcastreceiver's intent-filter "aaa"
intent.putExtra("title", title);
intent.putExtra("table", "计划");
PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Alarm.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time, pi);
The code is in onClickListener{onClick(){}}
Instead of Broadcasting which is not letting you to set repeat alarm you can use the calendar Object and set the alarm at whatever desired time. Once the alarm gets ON, your code will run automatically and as many times you have set the alarm.
You can also take a help of the following tutorial. I am sure it will help you out somehow.
http://blog.mikesir87.io/2013/04/android-creating-an-alarm-with-alarmmanager/

two alarms set at same time

in my application users are free to select time and date to set alarms .
What will happen if user choose exactly the same date and time for two alarms.
I am taking input from user(for date and time) and setting the alarm.
GregorianCalendar gc=new GregorianCalendar();
gc.set(2012, 1, 22, 11, 19,0);//values as given by the user
final Intent intent = new Intent(this, AlarmService.class);
gc.set(Calendar.AM_PM,0);
// final PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(),PendingIntent.getBroadcast(this,1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
I have used a broadcastreceiver to recieve the alarm broadcast.
How should I handle this situation. What should I prefer to do?
I want to know the tecchnical aspect as what happens in this situation.
AFAIK,it won't create any technical problem for alarms set for the same time.It will fire all alarms at the same time.
It depends upon you how you want your user to set alarm.If you want them to set same time for various alarms,it's not a problem.
But if you don't want them to repeat the alarm time once it is set for other,then you can store alarms' time in database and at the time of setting new one,you can check for the conflict of newly set alarm time with previously set alarms from database and reject if it is found same.

Categories

Resources