PushNotification не определена ошибка - PullRequest
0 голосов
/ 22 мая 2018

Мне нужно отправить уведомление в мое приложение с помощью push-плагина phonegap и отправить уведомление из консоли Firebase.Я также развернул плагин для входа в систему Google Plus и создал свое приложение с помощью Phonegap Build.Когда мое приложение открывается, оно сначала проверяет логин Google, а затем продолжает действовать соответствующим образом.

НЕ МОЖЕТ ПОЛУЧИТЬ РЕГИСТРАЦИОННЫЙ ИД index.html

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="push.js"></script> 


var push = PushNotification.init({
                                          "android": {
                                            "senderID": "xxxxxxxxxx"
                                          },

                                        });

                                        push.on('registration', function(data) {
                                            alert('registration event: ' + data.registrationId);
                                            console.log('registration event: ' + data.registrationId);

                                          var oldRegId = localStorage.getItem('registrationId');
                                          if (oldRegId !== data.registrationId) {
                                            // Save new registration ID
                                            localStorage.setItem('registrationId', data.registrationId);
                                            // Post registrationId to your app server as the value has changed
                                          }

                                          var parentElement = document.getElementById('registration');
                                          var listeningElement = parentElement.querySelector('.waiting');
                                          var receivedElement = parentElement.querySelector('.received');

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

                                        push.on('error', function(e) {
                                            alert("push error = " + e.message);
                                            console.log("push error = " + e.message);
                                        });



                                        push.on('notification', function(data) {
                                          console.log('notification event');
                                          navigator.notification.alert(
                                            data.message,         // message
                                            null,                 // callback
                                            data.title,           // title
                                            'Ok'                  // buttonName
                                          );
                                        });

config.xml:

<?xml version='1.0' encoding='utf-8'?>

        <widget id="com.phonegap.myapp1" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
            <name>MyApp</name>
            <description>
              My App
            </description>
            <author email="support@phonegap.com" href="http://phonegap.com">
                sqlchild
            </author> 


                <preference name="android-minSdkVersion" value="17" /> 

                <preference name='phonegap-version' value='cli-8.0.0' />

                <preference name='pgb-builder-version' value='2' />



            <plugin name="cordova-plugin-splashscreen" source="npm" spec="5.0.2"/>  



                <hook src="scripts/cordova-google-services-version-gradle-fix.js" type="before_prepare" />


                <platform name="android">

                    <!-- <framework src="com.google.android.gms:play-services-gcm:+" /> -->
                    <!-- <framework src="com.google.android.gms:play-services-gcm:11.8.0" /> -->
                    <framework src="com.google.android.gms:play-services-gcm:9.0.0" />

                    <!-- <framework src="com.android.support:support-v4:+" /> -->
                    <!-- <framework src="com.android.support:support-v4:11.8.0" /> -->
                    <framework src="com.android.support:support-v4:9.0.0" />

                </platform>


        <plugin name="phonegap-plugin-push" spec="2.1.3">   
            <param name="SENDER_ID" value="x:xxxxx:android:xxxxxx" />
        </plugin> 

            <plugin name="cordova-plugin-googleplus" source="npm" spec="5.3.0"></plugin>  

            <platform name="android">   

                <resource-file src="app/google-services.json" target="app/google-services.json" />  

            </platform>      


            <content src="index.html" />

</widget>

1 Ответ

0 голосов
/ 22 мая 2018

Учитывая, что у вас установлен плагин Push .

Вы должны выполнить инициализацию push-уведомлений после события готовности устройства.

<!DOCTYPE html>
<html>
  <head>
    <title>Device Ready Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" src="push.js"></script> 

    // Wait for device API libraries to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // device APIs are available
    //
    function onDeviceReady() {
        // Now safe to use device APIs
        var push = PushNotification.init({
            "android": {
              "senderID": "xxxxxxxxxx"
            },
        });


    push.on('registration', function(data) {
        alert('registration event: ' + data.registrationId);
        console.log('registration event: ' + data.registrationId);

        var oldRegId = localStorage.getItem('registrationId');
        if (oldRegId !== data.registrationId) {
        // Save new registration ID
        localStorage.setItem('registrationId', data.registrationId);
        // Post registrationId to your app server as the value has changed
     }

    var parentElement = document.getElementById('registration');
    var listeningElement = parentElement.querySelector('.waiting');
    var receivedElement = parentElement.querySelector('.received');

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

    </script>
  </head>
  <body onload="onLoad()">
  </body>
</html>

После этого вы будетебыть в состоянии видеть reg ID в консоли.

edit 1 : - Руководство по установке push-плагина

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...