【问题标题】:cordova fcm plugin on Android data.wasTapped not workingAndroid data.wasTapped 上的cordova fcm插件不起作用
【发布时间】:2017-01-08 04:23:03
【问题描述】:

我正在尝试在 Android 上使用 cordova fcm 插件来实现 Firebase Cloud Messaging 发送的数据。我成功收到了通知,但是当我点击它们时,它们并没有发出我想要的提醒。

这是 index.js 中使用的代码:

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

这是我用来发送通知的 php 代码:

    function pushAndroidNotification ($productUrl, $message, $deviceToken) {
        $url = 'https://fcm.googleapis.com/fcm/send';

        $fields = array(
            'registration_ids' => array($deviceToken),
            'notification' => array(
                "title" => "rBUX", 
                "body" => $message,
                "icon" => "name_of_icon" ),
            'data' => array("url" => $productUrl)
        );
        $jfields = json_encode($fields);
        echo "\r\n<br>Post json:".$jfields;

        $headers = array(
            'Authorization: key = AIzaSyBPRoJ7zgmevPYIzoDweVgNTbmsPBW5ofo',
            'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jfields);
        $result = curl_exec($ch);
        if ($result === FALSE) {
            echo "\r\n<br>Notify Failed:".curl_error($ch);
            die('Curl failed: '.curl_error($ch));
        }else{
           echo "\r\n<br>Notify passed:".$result;
        }
        $jresponse = json_decode($result, true);

        curl_close($ch);

        return $jresponse;
    }

但是每次启动应用,我都会看到提示“onNotification回调成功注册:OK”,所以FCM插件没有完全禁用。 我想在点击通知时启动 url,但现在看来我什至无法使用数据。如果有人知道如何解决这个问题请告诉我,并请告诉我如何使用字符串化数据在 Cordova inAppBrowser 中启动 url,谢谢。

【问题讨论】:

    标签: android cordova firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    对于“cordova-plugin-fcm”插件,您需要在通知负载部分将click_action设置为FCM_PLUGIN_ACTIVITY

    我从他们的website得到以下信息:

    {
      "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
    }
    

    【讨论】:

    • 搞定了!我用这个来测试它:curl --header "Authorization: key=YOUR_API_KEY" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"your_token_or_topic\",\"notification\": {\"title\": \"Click Action Message\",\"text\": \"Sample message\",\"click_action\":\"FCM_PLUGIN_ACTIVITY\"}}"
    • +1 这行得通。我在应用内成功获取了事件,但是在我添加 click_action 参数之前,单击状态栏中的通知并没有给出任何事件。
    • 不知道我是怎么错过的,你帮了我很多,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多