简体   繁体   中英

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 .

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM