0

Ich bin völlig neu zu Push-Benachrichtigung in Ionic App. Jetzt kann ich Benachrichtigungen erhalten, wenn jemand eine Benachrichtigung sendet. Ich möchte in der Lage sein, Benachrichtigungen zu versenden, wenn der Benutzer eine Aktion ausführt. Gibt es eine Möglichkeit, Benachrichtigungen von TypeScript mit Cordova FCM zu senden?wie Push-Benachrichtigung senden mit angularJS und Cordova FCM

FCM Zuhörer

constructor(private fcm: FCM){ 
     fcm.subscribeToTopic('all'); 


    fcm.onNotification().subscribe(data=>{ 
    if(data.wasTapped){ 
     console.log("Received in background"); 
    } else { 
     console.log("Received in foreground"); 
    }; 
    }) 
} 
+0

Zum Senden einer FCM-Nachricht muss der FCM-Serverschlüssel verwendet werden. Dies bedeutet, dass es keine direkte Möglichkeit gibt, Push-Benachrichtigungen ** von ** einem Client sicher zu senden. Ein Beispiel für eine Möglichkeit, FCM-Nachrichten zu senden, finden Sie in diesem Blogpost (https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html). Weitere Informationen finden Sie in [dieses Beispiel in der Cloud Functions-Dokumentation] (https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens). –

Antwort

0

Hallo Firebase-Konto erstellen,

Get Sender ID

https://ionicframework.com/docs/native/push/ [installieren Plugin von hier]

beste Tutorial zur Implementierung Push für ionic2

https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59

  Setting up Ionic 2 App to generate device token 
      For Android, follow FCM setup instructions. It will give you SERVER_KEY and SENDER_ID. SERVER_KEY is used by server to send push notification and SENDER_ID is used by device to generate device token. For iOS, nothing required to generate device token. 


      Replace YOUR_SENDER_ID in config.xml with above SENDER_ID 


      <plugin name="phonegap-plugin-push" spec="1.8.2">  
      <variable name="SENDER_ID" value="YOUR_SENDER_ID"/> 
      </plugin> 

      Add device token generation code in your main app constructor like below and replace YOUR_SENDER_ID in Push.init() method with above SENDER_ID 


      import {Component, ViewChild} from "@angular/core"; 
      import {AlertController, Nav, Platform} 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 {TabsPage} from "../pages/tabs/tabs"; 
      import {DetailsPage} from "../pages/details/details"; 

      @Component({ 
      template: '<ion-nav [root]="rootPage"></ion-nav>' 
      }) 
      export class Ionic2PushApp { 
      @ViewChild(Nav) nav: Nav; 
      rootPage: any; 

      constructor(public platform: Platform, 
         public statusBar: StatusBar, 
         public splashScreen: SplashScreen, 
         public push: Push, 
         public alertCtrl: AlertController) { 
       this.rootPage = TabsPage; 
       platform.ready().then(() => { 
       this.statusBar.styleDefault(); 
       this.splashScreen.hide(); 
       this.initPushNotification(); 
       }); 
      } 

      initPushNotification() { 
       if (!this.platform.is('cordova')) { 
       console.warn("Push notifications not initialized. Cordova is not available - Run in physical device"); 
       return; 
       } 
       const options: PushOptions = { 
       android: { 
        senderID: "YOUR_SENDER_ID" 
       }, 
       ios: { 
        alert: "true", 
        badge: false, 
        sound: "true" 
       }, 
       windows: {} 
       }; 
       const pushObject: PushObject = this.push.init(options); 

       pushObject.on('registration').subscribe((data: any) => { 
       console.log("device token ->", data.registrationId); 
       //TODO - send device token to server 
       }); 

       pushObject.on('notification').subscribe((data: any) => { 
       console.log('message', data.message); 
       //if user using app and push notification comes 
       if (data.additionalData.foreground) { 
        // if application open, show popup 
        let confirmAlert = this.alertCtrl.create({ 
        title: 'New Notification', 
        message: data.message, 
        buttons: [{ 
         text: 'Ignore', 
         role: 'cancel' 
        }, { 
         text: 'View', 
         handler:() => { 
         //TODO: Your logic here 
         this.nav.push(DetailsPage, {message: data.message}); 
         } 
        }] 
        }); 
        confirmAlert.present(); 
       } else { 
        //if user NOT using app and push notification comes 
        //TODO: Your logic on click of push notification directly 
        this.nav.push(DetailsPage, {message: data.message}); 
        console.log("Push notification clicked"); 
       } 
       }); 

       pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error)); 
      } 
      } 
+0

Danke für die Verwendung von Code. Happy coding ... –

+0

Unten Code Hilfe, um Ihre Benachrichtigung auch im Vordergrund zu zeigen. android: { senderID: "YOUR_SENDER_ID", forceShow: true, symbol: 'icon', sound: true, vibrieren: true} –

Verwandte Themen