Reputation: 129
I am developing an android application where I want to give my users the choice to set a time for notification and also if they want the notification re-sent after a specified interval. I am facing a problem with alarm manager. Somehow the alarm manager does not trigger the Broadcast receiver.
The following is the code for setting time for notification in alarm manager in main activity:
private void checkForCreatingNotification(Intent data){
if (Constants.hours!=-1 && Constants.minutes!=-1){
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,Constants.hours);
calendar.set(Calendar.MINUTE,Constants.minutes);
calendar.set(Calendar.SECOND, 0);
Log.e("time", " " + calendar.getTime());
Log.e("time"," "+calendar.getTimeInMillis());
boolean setOnce=data.getBooleanExtra(Constants.TIME_SET_ONCE,false);
boolean setRepeat=data.getBooleanExtra(Constants.TIME_SET_REPEAT,false);
int repeatDuration=data.getIntExtra(Constants.REPEAT_DURATION,Constants.NO_DURATION_REPEAT);
SharedPreferences preferences=getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putBoolean(Constants.TIME_SET_ONCE,setOnce);
editor.putBoolean(Constants.TIME_SET_REPEAT, setRepeat);
editor.putInt(Constants.REPEAT_DURATION, repeatDuration);
editor.apply();
Intent intent;
intent = new Intent(this,NotificationMessage.class);
PendingIntent pendingIntent = PendingIntent.getService(this,
Constants.NOTIFICATION_REQUEST , intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
if (setOnce){
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}else if (setRepeat){
if(repeatDuration==Constants.HOURS24_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
else if (repeatDuration==Constants.HOURS12_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
else if (repeatDuration==Constants.HOURS6_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_DAY/2, pendingIntent);
else if (repeatDuration==Constants.HOURS1_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HOUR, pendingIntent);
else if (repeatDuration==Constants.MINUTES30_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
else if (repeatDuration==Constants.MINUTES5_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_HOUR/6, pendingIntent);
else if (repeatDuration==Constants.MINUTES1_DURATION)
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES/15, pendingIntent);
}
//sendBroadcast(intent);
}
}
The BroadCastReceiver:
public class NotificationMessage extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getResources().getString(R.string.notification_title))
.setContentText(context.getResources().getString(R.string.notification_message));
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, WardrobeHome.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(WardrobeHome.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(Constants.NOTIFICATION_REQUEST, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
I have declared the receiver in the manifest file.
Upvotes: 0
Views: 314
Reputation: 21
use PendingIntent.getBroadcast instead of PendingIntent.getService and you are good to go
Upvotes: 2