2016-08-08 117 views
2

我正在尝试使用cordova来实现Firbase推送通知。我从这里使用代码为最新的fcm插件说明:Cordova Push Plugin使用cordova的FireBase推送通知

我可以获取注册令牌。然后我尝试使用该令牌从Firebase测试通知模块发送通知。每次我在我的设备运行应用程序,我具有alert-

“消息:onNotification成功注册的回调:OK”

是FCMPlugin.onNotification事件的第二个函数内。

但是第一个函数[我想获取通知的地方]没有被调用。 我没有找到我犯错的地方。这里是我的代码里面onDeviceReady:

function onDeviceReady() { 
    // Handle the Cordova pause and resume events 
    document.addEventListener('pause', onPause.bind(this), false); 
    document.addEventListener('resume', onResume.bind(this), false); 

    // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here. 
    var parentElement = document.getElementById('deviceready'); 
    var listeningElement = parentElement.querySelector('.listening'); 
    var receivedElement = parentElement.querySelector('.received'); 
    listeningElement.setAttribute('style', 'display:none;'); 
    receivedElement.setAttribute('style', 'display:block;'); 

    //========================= 
    FCMPlugin.getToken(
     function (token) { 
      alert("Token: " + token); 
       cordova.plugins.email.open({ 
        to: '[email protected]', 
        subject: 'Greetings', 
        body: token 
       }); 
     }, 
     function (err) { 
      alert("Error: " + 'error retrieving token: ' + err); 
     } 
    ); 

    FCMPlugin.onNotification(
     function (data) { 
      alert("Notify: " + JSON.stringify(data)); 
      if (data.wasTapped) { 
       //Notification was received on device tray and tapped by the user. 
       alert("Wrapped Notify: " + JSON.stringify(data)); 
      } else { 
       //Notification was received in foreground. Maybe the user needs to be notified. 
       alert("Notify: " + JSON.stringify(data)); 
      } 
     }, 
     function (msg) { 
      alert("Msg: " + 'onNotification callback successfully registered: ' + msg.Notification); 
     }, 
     function (err) { 
      alert("Error: " + 'Error registering onNotification callback: ' + err); 
     } 
    ); 
}; 
+0

嘿,你得到任何解决办法?我的FCM通知已成功传递,但我的问题是当我点击通知时我还想获得警报,同时我使用的是同一个插件,您使用的是哪一个。如果你解决了然后PLZ告诉我。 –

+0

@ KAUSHAL:我更换了插件。以下插件适用于我: [link](https://lokesh-patel.blogspot.com/2016/06/cordova-plugin-firebase-cloud -messaging.html?showComment = 1470646658688#c897350184092951555) –

回答

1

你缺少前onNotification功能这样来订阅你的话题:

FCMPlugin.subscribeToTopic('topic'); 
2

请务必添加"click_action":"FCM_PLUGIN_ACTIVITY"有效载荷的REST API。这必须适用于Android。如果这不可用,您将不会从轻按通知(或听到声音)接收数据。

从科尔多瓦-插件-FCM文档资料请参阅REST API的有效载荷例如:

//POST: https://fcm.googleapis.com/fcm/send 
//HEADER: Content-Type: application/json 
//HEADER: Authorization: key=AIzaSy******************* 
{ 
    "notification":{ 
    "title":"Notification title", 
    "body":"Notification body", 
    "sound":"default", 
    "click_action":"FCM_PLUGIN_ACTIVITY", // <<<<<<< Must be present for Android 
    "icon":"fcm_push_icon" 
    }, 
    "data":{ 
    "param1":"value1", 
    "param2":"value2" 
    }, 
    "to":"/topics/topicExample", 
    "priority":"high", 
    "restricted_package_name":"" 
}