2017-10-11 7 views
0

Eine Benachrichtigung wird angezeigt, aber beim Klicken auf diese Schaltfläche wird die Anwendung nur erneut geöffnet. Was ich möchte, ist beim Klicken auf die Benachrichtigung, öffnet es einen bestimmten Artikel.Ionic 2: Push-Benachrichtigung bei Klick

In Laravel, ich bin mit dem brozot/Laravel-FCM Paket für Firebase Cloud Messaging (FCM) Benachrichtigungen zu senden, und am anderen Ende, ich bin mit Ionic push notifications Benachrichtigungen in dem Infobereich zu empfangen und anzuzeigen.

Wenn ich auf Laravel setClickAction() nicht verwende, wird die Ionic-Anwendung beim Klicken auf die Benachrichtigung geöffnet, aber wenn ich setClickAction() einstelle, passiert nichts. Die Benachrichtigung verschwindet einfach.

Laravel-Code:

$notificationBuilder = new PayloadNotificationBuilder('my title'); 
$notificationBuilder->setBody('Hello world') 
        ->setSound('default') 
        ->setClickAction('window.doSomething'); 
$notification = $notificationBuilder->build(); 

Ionic 2 Rahmen Probe:

import { Component, ViewChild } from '@angular/core'; 
import { Platform, Nav, MenuController, ModalController, Events, AlertController } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { Push, PushObject, PushOptions } from '@ionic-native/push'; 
import { Storage } from '@ionic/storage'; 

import { 
    SearchPage 
} from '../pages/pages'; 

@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 

    rootPage: any = SearchPage; 

    constructor(
     platform: Platform, 
     statusBar: StatusBar, 
     splashScreen: SplashScreen, 
     private menu: MenuController, 
     private modalCtrl: ModalController, 
     private events: Events, 
     private push: Push, 
     private alertCtrl: AlertController, 
     private storage: Storage 
    ) { 

     platform.ready().then(() => { 
      // Okay, so the platform is ready and our plugins are available. 
      // Here you can do any higher level native things you might need. 
      statusBar.styleDefault(); 
      splashScreen.hide(); 
     }); 
     this.pushSetup(); 
    } 

    pushSetup() { 
     const options: PushOptions = { 
      android: { 
       senderID: 'xxxxxxxxxxx', 
       forceShow: true 
      }, 
      ios: { 
       senderID: 'xxxxxxxxxxx', 
       alert: 'true', 
       badge: true, 
       sound: 'true' 
      }, 
      windows: {}, 
      browser: { 
       pushServiceURL: 'http://push.api.phonegap.com/v1/push' 
      } 
     }; 

     const pushObject: PushObject = this.push.init(options); 

     pushObject.on('notification').subscribe((notification: any) => { 

     }); 

     pushObject.on('registration').subscribe((registration: any) => { 
      alert(registration.id); 
     }); 

     pushObject.on('error').subscribe(error => alert('Error with Push plugin' + error)); 

    } 

} 

(<any>window).doSomething = function() { 
    alert('doSomething called'); 
} 

Was bin ich?

+0

ich implementiert das gleiche Funktionalität in Ionic mit One-Signal und Firebase (beide sind frei). Wenn Sie bereit sind, Plugin zu ändern, wird es als Antwort zur Verfügung stellen – Webruster

+0

Ja, bitte, ich bin bereit – MTA

+0

Bitte überprüfen Sie meine Lösung – Webruster

Antwort

0

Es gibt diese Schritte, die für die allgemeine One-Signal-Push-Benachrichtigung durchgeführt werden müssen

  1. ein OneSignal Konto
  2. Add New APP in dem einem Signal erstellen umgesetzt werden, für Android konfigurieren zuerst (Sie können sich auf jede Plattform konzentrieren, aber ich konzentriere mich ab sofort auf Android). Sie müssen den Google Server-Schlüssel und die Google-Projekt-ID erhalten.

  3. Sie können die oben genannten Schlüssel aus der Firebase this Schritte

  4. nun mit der Konfiguration des OneSignal Konto, jetzt integriert mit dem ionic mit dem cordova plugin

In Ionic2 getan, was wir verwenden, erhalten:

OneSignal.startInit(//google Server Key, //Google ProjectId); 
      OneSignal.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification); 
      OneSignal.setSubscription(true); 
      OneSignal.handleNotificationReceived().subscribe(() => { 
       // handle received here how you wish. 


       // this.goToReleatedPage(data.Key, data.Value); 
      }); 
      OneSignal.handleNotificationOpened().subscribe((data: any) => { 
       //console.log('MyData'+ JSON.stringify(data.additionalData)); 
       this.parseObject(data); 
      }); 

      OneSignal.endInit(); 

ParsingObject in Ionic

public parseObject(obj) { 
     for (var key in obj) { 
      this.goToReleatedPage(key, obj[key]); 
      if (obj[key] instanceof Object) { 
       this.parseObject(obj[key]); 
      } 
     } 
    } 

goToReleatedPage Methode

public goToReleatedPage(Key, Value) { 
     //console.log("Pagename"+" " + Key + "ID" +" " + Value); 
     if (Key === 'xxxx') { 
      this.navCtrl.push(xxxPage, { 
       id: Value 
      }); 
     } else if (Key === 'Foo') { 
      this.navCtrl.push(foosPage, { 
       id: Value, 

      }); 
     } else if (Key === 'bar') { 
      this.navCtrl.push(barPage, { 
       id: Value 
      }); 
     } 

    } 

Während die Nachricht von OneSignal senden, müssen Sie angeben, welche page Sie öffnen müssen, und Sie wollen Id passieren wie folgt

enter image description here