phonegap pushwoosh 插件 registerDevice 回调函数永远不会在 android 上触发

Posted

技术标签:

【中文标题】phonegap pushwoosh 插件 registerDevice 回调函数永远不会在 android 上触发【英文标题】:phonegap pushwoosh plugin registerDevice callback functions never fire on android 【发布时间】:2014-05-18 14:15:50 【问题描述】:

我正在尝试使用 Pushwoosh 推送通知 phonegap 构建插件,但 registerDevice 回调永远不会在我的 android 设备上触发,但是 initPushwoosh 函数确实会触发,因为我在代码中看到了调用 alert("initPushwoosh" ) 的警报下面

下面是我的 config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns     = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        id        = "com.phonegap.hello-world"
        version   = "1.0.0">

    <name>Push notification</name>

    <description>
        Camera example app.
    </description>

    <author href="http://phonegap.com" email="support@phonegap.com">
        PhoneGap Team
    </author>

    <preference name="phonegap-version" value="3.3.0" /> 

    <gap:plugin name="org.apache.cordova.core.camera" />

     <plugin name="PushNotification"
        value="com.pushwoosh.plugin.pushnotifications.PushNotifications" onload="true"/>

     <access origin="*.pushwoosh.com" />
</widget>

这是我的index.js,我只是将项目id和appid更改为XXXX,以免我错误地透露太多。

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 explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() 
        alert("onDeviceReady" );
        app.initPushwoosh();
        app.receivedEvent('deviceready');

    ,

    initPushwoosh: function() 
        alert("initPushwoosh" );
            var pushNotification = window.plugins.pushNotification;
            pushNotification.registerDevice(
             projectid: "XXX-XXX-XXXX 3", appid : "XXXXX-XXXXX" ,
                function(status) 
                    var pushToken = status;
                    alert('push token: ' + pushToken);
                ,
                function(status) 
                    alert(JSON.stringify(['failed to register ', status]));
            );
            document.addEventListener('push-notification', function(event)             
                var title = event.notification.title;
                 var userData = event.notification.userdata;
                if (typeof(userData) != "undefined") 
                   alert('user data: ' + JSON.stringify(userData));
                    
                navigator.notification.alert(title);
            );
    ,
    // 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);
    ,

    takePicture: function() 
      navigator.camera.getPicture( function( imageURI ) 
        alert( imageURI );
      ,
      function( message ) 
        alert( message );
      ,
      
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI
      );
    
;

任何帮助将不胜感激...谢谢!

【问题讨论】:

【参考方案1】:

Cordova 已将 window.plugins 替换为函数 cordova.require() 您需要查找定义插件的命名空间。对于 pushwoosh,它是:“com.pushwoosh.plugins.pushwoosh.PushNotification”

所以而不是:

var PushNotification = window.plugins.PushNotification;

试试这个:

var PushNotification = cordova.require("com.pushwoosh.plugins.pushwoosh.PushNotification");

【讨论】:

【参考方案2】:

您使用的是最新版本的插件吗?如果是这样,请查看最新指南: http://www.pushwoosh.com/programming-push-notification/android/android-additional-platforms/phonegapcordova-sdk-integration/

最近改了,initPushwoosh函数长这样:

function initPushwoosh()

    var pushNotification = window.plugins.pushNotification;

    //set push notifications handler
    document.addEventListener('push-notification', function(event) 
        var title = event.notification.title;
        var userData = event.notification.userdata;

        if(typeof(userData) != "undefined") 
            console.warn('user data: ' + JSON.stringify(userData));
        

        navigator.notification.alert(title);
    );

    //initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
    pushNotification.onDeviceReady( projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID" );

    //register for pushes
    pushNotification.registerDevice(
        function(status) 
            var pushToken = status;
            console.warn('push token: ' + pushToken);
        ,
        function(status) 
            console.warn(JSON.stringify(['failed to register ', status]));
        
    );

您可能还需要仔细检查 AndroidManifest.xml。不幸的是,它真的很容易出错:((

【讨论】:

Shader,我正在使用 phonegap 构建,所以我的理解是我需要做的就是将这两行(如下)添加到 config.xl 文件中,并且 phonegap 构建将使用最新的 pushwoosh 插件并为我创建 AdroidManifest.xml 文件...您是否为此成功使用了 phonegap 构建? 问题是windows.plugins是未定义的,所以显然window.plugins.PushNotification是未定义的,应用程序在这条线上炸了 var pushNotification = window.plugins.PushNotification; 我明白了,对于 PGB,请使用原始代码,PGB 上的插件尚未更新(PGB 团队需要很长时间)。在 config.xml 中,您的输入不正确。它应该是:pushwoosh.com/programming-push-notification/android/… 我尝试在我的 config.xml 中同时使用两者,但我得到了相同的错误 window.splugin is not defined with either one

以上是关于phonegap pushwoosh 插件 registerDevice 回调函数永远不会在 android 上触发的主要内容,如果未能解决你的问题,请参考以下文章