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

3

15

Although this question might have been asked before on Stack Overflow, I still haven't found a clear answer.

I want to show a notification everyday at 12pm for example even when the app is closed. I have referenced from those links: Notifications in specific time every day android, Android daily repeating notification at specific time of a day using AlarmManager, Android BroadcastReceiver on startup - keep running when Activity is in Background and much more... I'm confused on the difference between Service and BroadcastReceiver. Which one shall I use? or shall I use both of them?

So far, I know how to show a notification, but I don't know how to show it automatically once everyday even when the app is closed.

My code:

public class NotifyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();

        Intent resultIntent = new Intent(this, HomeScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

        Notification.Builder notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("App Title")
                .setContentText("Some Text...")
                .setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT < 16) {
            notificationManager.notify(1, notification.getNotification());
        } else {
            notificationManager.notify(1, notification.build());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
    }
}

AppManifest.xml:

<service android:name=".NotifyService" />

How should I write my code to accomplish what I want? Any suggestions or any good link that I can understand from?

Trilbie answered 10/10, 2015 at 14:34 Comment(0)
I
14

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:

Insignia answered 10/10, 2015 at 14:45 Comment(3)
Thanks. I will take a look at them and reply to you.Trilbie
The second link solved my issue. I had to use a BroadcastReceiver instead of a Service. Thanks for help.Trilbie
The second link doesn't work, stacktips.com/tutorials/android/repeat-alarm-example-in-androidCavite
D
29

This is updated solution, and it works Android Oreo

Step 1: Create a Method in your MainActivity and use AlarmManager to set alarm at a specified time.

public void myAlarm() {
  
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 21);
    calendar.set(Calendar.MINUTE, 47);
    calendar.set(Calendar.SECOND, 0);
    
    if (calendar.getTime().compareTo(new Date()) < 0) 
        calendar.add(Calendar.DAY_OF_MONTH, 1);

    Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    if (alarmManager != null) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

  }
  
}

I'm setting my alarm at 09:47 PM

Step 2: Create BroadcastReceiver to listen when the alarm happens

public class NotificationReceiver extends BroadcastReceiver {

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

    NotificationHelper notificationHelper = new NotificationHelper(context);
    notificationHelper.createNotification();

   }
}

I'm creating this class named NotificationReceiver and extends BroadcastReceiver, in onReceive there is Class named NotificationHelper, don't confuse I will explain this Class for next steps.

Step 3: Create the Notification class

class NotificationHelper {

private Context mContext;
private static final String NOTIFICATION_CHANNEL_ID = "10001";

NotificationHelper(Context context) {
    mContext = context;
}

void createNotification()
{
   
    Intent intent = new Intent(mContext , NotificationActivity.class);
   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle("Title")
            .setContentText("Content")
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    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});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
       }
     }

This class handles the notification

Step 4: Come back to Step 2: and call the Notification Class

 NotificationHelper notificationHelper = new NotificationHelper(context);
 notificationHelper.createNotification();

Registering a BroadcastReceiver Go to your Androidmanifest file and register your broadcast receiver

<receiver
    android:name=".NotificationReceiver"></receiver>

For more info refer this guide from google

I hope it helps you.

Dorotheadorothee answered 29/4, 2019 at 20:46 Comment(16)
Thanks! But one small clarification in the code - you need to add calendar.set(Calendar.SECOND, 0); in Step 1. Otherwise, the alarm is triggered not exactly at the specified time, but with a delay of no more than a minute.Forum
@Forum you're right the alarm will trigger between 0 to 59 Seconds, It's a random but as you mention not more than a minute.Dorotheadorothee
what about 12 hours clock time?Merovingian
@JimaleAbdi can you check why this not works #67751097Eyestrain
@Sh_Khan Alarms do not fire when the device is idle in Doze mode I think that's the problem.Dorotheadorothee
@JimaleAbdi any workaround as this means no perfect app ? Also can job or workManager be used instead ???Eyestrain
The best choice of schedule repeating Alarms is using AlarmManager, If you must have an alarm fire even in Doze mode you can use either setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). follow this instruction. ThanksDorotheadorothee
If i have to schedule one at say "17:10:30" as "hh:mm:ss" daily should it be setInexactRepeating or setRepeating if i need it to fire at that time with any mode ??Eyestrain
If you want the alarm to fire an exact time consider using setExactAndAllowWhileIdle() also it can cause battery drain so use it with care.Dorotheadorothee
@JimaleAbdi Does setExactAndAllowWhileIdle repeats ?? as i need it dailyEyestrain
@JimaleAbdi Also when i test and schedule notifications , i play with date and time settings to wait 1 minute for the firing as a test does that affect anything , also i use simulator will this make a difference than real device ?Eyestrain
Let us continue this discussion in chat.Dorotheadorothee
What is this line for if (calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);?Underbodice
@SujithSManjavana This is comparing the time you defined to fire the alarm and the current time, so if the time is passed It will add a day to the calendar then the alarm will fire the next day.Dorotheadorothee
I think the only problem here is that if the android is restarted the alarm will stop workingCoseismal
I wanted to make the answer simple so I didn't include it, you can create a new BroadcastReceiver to listen for RECEIVE_BOOT_COMPLETED. Thanks, S.Iliev.Dorotheadorothee
I
14

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:

Insignia answered 10/10, 2015 at 14:45 Comment(3)
Thanks. I will take a look at them and reply to you.Trilbie
The second link solved my issue. I had to use a BroadcastReceiver instead of a Service. Thanks for help.Trilbie
The second link doesn't work, stacktips.com/tutorials/android/repeat-alarm-example-in-androidCavite
R
0
  1. Create a method which contains your code where you will define your time or at what time you want to show the notification. This method need to be called from where you want user to ask for notification.

    public void getNotification () {
    
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
        Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
    
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
    
        alarmManager.cancel(pendingIntent);
    
        Calendar calendar = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 00);
        if (now.after(calendar)) {
            Log.d("Hey","Added a day");
            calendar.add(Calendar.DATE, 1);
        }
    
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
    
  2. Create a Notification_receiver class which is going to extend Broadcast Receiver here you are going to define your Channel Id. This works for API 25 and above:

    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Build;
    import android.util.Log;
    
    import androidx.core.app.NotificationCompat;
    
    //Created By Prabhat Dwivedi
    public class Notification_receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder;
            PendingIntent pendingIntent;
    
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("Your App Name", "You app Package Name", NotificationManager.IMPORTANCE_HIGH);
                String channel_Id = channel.getId();
                CharSequence channel_name = channel.getName();
                Log.e("Notification_receiver", "channel_Id :" + channel_Id);
                Log.e("channel_name", "channel_name :" + channel_name);
    
                channel.setDescription("Make entry of today's spending now");      
                notificationManager.createNotificationChannel(channel);
            }
    
            builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.yourapp_logo)
                    .setChannelId("Your app Name is your Channel Id")
                    .setContentTitle("Your title")
                    .setContentText("Your Description")
                    .setAutoCancel(true);
    
            // Under this you will find intent it is going to define after clicking notification which activity you want to redirect
            Intent repeatingIntent = new Intent(context, HomePage.class);
            pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);
            notificationManager.notify(100, builder.build());
        }
    }
    
Rasp answered 25/11, 2020 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.