应用程序关闭/刷出时未提供Firebase推送通知,但在应用程序处于后台时工作

时间:2017-02-07 06:39:12

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

我正在学习cordova并尝试制作一个具有推送通知服务的简单cordova应用程序。我正在使用插件[cordova-plugin-firebase] [1]进行服务。我只使用php脚本serverSide.php发送数据有效负载。如果应用程序正在运行,即使应用程序在后台,我也会将推送通知发送到我的手机。但是当我关闭应用程序时,我没有收到任何推送通知。我已经使用了自动启动功能,它可以在后台启动,但是当应用程序关闭时我仍然无法获得推送通知。他们是firebaseNotification的任何监听器以及它们驻留在哪个文件上?从插件页面,我使用了代码

window.FirebasePlugin.onNotificationOpen(function(notification) {
    console.log(notification);
}, function(error) {
    console.error(error);
});

并且已经放入我的js / index.js文件中。我应该在插件提供的MyFirebaseMessagingService.java文件中编辑一些内容吗?

这是myserverSide.php文件

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', ' my api access key' );


$registrationIds = array( ' registration id of a particular device' );

// prep the bundle
$msg = array
(
    'message'   => 'here is a message1. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'click_action' => 'FCM_PLUGIN_ACTIVITY',  
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
    ,'time_to_live' => 0 
    ,'priority' => 'high'
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);



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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' ); 
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

我的js / index.js如下所示。

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');

        // I added the below part starts
        cordova.plugins.autoStart.enable();

        window.FirebasePlugin.getToken(function(token) {
            // save this server-side and use it to push notifications to this device
            console.log(token);
            alert('token is'+token);
            cordova.plugins.clipboard.copy(token);          
            document.getElementById("token_caps").innerHTML = token;
            document.getElementById("token").innerHTML = token;
        }, function(error) {
            console.error(error);
        });     


        window.FirebasePlugin.onNotificationOpen(function(notification) {
            console.log(notification);
            //alert(JSON.stringify(notification));
            alert('message is '+notification.message+' title is '+notification.title);
            document.getElementById("fcm_notification").innerHTML = JSON.stringify(notification);

        }, function(error) {
            console.error(error);
        }); 

        // I added the above part ends  




    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);


    }
};

app.initialize(); 

我的index.html看起来像这样

<!DOCTYPE html>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
    <head>
        <!--
        Customize this policy to fit your own app's needs. For more guidance, see:
            https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
        Some notes:
            * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
            * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
            * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
                * Enable inline JS: add 'unsafe-inline' to default-src
        -->
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
                <p id="fcm_notification"> This is for push notification message</p><br><br>
                <p id="token_caps" style="text-transform:uppercase"> This is for token</p><br><br>
                <p id="token" > This is for token</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>


  [1

]:https://www.npmjs.com/package/cordova-plugin-firebase

0 个答案:

没有答案