Skip to content

Notification demo with scheduling and repeating local notificaitons functionality.

Notifications You must be signed in to change notification settings

ankurgecr/LocalNotificationHelper

Repository files navigation

Local Notification Helper

Getting started

Notification Components

To get started with LocalNotificationHelper, you'll need to get add the dependency to your project's build.gradle file:

dependencies {
    //other dependencies
    implementation "android.helper:localnotification:1.1.9"
}

and sync up your project.

Good. Now you are all set to use LocalNotification Helper.

List of Notifications

In order to get list of all notifications async, call this method:

LocalNotificationHelper.getAll(new LocalNotificationHandler() {
     @Override
     public void onNotificationReceived(List<LocalNotification> notifications) {
         //..
     }
});

or for getting list of all notifications Sync, call this method:

List<LocalNotification> notificationList = LocalNotificationHelper.getAllSync();

make sure you do not call this sync method from Activity's main thread. You call it like this:

void getAllNotificationsSync() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            final List<LocalNotification> notificationList = LocalNotificationHelper.getAllSync();
            //.. do all Sync tasks here
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //.. this is main thread again, do all Async tasks here
                }
            });
        }
    }).start();
}

Here are the properties of LocalNotification model:

class LocalNotification {
    int notificationId;       /* Primary id of notification */
    String channelId;         /* Ignore this for now */
    String textTitle;         /* Title text on notification */
    String textContent;       /* Content text on notification */
    int smallIcon;            /* Small icon - Single color (Monochrome) png image with Transparency */
    int largeIcon;            /* Large icon - Any icon image jpeg/png */
    long triggerTime;          /* Time in milliseconds when notification is going to get triggered next time */
    long triggerDelay;         /* Delay in milliseconds after notification will triggered */
    long repeatDelay;          /* Delay in milliseconds after notification will repeated, 0 if not repeating */
}

Create/Schedule or Edit/Reschedule notification

In order to schedule a simple notification, call:

boolean result = LocalNotificationHelper.schedule(
        100,              //intNotificationId
        "This is a test", //stringContent
        10000L            //longTriggerDelayInMillis
);

or in order to schedule a repeating notification, call:

boolean result = LocalNotificationHelper.schedule(
        101,                        //intNotificationId
        "This is a repeating test", //stringContent
        10000L,                     //longTriggerDelayInMillis
        10000L                      //longRepeatDelayInMillis
);

or in order to schedule a notification with Date, call:

Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.set(Calendar.HOUR, 8);
c.set(Calendar.MINUTE, 30);
c.set(Calendar.SECOND, 0);
Date date = c.getTime(); // 8:30 AM of today's date

boolean result = LocalNotificationHelper.schedule(
        101,                        //intNotificationId
        "This is a repeating test", //stringContent
        date,                       //dateTriggerTime
        10000L                      //longRepeatDelayInMillis
);

or for more options call

boolean result = LocalNotificationHelper.schedule(
        intNotificationId,
        stringChannelName,
        intResSmallIcon,
        intResLargeIcon,
        stringTitle,
        stringContent,
        longTriggerDelayInMillis,
        longRepeatDelayInMillis,
        booleanShouldRepeat
);

result will be true if you notification is scheduled successfully.

Note:

  • If you will pass the 'id' of existing/already scheduled notification, it will override/reschedule the previous notification with new parameters.
  • If you are scheduling a repeating notification, make sure you keep minimum 15 minutes of interval because of limitations of WorkManager

Get notification status

In order to check if a notification is scheduled or not, call:

LocalNotificationHelper.isScheduled(notificationId, new LocalNotificationStatusHandler() {
   @Override
   public void onNotificationStatusReceived(boolean scheduled) {
       if (scheduled) {
           //..
       } else {
           //..
       }
   }
});

Or to check the same in Sync method,

boolean scheduled = LocalNotificationHelper.isScheduledSync(notificationId);

again, do not call this Sync method from Main thread of your activity.

Cancel/Unschedule notification

For cancelling a notification, call

LocalNotificationHelper.cancel(notificationId);

or

LocalNotification myNotification = /**/;
LocalNotificationHelper.cancel(myNotification);

For cancelling all the scheduled notifications, call

LocalNotificationHelper.cancelAll();

Technology

LocalNotificationHelper library uses WorkManager internally. which is the best way to perform delayed task as per this and this articles comparing WorkManager with Job Schedulers, Services, Loaders and AlarmManager

WorkManage uses a WorkRequest for repeating a Work. This Work executes multiple times until it is cancelled, with the first execution happening immediately or as soon as the given Constraints are met. The next execution will happen during the period interval; note that execution may be delayed because WorkManager is subject to OS battery optimizations, such as doze mode.

Demo

Check the demo app for more details.

About

Notification demo with scheduling and repeating local notificaitons functionality.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages