Pushwoosh和Android Phonegap App无法通信

时间:2013-04-04 22:55:08

标签: android cordova push-notification google-cloud-messaging pushwoosh

问题

好的,我有一个带Pushwoosh的Phonegap(2.3.0)Android应用程序来管理推送通知。我按照说明操作,得到了一个只有几个措辞错误的应用程序。该应用程序本身似乎运行得非常好,但看起来Pushwoosh似乎没有向GCM(谷歌云Messanger)推送任何东西,它转而没有向应用推送任何东西。现在这是我认为错的,但这是我第一次使用GCM和Pushwoosh。

在Pushwoosh控制台页面上,它显示每个“推送”已完成且没有错误,但随后在我的GCM控制台上它没有显示任何请求,也没有在我的手机上弹出任何通知。

我的XML中有GCM API密钥(服务器密钥),而我的Pushwoosh是正确的XXXXX-XXXXX密钥。这是我的代码,以更好地概述我的代码是如何设置的(也许有人可以看到我缺少的东西)。


守则

这是AndroidManifest.xml

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

<supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:resizeable="true"
    android:anyDensity="true"
    />


<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<!--library-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>

 <!--
Creates a custom permission so only this app can receive its messages.

NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
    where PACKAGE is the application's package name.
-->
<permission
     android:name="com.springmobile.employee.permission.C2D_MESSAGE"
     android:protectionLevel="signature"/>
<uses-permission
     android:name="com.springmobile.employee.permission.C2D_MESSAGE"/>

<!-- This app has permission to register and receive data message. -->
<uses-permission
     android:name="com.google.android.c2dm.permission.RECEIVE"/>



<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:allowBackup="true">

  <!--
Service for sending location updates
-->
<service android:name="com.arellomobile.android.push.GeoLocationService"/>
<intent-filter>
     <action android:name="com.springmobile.employee.MESSAGE"/>
     <category android:name="android.intent.category.DEFAULT"/>
 </intent-filter>
 <intent-filter>
     <action android:name="android.intent.action.MAIN"/>
     <category android:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
    <activity android:name="com.arellomobile.android.push.PushWebview"/>

    <activity android:name="com.arellomobile.android.push.MessageActivity"/>

    <activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>

    <!--
    BroadcastReceiver that will receive intents from GCM
    services and handle them to the custom IntentService.

    The com.google.android.c2dm.permission.SEND permission is necessary
    so only GCM services can send data messages for the app.
    -->
    <receiver
    android:name="com.google.android.gcm.GCMBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
    <!-- Receives the actual messages. -->
    <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
    <!-- Receives the registration id. -->
    <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
    <category android:name="com.springmobile.employee"/>
    </intent-filter>
    </receiver>

    <!--
    Application-specific subclass of PushGCMIntentService that will
    handle received messages.
    -->
    <service android:name="com.arellomobile.android.push.PushGCMIntentService"/>
    <activity android:name="org.apache.cordova.example.cordovaExample" android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar"
            android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


这是我的Phonegap插件config.XML

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

这是我的Phonegap页面的JS,用于初始化和监听Pushwoosh通知。

function initPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
pushNotification.onDeviceReady();

pushNotification.registerDevice({ projectid: "I_HAVE_MY_PROJECT_ID_HERE", appid : "THIS_IS_MY_PUSHWOOSH_ID" },
    function(status) {
        var pushToken = status;
        console.warn('push token: ' + pushToken);
    },
    function(status) {
        console.warn(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") {
        console.warn('user data: ' + JSON.stringify(userData));
    }

    navigator.notification.alert(title);
});
}

function init() {
document.addEventListener("deviceready", initPushwoosh, true);

//rest of the code
document.addEventListener('push-notification', function(event) {
    var title = event.notification.title;
        var userData = event.notification.userdata;

    console.warn('user data: ' + JSON.stringify(userData));
    navigator.notification.alert(title);
});
}

function initPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
pushNotification.onDeviceReady();

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);

                            pushNotification.stopGeoPushes();
                          });
}

function registerPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
//projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID"
pushNotification.registerDevice({ projectid: "1039894503284", appid : "EE861-B95A3" },
                                function(token) {
                                    alert(token);
                                    onPushwooshInitialized(token);
                                },
                                function(status) {
                                    alert("failed to register: " +  status);
                                    console.warn(JSON.stringify(['failed to register ', status]));
                                });
}

function unregisterPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
pushNotification.unregisterDevice(function(token) {
                                    alert("unregistered, old token " + token);
                                },
                                function(status) {
                                    alert("failed to unregister: " +  status);
                                    console.warn(JSON.stringify(['failed to unregister ', status]));
                                });
}

//set the settings for Pushwoosh or set tags, this must be called only after successful registration
function onPushwooshInitialized(pushToken)
{
//output the token to the console
console.warn('push token: ' + pushToken);

var pushNotification = window.plugins.pushNotification;

//set multi notificaiton mode
//pushNotification.setMultiNotificationMode();

//set single notification mode
//pushNotification.setSingleNotificationMode();

//disable sound and vibration
//pushNotification.setSoundType(1);
//pushNotification.setVibrateType(1);

pushNotification.setLightScreenOnNotification(false);

//goal with count
//pushNotification.sendGoalAchieved({goal:'purchase', count:3});

//goal with no count
//pushNotification.sendGoalAchieved({goal:'registration'});

//setting list tags
//pushNotification.setTags({"MyTag":["hello", "world"]});

//settings tags
pushNotification.setTags({deviceName:"hello", deviceId:10},
                                function(status) {
                                    console.warn('setTags success');
                                },
                                function(status) {
                                    console.warn('setTags failed');
                                });

function geolocationSuccess(position) {
    pushNotification.sendLocation({lat:position.coords.latitude, lon:position.coords.longitude},
                             function(status) {
                                  console.warn('sendLocation success');
                             },
                             function(status) {
                                  console.warn('sendLocation failed');
                             });
};

// onError Callback receives a PositionError object
//
function geolocationError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

function getCurrentPosition() {
    navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);
}

//greedy method to get user position every 3 second. works well for demo.
//  setInterval(getCurrentPosition, 3000);

//this method just gives the position once
//  navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError);

//this method should track the user position as per Phonegap docs.
//  navigator.geolocation.watchPosition(geolocationSuccess, geolocationError, { maximumAge: 3000, enableHighAccuracy: true });

//Pushwoosh Android specific method that cares for the battery
pushNotification.startGeoPushes();
}

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() {
    initPushwoosh();
    app.receivedEvent('deviceready');

    //optional: create local notification alert
    //var pushNotification = window.plugins.pushNotification;
//pushNotification.clearLocalNotification();
//pushNotification.createLocalNotification({"msg":"message", "seconds":30, "userData":"optional"});

},
// 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);
}
};

这是PushWoosh.js代码

