2017-02-20 13 views
1

FCMPlugin.onNotification()内のイオンアプリでAlertControllerを使用して警告を作成しようとしましたが、警告コントローラは作成されません。実際、このメソッドは停止したように見え、コードでアラートが作成された後は、console.log()はそれ以上動作しません。FCMPlugin Ionic 2で呼び出されたときにAlertControllerが機能しない

pushNoteSetup(){ 
if(typeof(FCMPlugin) !== "undefined"){ 
    FCMPlugin.getToken(function(t){ 
    console.log("Use this token for sending device specific messages\nToken: " + t); 

    }, function(e){ 
    console.log("Uh-Oh!\n"+e); 
    }); 

    this.confirmAlert('Hi'); 

    FCMPlugin.onNotification(
    function(d){ 
     if(d.wasTapped){ 
     // Background receival (Even if app is closed), 
     // bring up the message in UI 
     let message = d['aps']['alert']; 
     console.log('Message received: ' + message); 
     this.alert = this.alertCtrl.create({ 
      title: 'Hi', 
      message: 'Boo', 
      buttons: ['Ok'] 
     }); 
     this.alert.present(); 
     console.log('Should have displayed an alert'); 
     this.confirmAlert(message); 
     console.log('Skipping over alers?'); 
     } else { 
     let message = d['aps']['alert']; 
     console.log('Message received: ' + message); 
     let alert = this.alertCtrl.create({ 
      title: 'Hi', 
      message: 'Boo', 
      buttons: ['Ok'] 
     }); 
     alert.present(); 
     console.log('Should have displayed an alert'); 
     this.confirmAlert(message); 
     console.log('Skipping over alers?'); 
     this.confirmAlert(message); 
     } 
    }, function(msg){ 
     // No problemo, registered callback 
     console.log('Message:' + msg); 
    }, function(err){ 
     console.log("Arf, no good mate... " + err); 
    }); 
    } else { 

    console.log("Notifications disabled, only provided in Android/iOS environment"); 
    } 
} 
public confirmAlert(message: any){ 
let mesg = String(message); 

console.log('Message to display ' + mesg + ' and ' + message); 

let confirmAlert = this.alertCtrl.create({ 
     title: 'Alert', 
     message: message, 
     buttons: [{ 
     text: 'Cancel', 
     role: 'cancel', 
     handler:() => { 
      console.log('cancel'); 
     } 
     }, { 
     text: 'Confirm', 
     handler:() => { 
      console.log('Confirm'); 
     } 
     }] 
    }); 
    confirmAlert.present(); 

} 

これは(platform.ready後に呼ばれている)app.componenet.ts

答えて

1

にあなたはthisの値を変更するJavaScript functionを使用しています。関数のコンテキストを指します。

self = this; 

とwithingコールバック、

function(d){ 
     if(d.wasTapped){ 
     // Background receival (Even if app is closed), 
     // bring up the message in UI 
     let message = d['aps']['alert']; 
     console.log('Message received: ' + message); 
     self.alert = self.alertCtrl.create({ 
      title: 'Hi', 
      message: 'Boo', 
      buttons: ['Ok'] 
     }); 
     self.alert.present(); 
     //... 

か良い方法はスラジュが言ったようにJavaScriptを使用しているarrow function

FCMPlugin.onNotification(
    (d)=>{ 
    //create alert 
    }); 
0

使用することです:あなたは、としてコンテキストを保存しようとすることができます。

代わりにこのコードを使用してみてください:

if(typeof(FCMPlugin) !== "undefined"){ 


FCMPlugin.getToken(
       (token)=>{ 
        console.log(token); 
       }, 
       (err)=>{ 
        console.log('error retrieving token: ' + err); 
       } 
      ); 



    FCMPlugin.onNotification(
     (data)=>{ 
      if(data.wasTapped){ 
       //Notification was received on device tray and tapped by the user. 
       //Do something 
      }else{ 
       //Notification was received in foreground. Maybe the user needs to be notified. 
       this.alertBox = this.alertCtrl.create({ 
       title: data.title, 
       subTitle: data.body, 
       buttons: [{ 
       text: 'Cancel', 
       role: 'cancel', 
       handler:() => { 
       console.log('cancel'); 
       } 
       }, { 
        text: 'Confirm', 
        handler:() => { 
        console.log('Confirm'); 
        } 
       }] 
       });  
       this.alertBox.present();     
      } 
     }, 
     (msg)=>{ 
      console.log('onNotification callback successfully registered: ' + msg); 
     }, 
     (err)=>{ 
      console.log('Error registering onNotification callback: ' + err); 
     } 
    ); 
} 
else console.log("Notifications disabled, only provided in Android/iOS environment"); 
関連する問題