我正在开发phonegap v5.3.6和cordova v5.3.3。我在README上做了所有的事情,但是插件不起作用。
我的代码如下;
onDeviceReady: function() {
var push = PushNotification.init({
"android": {
"senderID": "MY_SENDER_ID"
},
"ios": {},
"windows": {}
});
push.on('registration', function(data) {
console.log("registration event");
document.getElementById("regId").innerHTML = data.registrationId;
console.log(JSON.stringify(data));
});
push.on('notification', function(data) {
console.log("notification event");
console.log(JSON.stringify(data));
var cards = document.getElementById("cards");
var push = '<div class="row">' +
'<div class="col s12 m6">' +
' <div class="card darken-1">' +
' <div class="card-content black-text">' +
' <span class="card-title black-text">' + data.title + '</span>' +
' <p>' + data.message + '</p>' +
' </div>' +
' </div>' +
' </div>' +
'</div>';
cards.innerHTML += push;
});
push.on('error', function(e) {
console.log("push error");
});
}
没有抛出任何成功或错误消息。这个暗语有什么问题?
这是插件的git repo: https://github.com/phonegap/phonegap-plugin-push
谢谢你的帮助
尝试使用推送通知链接。
首先注册你的设备,
pushNotification.register(
successHandler,
errorHandler,
{
"senderID":"replace_with_sender_id", //It should be your project id that you will get from Google Developer Console while registering the project with package name.
"ecb":"onNotification"
}
并像这样添加两个事件-成功和失败,
function successHandler (result) {
alert('result = ' + result); //Here you will get your device id.
}
function errorHandler (error) {
alert('error = ' + error);
}
同时,添加onNotification事件,当设备接收到通知时触发。
function onNotification(e){
alert(e.event);
}
对于寻找答案的人来说,目前Phonegap最好的推送通知插件是这里提到的:Phonegap推送通知插件。它在所有Android和iOS版本上运行良好。它的用法如下:
var push = PushNotification.init({
android: {
senderID: "XXXXXXXXXXXX",
},
ios: {
alert: "true",
badge: "true",
sound: "true",
}
});
push.on('registration', function(data) {
console.log(data.registrationId);
registerDeviceToken(data.registrationId);
});
push.on('notification', function(data) {
console.log("notification event");
alert(JSON.stringify(data));
});
push.on('error', function(e) {
console.log("push error");
alert(JSON.stringify(e));
});
function registerDeviceToken(deviceToken){
//Register the registrationId or deviceToken to your server as per the webservice type and parameters configuration set
}
在应用程序中触发'DeviceReady'事件后调用上述代码。在你的AndroidManifest文件中的配置将是(参考目的):
<application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.adobe.phonegap.push.PushHandlerActivity"
android:exported="true" />
<receiver android:name="com.adobe.phonegap.push.BackgroundActionButtonHandler" />
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="yourPackageName" />
</intent-filter>
</receiver>
<service
android:name="com.adobe.phonegap.push.GCMIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.adobe.phonegap.push.RegistrationIntentService"
android:exported="false" />
</application>
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
这些配置在安装插件时自动添加到AndroidManifest中。在iOS中,在项目的功能部分打开推送通知服务。同时确保将Google Developer Console for Android的正确Api密钥和Apple Push Notification service的p12文件及其iOS密码共享给服务器团队,以避免配置漏洞。