(function(cordova) {

function PushNotification() {}

// Call this to register for push notifications and retreive a deviceToken
PushNotification.prototype.registerDevice = function(config, success, fail) {
    cordova.exec(success, fail, "PushNotification", "registerDevice", config ? [config] : []);
};

// Call this to set tags for the device
PushNotification.prototype.setTags = function(config, success, fail) {
    cordova.exec(success, fail, "PushNotification", "setTags", config ? [config] : []);
};

// Call this to send geo location for the device
PushNotification.prototype.sendLocation = function(config, success, fail) {
    cordova.exec(success, fail, "PushNotification", "sendLocation", config ? [config] : []);
};

//Android Only----
PushNotification.prototype.unregisterDevice = function(success, fail) {
    cordova.exec(success, fail, "PushNotification", "unregisterDevice", []);
};

//config params: {msg:"message", seconds:30, userData:"optional"}
PushNotification.prototype.createLocalNotification = function(config, success, fail) {
    cordova.exec(success, fail, "PushNotification", "createLocalNotification", config ? [config] : []);
};

PushNotification.prototype.clearLocalNotification = function() {
    cordova.exec(null, null, "PushNotification", "clearLocalNotification", []);
};

//advanced background task to track device position and not drain the battery
PushNotification.prototype.startGeoPushes = function(success, fail) {
    cordova.exec(success, fail, "PushNotification", "startGeoPushes", []);
};

PushNotification.prototype.stopGeoPushes = function(success, fail) {
    cordova.exec(success, fail, "PushNotification", "stopGeoPushes", []);
};

//sets multi notification mode on
PushNotification.prototype.setMultiNotificationMode = function(success, fail) {
    cordova.exec(success, fail, "PushNotification", "setMultiNotificationMode", []);
};

//sets single notification mode
PushNotification.prototype.setSingleNotificationMode = function(success, fail) {
    cordova.exec(success, fail, "PushNotification", "setSingleNotificationMode", []);
};

//type: 0 default, 1 no sound, 2 always
PushNotification.prototype.setSoundType = function(type, success, fail) {
    cordova.exec(success, fail, "PushNotification", "setSoundType", [type]);
};  

//type: 0 default, 1 no vibration, 2 always
PushNotification.prototype.setVibrateType = function(type, success, fail) {
    cordova.exec(success, fail, "PushNotification", "setVibrateType", [type]);
};  

PushNotification.prototype.setLightScreenOnNotification = function(on, success, fail) {
    cordova.exec(success, fail, "PushNotification", "setLightScreenOnNotification", [on]);
};

//set to enable led blinking when notification arrives and display is off
PushNotification.prototype.setEnableLED = function(on, success, fail) {
    cordova.exec(success, fail, "PushNotification", "setEnableLED", [on]);
};

//{goal:'name', count:3} (count is optional)
PushNotification.prototype.sendGoalAchieved = function(config, success, fail) {
    cordova.exec(success, fail, "PushNotification", "sendGoalAchieved", config ? [config] : []);
};

//Android End----

//iOS only----
PushNotification.prototype.onDeviceReady = function() {
    cordova.exec(null, null, "PushNotification", "onDeviceReady", []);
};

// Call this to get a detailed status of remoteNotifications
PushNotification.prototype.getRemoteNotificationStatus = function(callback) {
    cordova.exec(callback, callback, "PushNotification", "getRemoteNotificationStatus", []);
};

// Call this to set the application icon badge
PushNotification.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
    cordova.exec(callback, callback, "PushNotification", "setApplicationIconBadgeNumber", [{badge: badge}]);
};

// Call this to clear all notifications from the notification center
PushNotification.prototype.cancelAllLocalNotifications = function(callback) {
    cordova.exec(callback, callback, "PushNotification", "cancelAllLocalNotifications", []);
};
//iOS End----

// Event spawned when a notification is received while the application is active
PushNotification.prototype.notificationCallback = function(notification) {
    var ev = document.createEvent('HTMLEvents');
    ev.notification = notification;
    ev.initEvent('push-notification', true, true, arguments);
    document.dispatchEvent(ev);
};

cordova.addConstructor(function() {
    if(!window.plugins) window.plugins = {};
    window.plugins.pushNotification = new PushNotification();
});

})(window.cordova || window.Cordova || window.PhoneGap);

GCM,Pushwoosh和Eclipse的截图

对不起,这很冗长,但我想确保我在这里有一切。也没有足够的代表发布图片所以下面的链接。

GCM API server key, Pushwoosh Control Panel, and Eclipse setup


谢谢!

3 个答案:

答案 0 :(得分:1)

允许check您的配置和设置适用于推送woosh通知。Other推送通知服务也适用于电话间隙,您可以查看它。您可以找到电话间隙代码(zip) ,适当的配置和设置文档以及如何使用Java脚本使用他们的API

答案 1 :(得分:1)

Pushwoosh版本在使用phonegap应用程序进行配置时非常重要,因此您需要确保Pushwoosh脚本与Phonegap 2.3.0兼容。如果Pushwoosh脚本不正确,它将不适用于您的Phonegap版本,我认为他们现在只支持最新的Phonegap。

GCM API密钥必须用于Android应用程序,而不是服务器

答案 2 :(得分:0)

我遇到了这个问题,因为我的捆绑ID并不正确。 请确保您的应用名称和捆绑ID在xcode和https://developer.apple.com/account/ios/identifiers中的iOS应用标识符中相同。