How To give notifications on android on specific time?

前端 未结 4 559
傲寒
傲寒 2020-12-02 09:10

I want to give notification to my app on a specific time. Say everyday i have to give notification on 7 AM even if the app is closed.

How can i do this? Any tutorial

4条回答
  •  情书的邮戳
    2020-12-02 10:01

    You can use AlarmManager to set alarm at specified time

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (!prefs.getBoolean("firstTime", false)) {
    
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 1);
    
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);
    
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("firstTime", true);
        editor.apply();
    }
    

    I used SharedPreferences to check that's not the first time to run the app and if it is, you set that alarm otherwise do nothing instead of resetting the alarm each time you start your app.
    Use a BroadcastReceiver to listen when the alarm happens

    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // show toast
            Toast.makeText(context, "Alarm running", Toast.LENGTH_SHORT).show();
        }
    }
    

    Use another receiver to listen to device boots so that you can reset the alarm

    public class DeviceBootReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
                // on device boot compelete, 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);
    
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.set(Calendar.HOUR_OF_DAY, 7);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 1);
    
                manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            }
        }
    }
    

    add the permission to the manifest

    
    
    

    then register your receivers

    
        
            
        
    
    
    

提交回复
热议问题