ple*_*shy 9 android push-notification phonegap-plugins cordova
我无法接收任何类型的回调用于phonegap构建的推送通知插件,我已将该插件包含在config.xml中.
我已经注册了GCM并获得了pushNotification.register()所需的项目编号.
我也可以访问window.plugins.pushNotification对象,所以我知道它包含了插件.
我的index.html js文件包括:
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript" src="js/lib/jquery.js" ></script>
<script type="text/javascript" src="js/lib/handlebars.js"></script>
<script type="text/javascript" src="js/handlebars/helpers.js"></script>
<script type="text/javascript" src="js/plugins/fastclick.js"></script>
<script type="text/javascript" src="js/app.js"></script>
Run Code Online (Sandbox Code Playgroud)
我的config.xml插件包括:
// plugins
<gap:plugin name="org.apache.cordova.console" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.inappbrowser" />
<gap:plugin name="org.apache.cordova.splashscreen" />
<gap:plugin name="com.phonegap.plugins.pushplugin" />
// access to external domains
<access origin="*"/>
Run Code Online (Sandbox Code Playgroud)
我的app.js调用pushNotification.register()
var app = {
init: function() {
document.addEventListener("deviceready", this.onDeviceReady, false);
},
onDeviceReady: function(){
// DO STUFF
// ....
// ENABLE PUSH
this.push_init();
},
push_init: function(){
app.SENDER_ID = 123456789; // replaced by my actual GCM project no
var pushNotification = window.plugins.pushNotification;
pushNotification.register(
function(){alert('Push: win');}, // never called
function(){alert('Push: Error');}, // never called
{ senderID: app.SENDER_ID, ecb: "app.push_android" }
);
},
// never called
push_android: function(e){
alert('connection established...');
console.log( 'successfully started android' );
console.log( e );
}
};
// start the app
app.init();
Run Code Online (Sandbox Code Playgroud)
在调用之后,没有执行任何操作,app.push_android()是app对象的一个功能.
如果我没有输入senderID,我会收到错误,说明没有发件人ID,所以我知道某些内容正在运行.这是多么令人沮丧的想法?
PS - 我也注意到一些奇怪的东西,当我调试window.log window.plugins.pushNotification它返回一个空对象,但是我仍然可以调用window.plugins.pushNotification.register(),但我想我会在控制台内看到日志.
我想我已经找到了解决方案。
我为对象中的属性 senderID 传递了一个整数而不是字符串
不起作用
pushNotification.register(
function(){alert('Push: win');}, // NOT called
function(){alert('Push: Error');}, // NOT called
{ senderID: 123456789, ecb: "app.push_android" }
);
Run Code Online (Sandbox Code Playgroud)
确实有效
pushNotification.register(
function(){alert('Push: win');}, // called
function(){alert('Push: Error');}, // called
{ senderID: "123456789", ecb: "app.push_android" }
);
Run Code Online (Sandbox Code Playgroud)