Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DailyReminder extends BroadcastReceiver {
- private static final int NOTIFICATION_ID = 101;
- public static final String NOTIFICATION_CHANNEL_ID = "10001";
- @Override
- public void onReceive(Context context, Intent intent) {
- String appName = context.getString(R.string.app_name);
- String message = context.getString(R.string.msg_daily_reminder);
- showAlarmNotif(context, appName, message, NOTIFICATION_ID);
- }
- public DailyReminder(){
- }
- private void showAlarmNotif(Context context, String title, String message, int notifId){
- NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- Intent intent = new Intent(context, MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(context, notifId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- Uri alarmRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
- .setSmallIcon(R.drawable.ic_alarm_black_24dp)
- .setContentTitle(title)
- .setContentText(message)
- .setContentIntent(pendingIntent)
- .setPriority(NotificationCompat.PRIORITY_HIGH)
- .setAutoCancel(true)
- .setColor(ContextCompat.getColor(context, android.R.color.transparent))
- .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
- .setSound(alarmRingtone);
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
- int importance = NotificationManager.IMPORTANCE_HIGH;
- NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
- notificationChannel.enableLights(true);
- notificationChannel.setLightColor(Color.RED);
- notificationChannel.enableVibration(true);
- notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
- builder.setChannelId(NOTIFICATION_CHANNEL_ID);
- notificationManager.createNotificationChannel(notificationChannel);
- }
- notificationManager.notify(notifId, builder.build());
- }
- private void setRepeatingAlrm(Context context){
- cancelAlarm(context);
- AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, 7);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- int SDK_INT = Build.VERSION.SDK_INT;
- if (SDK_INT < Build.VERSION_CODES.KITKAT) {
- alarmManager.set(AlarmManager.RTC_WAKEUP,
- calendar.getTimeInMillis(), getPendingIntent(context));
- } else if (SDK_INT > Build.VERSION_CODES.KITKAT && SDK_INT < Build.VERSION_CODES.M) {
- alarmManager.setInexactRepeating(
- AlarmManager.RTC_WAKEUP,
- calendar.getTimeInMillis(),
- AlarmManager.INTERVAL_DAY,
- getPendingIntent(context)
- );
- } else if (SDK_INT >= Build.VERSION_CODES.M) {
- alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
- calendar.getTimeInMillis(), getPendingIntent(context));
- }
- }
- public void cancelAlarm(Context context) {
- AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
- alarmManager.cancel(getPendingIntent(context));
- // notificationManager.cancelAll();
- }
- private static PendingIntent getPendingIntent(Context context) {
- Intent alarmIntent = new Intent(context, DailyReminder.class);
- boolean isAlarmOn = (PendingIntent.getBroadcast(context, NOTIFICATION_ID, alarmIntent, PendingIntent.FLAG_NO_CREATE) != null);
- Log.e("isAlarmOn : ", String.valueOf(isAlarmOn));
- return PendingIntent.getBroadcast(context, NOTIFICATION_ID, alarmIntent,
- PendingIntent.FLAG_CANCEL_CURRENT);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement