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

前端 未结 3 438
温柔的废话
温柔的废话 2021-02-04 10:52

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

3条回答
  •  被撕碎了的回忆
    2021-02-04 10:59

    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

    
    

    For more info refer this guide from google

    I hope it helps you.

提交回复
热议问题