@marcelodeandrade0201 wrote:
I'm trying to make a local notification trigger a random message every time there is a notification.
I'm doing an event handling of the plugin itself, but the problem is:
How should I access the
Provider, who provides data to notify, through the event logged by the notifications plugin?
HomePageimport { MessagesProvider } from './../../providers/messages/messages'; import { Component } from '@angular/core'; import { LocalNotifications } from '@ionic-native/local-notifications'; import { AlertController, IonicPage, NavController, Platform } from 'ionic-angular'; import * as moment from 'moment'; @IonicPage() @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { notificationTime: string; timeNotify: Date; message: any; notification: any; constructor( public navCtrl: NavController, public alertCtrl: AlertController, public platform: Platform, public localNotifications: LocalNotifications, public messagesProvider: MessagesProvider ) { this.notificationTime = moment(new Date()).format("HH:mm:ss"); this.timeNotify = new Date(); } public onPageDidEnter() { this.messagesProvider.getMessage().subscribe(data => { this.message = data; }); } timeChange(time) { this.timeNotify = new Date(); this.timeNotify.setHours(time.hour, time.minute, 0); } onEnable() { let self = this; this.messagesProvider.getMessage().subscribe(data => { let notification = { id: 99, title: '1', message: '1', every: 'minute' }; this.localNotifications.schedule(notification); this.localNotifications.on("trigger", function(localNotifications, state){ self.messagesProvider.getMessage().subscribe(data => { let notification = { id: 99, title: localNotifications.title +1, message: localNotifications.text +1, every: 'minute' }; let notificationUpdate = new LocalNotifications(); notificationUpdate.update(notification); }); }); let alert = this.alertCtrl.create({ title: 'Notifications set', buttons: ['Ok'] }); alert.present(); }); } onCancel() { return this.localNotifications.cancelAll().then(() => { console.log('Notification has been canceled...'); let alert = this.alertCtrl.create({ title: 'Notifications has been canceled...', buttons: ['Ok'] }); alert.present(); }); } }
MessagesProviderimport { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class MessagesProvider { data: any; constructor(public http: Http) { console.log('Hello MessagesProvider Provider'); } getMessage(){ return this.http.get('assets/data/messages.json') .map(res => { let data = res.json(); return this.getRandomMessage(data.messages); }); // return new Promise(resolve => { // this.http.get('assets/data/messages.json') // .map(res => res.json()) // .subscribe(data => { // this.data = this.getRandomMessage(data.messages); // resolve(this.data); // }, // err =>{ // console.log("Error !!!!" + err.message); // }); // }); } getRandomMessage(messages) { return messages[Math.floor(Math.random() * messages.length)]; } }
Posts: 1
Participants: 1