2017-05-15 18 views
0

Ich erstelle eine Ionic 3-Anwendung und ich muss lokale Benachrichtigungen planen. Ich versuche nur, eine Testbenachrichtigung einzurichten, aber ich kann es nicht funktionieren lassen. Ich habe es in einem Emulator versucht, aber nichts passiert. Das LocalNotifications-Plugin wurde auch zu meiner app.module.ts-Datei hinzugefügt.Wie kann ich eine lokale Benachrichtigung in Ionic 3 senden?

Hier ist meine Typoskript-Datei importiert und Funktion:

import { Component } from '@angular/core'; 
import { NavController, NavParams, Platform, AlertController } from 'ionic-angular'; 
import { LocalNotifications } from '@ionic-native/local-notifications'; 
import * as moment from 'moment'; 

@Component({ 
    selector: 'page-notification', 
    templateUrl: 'notification.html', 
}) 
export class NotificationPage { 

// Define the variables used in the view 
notifyTime: any; 
notifications: any[] = []; 


// The constructor initializes the imports 
constructor(
      public navCtrl: NavController, 
      public navParams: NavParams, 
      public platform: Platform, 
      public alertController: AlertController, 
      public localNotifications: LocalNotifications 
     ) {} 

testNotification() { 

     // The notification 
     let notification = { 
      id:1, 
      title: "test", 
      text: "I am tester ?", 
      every: "minute" 
     }; 

     this.notifications.push(notification); 

     if(this.platform.is('cordova')){ 

       // Cancel any existing notifications 
       this.localNotifications.cancelAll().then(() => { 

        // Schedule the new notifications 
        this.localNotifications.schedule(this.notifications); 

        this.notifications = []; 

        let alert = this.alertController.create({ 
         title: 'Notifications set', 
         buttons: ['Ok'] 
        }); 

        alert.present(); 

       }); 

      } 



     console.log("Notifications to be scheduled: ", this.notifications); 

} 

Ich kann sehen, das folgende Objekt in der Konsole übergeben worden:

every:"minute" 
id:1 
text:"I am tester ?" 
title:"test" 

Es ist immer noch etwas zu zeigen, wenn die Schaltfläche geklickt wird . Weißt du, was zu tun ist?

Antwort

1

So erstellen wir eine einfache lokale Benachrichtigung.

  1. Installieren Sie das Plugin.

    $ ionic plugin add --save de.appplant.cordova.plugin.local-notification 
    $ npm install --save @ionic-native/local-notifications 
    

Incase Version 3 CLI verwenden

ionic cordova plugin add de.appplant.cordova.plugin.local-notification

  1. zu module.ts Datei hinzufügen

    import { LocalNotifications } from '@ionic-native/local-notifications'; 
    
  2. in die Providerliste

    hinzufügen
    providers: [ 
    SplashScreen, 
    LocalNotifications, 
    {provide: ErrorHandler, useClass: IonicErrorHandler} 
    ] 
    
  3. Einfuhr in die Seite, wo wir nutzen wollen und es so nutzen .. (zu Demonstrationszwecken, rief ich sie auf viewDidLoad Methode)

    export class HomePage { 
        constructor(public navCtrl: NavController,private localNotifications: 
         LocalNotifications) {} 
        ionViewDidLoad() { 
         console.log('ionViewDidLoad About2'); 
         this.localNotifications.schedule({ 
           id: 1, 
           text: 'Single ILocalNotification', 
           data: 'test' 
         }); 
        } 
    } 
    

und es auf realen testen Gerät. wenn Sie in Browser zu suchen, cordova nicht verfügbar ist, so dass es eine Warnung wie dieses

error

auf dem Gerät werfen, wil Ergebnis

result

+1

Ihre Lösung funktioniert, und ich glaube, ich tat, wie Gut. Anscheinend waren meine Plugins nicht richtig installiert. Wenn Sie Probleme haben, installieren Sie die Plugins in der Antwort und verwenden Sie die neueste Version des Ionic CLI. – krillebimbim

+0

@krillebimbim auch ich bin konfrontiert "Fehler bei der Installation 'de.appplant.cordova.plugin.local-notification': CordovaError: Fehler beim Abrufen des Plugins cordova-plugin-app-event über die Registrierung". Fehler beim Installieren des Benachrichtigungs-Plugins in ionic 3. Sind Sie gleichgesagt? Wie hast du es gelöst? – Thakur

Verwandte Themen