当我尝试初始化PushNotification时,应用程序会抛出错误。
的index.html
<script src="cordova.js" type="text/javascript" charset="utf-8"></script>
<script src="js/push.js" type="text/javascript"></script>
<script src="js/app-config.js" type="text/javascript"></script>
<script src="js/app-main.js" type="text/javascript"></script>
应用-config.js
//**** set global variables required for push notification
// google project id for application
var googleProjectID = "XXXXXXX";
// cms key - do not change
var cmsKey = "XXXXXX";
// server to send registration to and from where notifications will be send
var cmsPushServerHost = "XXXXX";
// text id of cms "Application" article
var cmsPushApplicationId = "XXXXXX";
//Push Notification, GA Plugin
var gaPlugin, push;
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() {
app.receivedEvent('deviceready');
try {
//Push Notification
push = PushNotification.init({ android: {senderID: googleProjectID,}, ios: {alert: "true",badge: "true",sound: "true"}});
push.on('registration', function(data) {
window.localStorage.setItem("app_register_id", data.registrationId);
alert("Device Registered = " + data.registrationId);
});
push.on('notification', function(data) {
alert(data.title+" Message: " +data.message);
});
push.on('error', function(error) {
alert("PushNotification error : " + error);
});
//Google Analyics
}
catch(err) {
alert("Device Ready error : " + err);
}
},
// Update DOM on a Received Event
receivedEvent: function(id) {
},
addPushNotification: function() {
try
{
var deviceType;
if (device.platform === 'android' || device.platform === 'Android')
{
deviceType = "android";
}
else
{
deviceType = "ios";
}
if (typeof window.localStorage.getItem('app_register_id') !== "undefined" && window.localStorage.getItem('app_register_id') !== '' && window.localStorage.getItem('app_register_id') !== null)
{
if (typeof window.localStorage.getItem('settingUpdates') !== "undefined" && window.localStorage.getItem('settingUpdates') !== '' && window.localStorage.getItem('settingUpdates') !== null)
{
subscribePushNotifications(window.localStorage.getItem('app_register_id'), deviceType);
}
else
{
unsubscribePushNotification(window.localStorage.getItem('app_register_id'), deviceType);
}
}
}
catch(err) {
alert("addPushNotification error : " + err);
}
}
};
应用-main.js
//App Initialize, Push Notifications
app.initialize();
config.xml中
<access origin="*" />
<!-- Added the following intents to support the removal of whitelist code from base cordova to a plugin -->
<!-- Whitelist configuration. Refer to https://cordova.apache.org/docs/en/edge/guide_appdev_whitelist_index.md.html -->
<allow-navigation href="http://*/*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<plugin name="cordova-plugin-whitelist" source="npm" />
<plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />
<plugin name="cordova-plugin-inappbrowser" source="npm" spec="~1.3.0" />
<plugin name="phonegap-plugin-push" source="npm">
<param name="SENDER_ID" value="XXXXXXX" />
</plugin>
请你告诉我我做错了什么。现在一直在努力解决这个问题。
提前致谢