在 FCMPlugin Ionic 2 中调用时,AlertController 不起作用

Posted

技术标签:

【中文标题】在 FCMPlugin Ionic 2 中调用时,AlertController 不起作用【英文标题】:AlertController not working when called in FCMPlugin Ionic 2 【发布时间】:2017-07-09 10:27:21 【问题描述】:

尝试在 FCMPlugin.onNotification() 内的 ionic 应用程序中使用 AlertController 创建警报,但未创建警报控制器。实际上,在代码中创建警报后,该方法似乎停止并且不再有 console.log() 工作。

  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 中的 platform.ready() 之后调用的

【问题讨论】:

【参考方案1】:

您正在使用 javascript function 更改 this 的值。它将指向函数上下文。您可以尝试将上下文保存为:

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

【讨论】:

【参考方案2】:

你正在使用 suraj 说的 javascript。

尝试使用此代码:

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

【讨论】:

以上是关于在 FCMPlugin Ionic 2 中调用时,AlertController 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

构建android应用程序时的cordova FCMplugin问题

在父 html 中单击时,在 Angular 2/Ionic 中调用子方法

ionic 3:在 android fcm.onNotification() 中,当应用程序处于后台时,点击通知时不会调用

如何修复 Phonegap 中的“FCMPlugin Ready ERROR”?

调用GET HTTP时android上的ionic3错误

Ionic 2 MEAN 应用程序不会在获取请求时返回更新的数据