I'm making an app where I want the user to specify a time in shared preferences then I'll use that time to launch a notification whether they're in the app or not, like a user-defined alarm notification
.
I've tried everything but I don't know how to make it work. The notification should appear every day at that specific time if the preference (checkbox) is true.
Finally I found out how to do it. For anyone who might need the same, I'll post the code:
This here is the method to call the notification in the activity once time is right:
public void notifyAtTime() {
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
long time = sharedPref.getLong("Timepref", 0);
SimpleDateFormat formatter = new SimpleDateFormat("HH", Locale.UK);
SimpleDateFormat formatter1 = new SimpleDateFormat("mm", Locale.UK);
Date date = new Date(time);
Date date1 = new Date(time);
long hour = Long.parseLong(formatter.format(date));
long minute = Long.parseLong(formatter1.format(date1));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, (int) hour);
cal.set(Calendar.MINUTE, (int) minute);
cal.set(Calendar.SECOND,0);
if (alarmManager != null) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}
}
This here is the receiver
public class AlarmReceiver extends BroadcastReceiver {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, TicketActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(TicketActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String ringer = sharedPreferences.getString("key_notifications_new_message_ringtone","");
boolean vibrate = sharedPreferences.getBoolean("Vibrate",true);
Uri ringtone = Uri.parse(ringer);
Notification notification = builder.setContentTitle(context.getResources().getString(R.string.notific))
.setContentText(context.getResources().getString(R.string.notific))
.setTicker(context.getResources().getString(R.string.notific))
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(ringtone)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0, notification);
}
}}