I'm building a study planner application for Android, i have the AlarmManager set up and working to trigger a notification when the time elapses, which works fine.
When setting the alarm, the alarm details are saved into an SQLite database, when the alarm is set off, i need to recall the alarm information, is there some form of variable i can save into the database and link to the specific alarm so that i can use it in an SQLite query to find the correct entry.
UPDATE Code below:
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
AlarmReciever:
public void onReceive(Context context, Intent intent)
{
long dbId = intent.getLongExtra("nID", 0);
String notificationId = Long.toString(dbId);
Log.i("App", "called receiver method");
try{
Toast.makeText(context, notificationId, Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}
Thanks in advance
You're sending a String through the intent, it needs a Long, this code should work.
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nIdLong);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
While setting the alarm, first retrieve the id which will be created while inserting alarm details to SQLite via
long uniqueId = db.insert(your query);
And put that in the intent for PendindIntent for AlarmManager
Intent alarmIntent = new Intent(this, AlarmEventReceiver.class);
alarmIntent.putExtra("dbId", uniqueId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent , 0);
Inside the onReceive() of AlarmEventReceiver, you can get the id & retrieve the data from DB.
#Override
public void onReceive(Context context, Intent intent) {
long dbId = intent.getLongExtra("dbId", 0);
}
Related
I'm working on an app which will allow users to set multiple notifications, and cancel any notification they choose.
The problem I have is that when I cancel a pending intent using the AlarmManager it is cancelling all alarms. Each of my pending intents has it's own unique request code.
For instance I would call this to create the alarm and then delete:
setAlarm(5062, 1453269670) // Set alarm 1
setAlarm(5063, 1453774418) // Set alarm 2
cancelAlarm(5062) // Cancel alarm 1
cancelAlarm(5063) // Cancel alarm 2
Setting mutiple alarms always works, I can set as many as I like and they all produce the notification. However if I was to cancel alarm 1, it also cancels alarm 2.
I know that the pending intent has to be the same when setting and cancelling, and each pending intent has it's own unique request code, so I don't know why it's not working. I've spent many hours googling, but none of the suggested answers have helped me.
void setAlarm(int request_code, long alarm_time) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("request_code", request_code);
PendingIntent pendingIntent = PendingIntent.getService(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm_time, pendingIntent);
}
void cancelAlarm(int request_code) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("request_code", request_code);
PendingIntent pendingIntent = PendingIntent.getService(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
}
The answer is simple
Intent firstIntent = new Intent(this, Receiver.class);
intent.setAction("action 1");
intent.putExtra("extra", "extra1");
firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);
Intent secondIntent = new Intent(this, Receiver.class);
intent.setAction("action 2");
intent.putExtra("extra", "extra2");
secondPendingIntent = PendingIntent.getBroadcast(this, 0, secondIntent, 0);
if you compare two PendingIntents created in ABOVE code they are NOT EQUAL
But in code BELOW:
Intent firstIntent = new Intent(this, Receiver.class);
intent.setAction("action 1");
intent.putExtra("extra", "extra1");
firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);
Intent secondIntent = new Intent(this, Receiver.class);
intent.setAction("action 1");
intent.putExtra("extra", "extra2");
secondPendingIntent = PendingIntent.getBroadcast(this, 0, secondIntent, 0);
if you compare two PendingIntents they gonna be EQUAL, so doesn't matter what you pass in putExtra method
I create multiple notifications using AlarmManager. Each notification pop-ups correctly and I would like to cancel each of these notification separately. I have an AlarmReceiver that creates the notification and sets the unique id for it. I'm sending the unique id in the intent (via putExtra). This way, I can retrieve it in the NotificationReceiver and cancel the notification. However, it is not working.
Do you know why I always get the default value when I use intent.getIntExtra("UNIQUE_ID_NAME",0) ?
Is it a proper way to achieve the goal of cancelling these notifications ?
Here is my code:
MainActivity class:
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long ctime = System.currentTimeMillis();
long tensec = 1000 * 5;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, ctime+tensec, 1000*10 , pendingIntent);
}
AlarmReceiver Class:
#Override
public void onReceive(Context context, Intent intent) {
int notification_id = generateRandom();
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent bYesIntent = new Intent(context, NotifReceiver.class);
bYesIntent.putExtra(NotificationReceiver.YES_ACTION_ID, "yesID");
PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, 0, bYesIntent, 0);
Intent bNoIntent = new Intent(context, NotifReceiver.class);
bNoIntent.putExtra(NotificationReceiver.NO_ACTION_ID, "noID");
PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, 0, bNoIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel1")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Notif")
.setContentText("Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(contentIntent)
.addAction(R.mipmap.ic_launcher, "YES", actionYesIntent)
.addAction(R.mipmap.ic_launcher, "NO", actionNoIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notification_id, builder.build());
}
public int generateRandom(){
Random random = new Random();
return random.nextInt(9999 - 1000) + 1000;
}
NotificationReceiver Class:
public class NotificationReceiver extends BroadcastReceiver {
public static String YES_ACTION_ID = "YesID";
public static String NO_ACTION_ID = "NoID";
public static String UNIQUE_ID_NAME = "notification_id";
#Override
public void onReceive(Context context, Intent intent) {
int notification_id= intent.getIntExtra(UNIQUE_ID_NAME,0);
System.out.println("Notif ID: " + notification_id); // here always is 0;
String actionYes = intent.getStringExtra(YES_ACTION_ID);
String actionNo = intent.getStringExtra(NO_ACTION_ID);
if(actionYes!=null){
Toast.makeText(context, "YES", Toast.LENGTH_SHORT).show();
}
if(actionNo!=null){
Toast.makeText(context, "NO", Toast.LENGTH_SHORT).show();
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notification_id);
}
}
I solved this problem by updating current intent and request code. Notifications are reusing the same Intent so adding FLAG_UPDATE_CURRENT should be used to be able to cancel all notification separately.
For contentIntent:
PendingIntent contentIntent = PendingIntent.getActivity(context, notification_id, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
For actionYesIntent
PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, notification_id, bYesIntent, PendingIntent.FLAG_UPDATE_CURRENT);
For actionNoIntent:
PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, notification_id, bNoIntent, PendingIntent.FLAG_UPDATE_CURRENT);
You are not adding notification_id for cases when Yes or No button is pressed,
Intent bYesIntent = new Intent(context, NotifReceiver.class);
bYesIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
bYesIntent.putExtra(NotificationReceiver.YES_ACTION_ID, "yesID");
PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, 0, bYesIntent, 0);
Intent bNoIntent = new Intent(context, NotifReceiver.class);
bNoIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
bNoIntent.putExtra(NotificationReceiver.NO_ACTION_ID, "noID");
PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, 0, bNoIntent, 0);
I am trying to implement multiple notifications for each reminder I have in my app. I found some leads around here that it could be achieved using unique id per each notification but it is not working I get only one notification which is overwritten by the last one set. Also I would like know how is it possible to keep the notification intact after device is rebooted please. Here is my code
//call notification activation Intent
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
AlarmReceiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//creating unique id for each specific notification
int ID = (int) ((new Date().getTime()/1000L) % Integer.MAX_VALUE);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notification = new Intent(context,MainActivity.class);
notification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(context,0,notification,0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mynotification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_date_range_black_48dp)
.setContentTitle("note taker")
.setContentText("you have a reminder about a note")
.setAutoCancel(true)
.setContentIntent(pi)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000,1000,1000,1000,1000});
notificationManager.notify(ID,mynotification.build());
}
}
I found some leads around here that it could be achieved using unique id per each notification but it is not working i get only one notification which is overwritten by the last one set
I think the problem lies not in your notification, but in your AlarmManager.
Try using different ID for each PendingIntent, and set the flag to PendingIntent.FLAG_ONE_SHOT.
Your calling intent should look like this:
//call notification activation Intent
Intent intent = new Intent(getBaseContext(), AlarmReciever.class);
int RQS_1 = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Try to give different request code and PendingIntent.FLAG_UPDATE_CURRENT as Flag for Pending Intent and check like this,
PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(1000000), notification, PendingIntent.FLAG_UPDATE_CURRENT);
I've got a question for you. I want to use a notification service through BroadcastReceiver and AlarmManager. I set the alarm manager with a dynamic list of times from my db.
I use this method that i call on my MainActivity OnCreate(), but doesn't work
private void restartNotify() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pendingIntent);
ArrayList<String> item = new ArrayList<String>();
item = GetLists.GetTimesListForNotification(this);
for (int i = 0; i < item.size(); ++i) {
String time = item.get(i);
int Position = time.indexOf(":");
int hour = Integer.parseInt(time.substring(0, Position));
int min = Integer.parseInt(time.substring(Position + 1));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
am.set(AlarmManager.RTC, cal.getTimeInMillis(),
pendingIntent);
}
this is my broadcast receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mManager;
mManager = (NotificationManager) context.getApplicationContext()
.getSystemService(
context.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context.getApplicationContext(),
MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,
"Nuovo messaggio da cura alert", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
context.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context.getApplicationContext(),
"Le notifiche funzionano", "it is working",
pendingNotificationIntent);
mManager.notify(0, notification);
}
}
Thank you.
Well this is because your pending intents request code is same though you are running a loop. What you can do is get your pending intent intu your loop and set the request code i. This may work. Though it is late but this may help others
I would like to create a custom notification service in my app.
I wouldn't use the google service (google cloud messaging).
Is there a way to create a daemon that check every X seconds a particular condition on my app db and show a notification?
Thank you,
edit
I call this method in MainActivity's oncreate(). I get the times from my db
private void restartNotify() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pendingIntent);
ArrayList<String> item = new ArrayList<String>();
item = GetLists.GetTimesListForNotification(this);
for (int i = 0; i < item.size(); ++i) {
String time = item.get(i);
int Position = time.indexOf(":");
int hour = Integer.parseInt(time.substring(0, Position));
int min = Integer.parseInt(time.substring(Position + 1));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
am.set(AlarmManager.RTC, cal.getTimeInMillis(),
pendingIntent);
}
And this is my broadcast receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager mManager;
mManager = (NotificationManager) context.getApplicationContext()
.getSystemService(
context.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context.getApplicationContext(),
MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,
"New message to read", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
context.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context.getApplicationContext(),
"notification are work", "it's work",
pendingNotificationIntent);
mManager.notify(0, notification);
}
}
You can use AlarmManager + BroadcastReceiver like so:
private void setRecurringAlarm(Context context) {
Intent downloader = new Intent(this, MyStartServiceReceiver.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, downloader, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 6000, 10000, pendingIntent);
}
BroadcastReceiver class:
public class MyStartServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//do the stuff...
}
}
And don't forget to register receiver in your manifest:
<receiver android:name=".MyStartServiceReceiver"
android:enabled="true"/>
You have to use Alarm Manager to schedule code for execution every specific period. take a look here:
http://developer.android.com/training/scheduling/alarms.html