2017-02-20 56 views
1

試圖在FCMPlugin.onNotification()中的離子應用程序中使用AlertController創建警報,但未創建警報控制器。事實上,該方法似乎停止,並且在代碼中創建警報之後不再有console.log()工作。AlertController在FCMPlugin Ionic 2中調用時不起作用

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(); 

} 

這是正在app.componenet.ts

回答

1

您正在使用javascript function這改變的this值platform.ready()之後調用。它將指向功能上下文。你可以嘗試到上下文保存爲:

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(); 
     //... 

或者更好的辦法是arrow function

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

您正在使用JavaScript作爲SURAJ說要用。

嘗試使用此代碼來代替:

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");