离子FCM总是错误的

时间:2016-11-28 06:31:32

标签: javascript firebase ionic-framework firebase-cloud-messaging

我在Ionic框架中使用带有cordova fcm插件的Firebase Notification。我想在用户收到推送通知并点击它时打开一个状态。我正在使用params从Firebase控制台发送通知,但它总是 data.wasTapped false,这就是为什么它不起作用。

FCMPlugin.onNotification(function(data) 
{
    if (data.wasTapped) {
        //Notification was received on device tray and tapped by the user.
        console.log("Tapped: " + JSON.stringify(data));
        if (data.hasOwnProperty('state')) {
            $timeout(function(){
                $state.go(data.state);
            })

        }

    } 
    else 
    {
        //if user already opened app
        console.log("Not tapped: " + JSON.stringify(data));
    }
}, function(msg) {
    console.log('onNotification callback successfully registered: ' + msg);
}, function(err) {
    console.log('Error registering onNotification callback: ' + err);
});

1 个答案:

答案 0 :(得分:12)

您需要根据 cordova-plugin-fcm https://github.com/fechanique/cordova-plugin-fcm)在您的有效负载中发送"click_action":"FCM_PLUGIN_ACTIVITY",但在firebase控制台中,没有空间可以发送此属性。所以你必须手动发送它。

示例

//POST: https://fcm.googleapis.com/fcm/send
//HEADER: Content-Type: application/json
//HEADER: Authorization: key=AIzaSy*******************
{
  "notification":{
    "title":"Notification title",  //Any value
    "body":"Notification body",  //Any value
    "sound":"default", //If you want notification sound
    "click_action":"FCM_PLUGIN_ACTIVITY",  //Must be present for Android
    "icon":"fcm_push_icon"  //White icon Android resource
  },
  "data":{
    "param1":"value1",  //Any data to be retrieved in the notification callback
    "param2":"value2"
  },
    "to":"/topics/topicExample", //Topic or single device
    "priority":"high", //If not set, notification won't be delivered on completely closed iOS app
    "restricted_package_name":"" //Optional. Set for application filtering
}

在Play商店中有一个我发现发送Firebase通知的Android应用程序。希望这有助于您发送Firebase通知https://play.google.com/store/apps/details?id=com.learn24bd.fcm