如何通过本地通知事件访问提供商?

时间:2017-07-12 02:09:04

标签: angular typescript ionic2

每次收到通知时,我都会尝试让本地通知触发随机消息。

我正在对插件本身进行事件处理,但问题是:

如何通过通知插件记录的事件访问提供数据通知的Provider

HomePage

import { 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();
    });
  }


}

MessagesProvider

import { 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)];
  }
}

0 个答案:

没有答案