How to schedule multiple notifications from an activity? - android

Can someone please tell me what changes i'd have to make to the code below to schedule multiple notifications(at different timings).
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
This is the part of my Android Manifest file where i added the class that extends BroadcastReceiver:
</activity>
<receiver android:name=".NotificationPublisher"/>
</application>
At present only my last scheduled notification appears.

I'm sorry for my dumb question. I was not clear on what the parameters were for PendingIntent.getBroadcast().
All I had to do was creating unique PendingIntents by changing the 2nd parameter.

Related

Alarm Manager Confusion

I am trying to setup a Alaram below, however sometimes it fires normally and sometimes it does not and it only fires when i "open" the app. so say if alarm was schedule for 6pm, and then when i open my app at 7pm then it will fire. any ideas?
Intent intent = new Intent(AndroidApp.Context, typeof(AlarmHandler));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(AndroidApp.Context, uniqueMessageId, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);
AlarmManager alarmManager = AndroidApp.Context.GetSystemService(Context.AlarmService) as AlarmManager;
alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, triggerTime, pendingIntent);
Switching alarmManager.SetExactAndAllowWhileIdle to alarmManager.SetAlarmClock Fixed it for me. Hope it can help someone else.
Documentation: https://developer.android.com/reference/android/app/AlarmManager#setAlarmClock(android.app.AlarmManager.AlarmClockInfo,%20android.app.PendingIntent)

android pendingintent sending duplicate data

My PendingIntent is working on time broadcast receiver. The problem is when I set my time and send first data it work successfully but when I send other data it again displays the first data and not the new one, below is my code.
All my mServiceIntenyt code is perfect but what is the error
mServiceIntent.putExtra("unique",lackmyData);
PendingIntent pintent = PendingIntent.getService(MessageActivity.this, 0, mServiceIntent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(), pintent);
If you do not pass any flag then the PendingIntent ignores changes in extras when comparing Intent, causing the same old Intent to be spent again. You need to ask the PendingIntent to update the Intent as below:
PendingIntent pintent = PendingIntent.getService(MessageActivity.this,
0, mServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Or in order to make sure your intent is unqiue based on extras, you may play with setData as below:
mServiceIntent.setData(Uri.parse(mServiceIntent.toUri(Intent.URI_INTENT_SCHEME)));

What can I do to be sure that a background service will not start twice?

The past few days I am working on creating a background service and I noticed that quite a few people say that AlarmManager is the best way to go.
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 5 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 ,
What I wish to know is how to make sure that if this code is ran twice the service won't start 2 times. Thank you for the help in advance!
A service can't be started twice, it will remain running if you try to start it again.
See here: http://developer.android.com/guide/components/services.html#StartingAService
Edit:
However, everytime you start the service, the onStartCommand() method is called.
This is what I have used, for starting service after 30 seconds from current time,
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
PendingIntent pintent = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
Try it, and let me know what happen...
EDIT:
In your manifest.xml file
<service android:enabled="true" android:name=".ServiceClass" />

Delete AlarmManager Alarm ONLY for a Deleted Alarm

My app will have several alarms set simultaneously. Unfortunately, each alarm is being set with the same PendingIntent object. Here's the code I'm using to set the alarm:
//Use AlarmManager to trigger the notification/alarm.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//PendingIntent to launch activity when the alarm triggers.
Intent i = new Intent("com.testapp.DisplayNotification");
//Assign the reminder's primary key as the notification ID.
i.putExtra("Reminder_Name", editRemindMeTo.getText().toString());
i.putExtra("Reminder_Primary_Key", reminderPrimaryKey);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0, i, 0);
//Set the alarm to trigger.
alarmManager.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(), displayIntent);
I know that I can delete an alarm by using the following code:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyPendingIntentService.class);
PendingIntent displayIntent = PendingIntent.getService(context, 0, i, 0);
alarmManager.cancel(displyIntent);
However, using this code will delete ALL my alarms (correct me if I'm wrong here). Is there a way to delete just the alarm that a user has deleted from the database? The alarm should be deleted from my app right after the user deletes an alarm entry in my app's database. I'm guessing that using different PendingIntent names would be the way to go, but I have no idea how to do this for each new alarm that a user creates. Thoughts on how to do this? Thanks!
See the same problem i faced here previously...
So the solution is to pass unique pending intent to the alarm service.. So here i how it can be done
PendingIntent pIntent = PendingIntent.getBroadcast(context, (int) alarm_id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
here in the pending intent i have passed unique request id to the pending intent. Which you have to remember while cancelling the alarm.
So in my case what i have done to generate the unique id is put it in the database i have retrieved the id of the tables row and passed to the pending Intent.
So if you want to cancel the particular alarm you have to remember the same request id of the pending intent with the use of the same table entry...
I am sure it will work..
Use a different requestCode when registering each alarm. This is the second parameter of PendingIntent.getActivity/Service().
From the Android Documentation, cancel() will cancel all alarms with same pendingIntent. So the only way out is to create different pendingIntents. OR you could resort to scheduling repeating alarms as well, in this way you could use the same pendingIntents.

Cannot start Widget Update With Alarm Manager and PendingIntent

I am trying for couple of days to solve this problem , hope someone can help me.
I am using Alarm Manager to make my widget update nay time i want (if to sue XML its only once in 30 min) so i made a pending intent and wrote it like in the examples that i found but , its updates only once when i compile the program.
Here is mu code:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
android.util.Log.w("FullTankWidget.UpdateService", "onUpdate()");
Intent updateIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// alarmManager.setRepeating(AlarmManager.RTC, nextMinute.getTimeInMillis(),60000,pendingIntent);
long firstTime = SystemClock.elapsedRealtime();
alarmManager.set(AlarmManager.RTC, firstTime + (60 * 1000), pendingIntent);
android.util.Log.w("FullTankWidget.UpdateService", String.valueOf(firstTime));
// To prevent any ANR timeouts, we perform the update in a service
context.startService(new Intent(context,WidgetUpdateService.class));
the intent filter is writen in the manifest - defoult widget reciever
<receiver android:name=".FullTankWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/fulltank_widget_config" />
</receiver>
But your alarmManager.setRepeating() line is commented out...
First of all you should also specify your class in the Intent.
updateIntent.setClass(context, YourProviderClassName.class);
Then it is subject of the PendingIntent. There is a PendingIntent filter working on the background that does not allow two similar Intents to be set as pending. So when it is called, it will be ignored if there is a similar intent to it.
If in your appwidget provider info you have a value set for updatePeriodMillis other than zero, then the system has already set a PendingIntent for updating. This is why your new PendingIntent won't work.
The solution is to disable updatePeriodMillis and use AlarmManager instead. The AlarmManager class has methods that help you set PendingIntents the way you want. But remember you will still have only one PendingIntent at a time.

Categories

Resources