Search code examples
cordovaangularionic2cordova-pluginslocalnotification

Using Ionic 2 LocalNotifications


i need help with Ionic 2 local notifications!

I want to allow users to pick an hour from DateTime picker and then, save a reminder that will fire when the time is the indicated by user.

I have this to allow users, pick an hour: <ion-datetime pickerFormat="HH:mm" [(ngModel)]="time" cancelText="Cancelar" doneText="Aceptar"></ion-datetime>

<button ion-button block round (click)="saveReminder()">Guardar</button>

Actually, in saveReminder() i have:

public saveReminder(): void {

        if (this.timePicked()) {

            LocalNotifications.schedule({
                id: 1,
                text: '¡Hora de meditar!',
                at: new Date(new Date().getTime() + 5),
                sound: 'file://audio/sound.mp3',
                every: "day",
                //data: { message : 'Informacion' },
                //icon: 'res://icon',
                //smallIcon: 'res://ic_popup_sync'
            });

            this.showSavedReminderMsg();
        } else {
            this.showNoTimePickedError();
        }

    }

saveReminder() schedule a local notification that will appear 5 seconds after user press button (it's only for test) Now, i need to know how to schedule this notification with the date picked by the user.

My date picker, returns a string like: "08:45" and i need if exist any way to get separated hours and minutes to pass them to scheduler, or how to achieve this in some other way.

Here is the official ionic doc that explain with Date.now(): https://ionicframework.com/docs/v2/native/local-notifications/

But it does not apply to my case.

Thank's so much in advance!

Ivan.


Solution

  • If you question is about creating a new Date() object based on the user provided time, you can use hte below method to create a custom date value.

    var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
    

    Hope this is what you are looking for.

    LocalNotifications.schedule({
       text: 'Delayed ILocalNotification',
       at: new Date(year, month, day, hours, minutes, seconds, milliseconds),
       led: 'FF0000',
       sound: null
    });