Search code examples
androidservicealarmmanager

Repeating different notifications in one service


I'm developing an app that gives a notification at 8 am and 8 pm so what i wanna do is give 2 different notification like at 8 am he say hello it's morning and at 8 pm he say hello it's evening so how can i do that by 1 service and 1 alarm manager. Can someone gives me full example for that .


Solution

  • public class NotifTriggerService extends IntentService {
    
        public static final String NOTIF_TYPE = "notif_type";
    
        public static final String NOTIF_TYPE_MORNING = "notif_day";
        public static final String NOTIF_TYPE_NIGHT = "notif_night";
    
    
        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         */
        public NotifTriggerService() {
            super("Trigger");
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            String notifType = intent.getStringExtra(NOTIF_TYPE);
        if (notifType.equals(NOTIF_TYPE_MORNING)) {
            //Send morning notification
        } else if( notifType.equals(NOTIF_TYPE_NIGHT)) {
            //Send night notification            
        }
    
    }
    

    Now while starting your service just put an extra like this -

    Intent intent = new Intent(getActivity(), YourService.class);
    intent.putExtra(YourService.NOTIF_TYPE, YourService.NOTIF_TYPE_MORNING);