0

我是在Ionic App中推送通知的全新手段。現在,當有人發送通知時,我可以收到通知。我希望能夠在用戶進行某些操作時推送通知。有沒有辦法使用Cordova FCM發送來自TypeScript的通知?如何通過angularJS和cordova FCM發送推送通知

FCM聽衆

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

發送FCM消息需要使用FCM服務器密鑰。這意味着沒有直接的方法來安全地從**客戶端發送推送通知**。有關發送FCM消息的一種方式的示例,請閱讀[此博客文章](https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html)。另請參閱[Cloud Functions文檔中的此示例](https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens)。 –

回答

0

嗨創建火力地堡帳戶,

獲取發件人ID

https://ionicframework.com/docs/native/push/ [從這裏安裝插件]

最佳教程,以實現推動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

感謝您使用代碼。快樂編碼... –

+0

下面的代碼有助於在前臺顯示您的通知。 android:{ senderID:「YOUR_SENDER_ID」, forceShow:true, icon:'icon', sound:true, vibrate:true} –