在一项服务中重复不同的通知

Repeating different notifications in one service

我正在开发一个在早上 8 点和晚上 8 点发出通知的应用程序,所以我想做的是发出 2 个不同的通知,比如早上 8 点他说你好,现在是早上,晚上 8 点他说你好,现在是晚上所以我如何通过 1 个服务和 1 个警报管理器来做到这一点。 有人可以给我完整的例子吗?

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            
    }

}

现在,在启动您的服务时,只需像这样添加一个额外的 -

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