Schedule local notification in android

      8 Comments on Schedule local notification in android

Hello Folks, In this tutorial we will discuss how we can schedule local notification in android. Its a super easy job and involves few android components.

I have created a demo project for this tutorial you can find it on github.

Components involved to schedule local notification in android

  1. BroadCastReceiver
  2. AlarmManager
  3. Notification Service
  4. PendingIntent

We will discuss these components whenever we will come across them in the code.

Watch this video to understand scheduling local notification in depth

Create MainActivity

There are few things to discuss here

Intent

In the above code we are creating a notification intent with action android.media.action.DISPLAY_NOTIFICATION.

AlarmManager

AlarmManager is created using the ALARM_SERVICE. It takes a PendingIntent and time as  parameters. And it launches that intent when the time is over. In our case the time when the intent will be broadcasted is 15 seconds.

In the above code main components are

BroadCastReceiver

Here AlarmReceiver is a broadcast receiver which listens to broadcasts with action android.media.action.DISPLAY_NOTIFICATION. This action is declared in the AndroidManifest.xml file.

NotificationCompat.Builder

This builder is used to build a Notification object which can be used by NotificationManger to notify the user. The Notification object can have an Icon, Text to be displayed in notification and most importantly a  PendingIntent which will be used when we will click on the notification.

NotificationManager

At last NotificationManager will do the job of sending the notification to the user. And it will take notification as the 2nd argument. First argument to notify function is an identifier for the notification which should be unique within that system or mobile.

Lets see how AlarmManager receives the broadcast, where do we define that configuration

Well its the AndroidManifest.xml which does the magic. We define all the broadcast receivers in that file. And what should they do when a broadcast is broadcasted. Lets have a look at our AndroidManifest.xml

As you can see in the above file I have defined a receiver whose name is .AlarmReceiver which points to java class AlarmReceiver. And it has the action to which it listens to and a category. The intent-filter helps the receiver to filter those broadcast which it suppose to listen.

8 thoughts on “Schedule local notification in android

  1. atif

    android.media.action.DISPLAY_NOTIFICATION

    the very first line is giving error on action.. i have highlighted

    android.media.action.DISPLAY_NOTIFICATION

    how to solve this ?

    Reply
  2. xeus

    Thank you, very helpful.
    I’ve changed
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
    to
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
    so app can gain compatibility with lower APIs (15+)

    Reply
    1. Ajit Singh Post author

      This code is purely for demonstration purposes. If you want to permanently set an alarm in your application then you will have to store your alarms in database or other storage and make sure that you set the alarms whenever phone reboots. You can achieve this by listing to

      And when your receiver is called just set the alarms again.

      Reply
  3. Aswathi Ps

    I want to show Local Notification Repeatedly on every day where my application state either running or not. Its not work for me.. SendNotification() called from main activity

    public async Task SendNotification()
    {

    Intent alarmIntent = new Intent(this, typeof(AlarmReceiverNew));
    alarmIntent.PutExtra(“message”, “message”);
    alarmIntent.PutExtra(“title”, “Welcome”);

    PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
    AlarmManager alarmManager = (AlarmManager)this.GetSystemService(Context.AlarmService);
    long futureInMillis = SystemClock.ElapsedRealtime() + 12 * 3600 * 1000;
    alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, futureInMillis, pendingIntent);
    }

     

    [BroadcastReceiver]
    public class AlarmReceiverNew : BroadcastReceiver
    { public async override void OnReceive(Context context, Intent intent)
    {

    Intent notIntent = new Intent(context, typeof(MainActivity));
    PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
    NotificationManagerCompat manager = NotificationManagerCompat.From(context);

    var wearableExtender = new NotificationCompat.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, 1));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .SetContentIntent(contentIntent)
    .SetSmallIcon(Resource.Drawable.icon)
    .SetContentTitle(“title”)
    .SetContentText(“message”)
    .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
    .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
    .Extend(wearableExtender);

    Notification notification = builder.Build();

    manager.Notify(0, notification);

    }
    }

     

     

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *