How to Repeat Notification Daily on Specific Time in Android Through Background Service

Notifications in specific time every day android

Use alarm manager and put your notification in NotifyService class

Intent myIntent = new Intent(Current.this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent); //set repeating every 24 hours

i want show notification at 8:00 am everyday

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(context of current file, AlarmReceiver1.class);

pendingIntent = PendingIntent.getBroadcast(Menu.this, 0, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
alarmManager.cancel(pendingIntent);

Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 8);
alarmStartTime.set(Calendar.MINUTE, 00);
alarmStartTime.set(Calendar.SECOND, 0);
if (now.after(alarmStartTime)) {
Log.d("Hey","Added a day");
alarmStartTime.add(Calendar.DATE, 1);
}

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Alarm","Alarms set for everyday 8 am.");

Coming to the broadcast receiver class. You need to register your broadcast receiver in the manifest. This will cause you to receive clock events.
Override the onReceive method of this broadcast receiver and make a notification there itself or make a seperate notification building service and build and display your notification there.

The manifest code snippet:

<receiver android:name="AlarmReceiver1"  android:enabled="true">

The broadcast receiver code snippet:

public class AlarmReceiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationService1.class);
service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
context.startService(service1);
}

Notification building service code snippet:

public class NotificationService1 extends IntentService{

private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
ontext context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, Activity to be opened after clicking on the notif);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif","Notifications sent.");

}

}

How to show a notification everyday at a certain time even when the app is closed?

If I understood you correctly, I believe that you need setup a recurring alarm using AlarmManager. You also need to setup starting alarm service on device reboot. You can write a method that does what you want so it get executed when the alarm runs e.g. show notification. The following links should help you:

  • Android Fundamentals: Scheduling Recurring Tasks
  • Repeat Alarm Example In Android Using AlarmManager

How To give notifications on Android on specific time in Android Oreo?

After looking at different posts and some research on AlarmManager implementation, this is what worked for me.

The base for this is this post and Schedule repeating Alarms Android Documentation.

This is my current implementation:

I have a SwitchPreference and a TimePicker implementation is Settings

SwitchPreference to ask if user wants to enable Repeating Daily Notifications.

TimePicker to set the Notification time.

In MainActivity's OnCreate method or wherever you are reading the SharedPreferences do this:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean dailyNotify = sharedPref.getBoolean(SettingsActivity.KEY_PREF_DAILY_NOTIFICATION, true);
PackageManager pm = this.getPackageManager();
ComponentName receiver = new ComponentName(this, DeviceBootReceiver.class);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

// if user enabled daily notifications
if (dailyNotify) {
//region Enable Daily Notifications
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
calendar.set(Calendar.SECOND, 1);
// if notification time is before selected time, send notification the next day
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
if (manager != null) {
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
//To enable Boot Receiver class
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
//endregion
} else { //Disable Daily Notifications
if (PendingIntent.getBroadcast(this, 0, alarmIntent, 0) != null && manager != null) {
manager.cancel(pendingIntent);
//Toast.makeText(this,"Notifications were disabled",Toast.LENGTH_SHORT).show();
}
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}

Next add AlarmReceiver class that implements BroadcastReceiver like this:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));
SharedPreferences.Editor sharedPrefEditor = prefs.edit();

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingI = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
"Daily Notification",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Daily Notification");
if (nm != null) {
nm.createNotificationChannel(channel);
}
}
NotificationCompat.Builder b = new NotificationCompat.Builder(context, "default");
b.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setTicker("{Time to watch some cool stuff!}")
.setContentTitle("My Cool App")
.setContentText("Time to watch some cool stuff!")
.setContentInfo("INFO")
.setContentIntent(pendingI);

if (nm != null) {
nm.notify(1, b.build());
Calendar nextNotifyTime = Calendar.getInstance();
nextNotifyTime.add(Calendar.DATE, 1);
sharedPrefEditor.putLong("nextNotifyTime", nextNotifyTime.getTimeInMillis());
sharedPrefEditor.apply();
}
}
}

The system will turn off the AlarmManager if Device is powered off or reboots, so restart it again on BOOT COMPLETE add this class:

public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), "android.intent.action.BOOT_COMPLETED")) {
// on device boot complete, reset the alarm
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
calendar.set(Calendar.SECOND, 1);

Calendar newC = new GregorianCalendar();
newC.setTimeInMillis(sharedPref.getLong("nextNotifyTime", Calendar.getInstance().getTimeInMillis()));

if (calendar.after(newC)) {
calendar.add(Calendar.HOUR, 1);
}

if (manager != null) {
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
}

And Finally do not forget to add these permissions to AndroidManidest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and register your receivers in AndroidManifest

<application
<!--YOUR APPLICATION STUFF-->

<receiver android:name=".DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" />

The Notification should be set by this at a specific time of the day specified by TimePicker and if user enabled the SwitchPreference.

UPDATE (Aug-2022): For Android 12 Devices

To make this work with Android 12 devices, you need to modify the pendingIntent's flags from 0 to FLAG_IMMUTABLE like this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, FLAG_IMMUTABLE);

Up until Build.VERSION_CODES.R, PendingIntents are assumed to be mutable by default, unless FLAG_IMMUTABLE is set. Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents on creation with either FLAG_IMMUTABLE or FLAG_MUTABLE.
You can find more information on PendingIntent Flags here

How To give notifications on android on specific time?

first you need to use a broadcastreceiver. and because a broadcast receiver up only for a short time

from the android developer blog.When handling a broadcast, the application is given a fixed set of time (currently 10 seconds) in which to do its work. If it doesn't complete in that time, the application is considered to be misbehaving, and its process immediately tossed into the background state to be killed for memory if needed.

its a better practice to use also intent service here you have a example how to do it.

this is the broadcast receiver class.

public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {

Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}

and register it in the manifest.

<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false" >
</receiver>

this is the intent service class.

public class MyNewIntentService extends IntentService {
private static final int NOTIFICATION_ID = 3;

public MyNewIntentService() {
super("MyNewIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("My Title");
builder.setContentText("This is the Body");
builder.setSmallIcon(R.drawable.whatever);
Intent notifyIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//to be able to launch your activity from the notification
builder.setContentIntent(pendingIntent);
Notification notificationCompat = builder.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(NOTIFICATION_ID, notificationCompat);
}
}

and register it in the manifest.

<service
android:name=".MyNewIntentService"
android:exported="false" >
</service>

and then in your activity set the alarm manger to start the broadcast receiver at a specific time and use AlarmManager setRepeating method to repeat it this example bellow will repeat it every day.

 Intent notifyIntent = new Intent(this,MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(context, NOTIFICATION_REMINDER_NIGHT, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000 * 60 * 60 * 24, pendingIntent);

i hope this will help you.

Creating an android notification that reoccurs every day at some time even when app is closed

You are almost there. Very close just one more thing.
Instead of startService() you should use startForegrounService(). In that case your alarm will always fire. Just a few tips:

  1. you should declare the foreground service in your manifest
  2. You have to use alarmManager.setExactAndAllowWhileIdle for API level higher than 23

You can also check this out for more information.

Update: This method worked forme on Xiaomi Redme note 8, but the just suddenly stopped working! It may not be a very dependable solution for custom OS devices at the moment. Hope google come up with a solid solution.

Repeat notification every day 12h

Update your manifest file to have the following parameters for your receiver:

<receiver
android:name="com.example.alarmmanagernotifcation.AlarmRecei‌​ver"
android:enabled="true"
android:process=":remote" />

Then I believe the problem could be with the icon you have chosen with:

.setSmallIcon(R.drawable.photo)

If the image is not compatible then you will not see a crash outside the app, it will only show a fatal exception inside of the android monitor if the phone is plugged in, so it can go unnoticed and the notification will not be fired.

The drawable that is used needs to have the different sizes for different phone densities. To make the correct drawables, right click on your drawable package and do New -> Image Asset. From the pulldown select Notification Icons, and use that to generate all the different sizes for the icon.



Related Topics



Leave a reply



Submit