【发布时间】:2016-02-17 13:37:50
【问题描述】:
我添加了电话间隙推送插件和 ngcordova。
ngcordova.js 已在 cordova.js 之前导入
我在浏览器中收到$window.plugin not defined 错误,我认为这是预期的。但我在应用程序中收到 cannot read property register of undefined 错误,这意味着 $cordovaPush 未定义。
我看过this 教程并尝试实现相同的
angular
.module('init')
.run(function($ionicPlatform, $rootScope, $state, $cordovaPush, $cordovaToast){
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
window.StatusBar.styleDefault();
}
registerForPush();
function registerForPush(){
try {
var config = {};
//register for push notifications
if (ionic.Platform.isAndroid()) {
config = {
"senderID": "1232"
};
}else if (ionic.Platform.isIOS()) {
config = {
"badge": "true",
"sound": "true",
"alert": "true"
}
}
$cordovaPush.register(config).then(function (result) {
alert("Register success " + result);
$cordovaToast.showShortCenter('Registered for push notifications');
// ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here
if (ionic.Platform.isIOS()) {
//register
}
}, function (err) {
console.log("Register error " + err)
});
}
catch(err) {
alert(err.message);
}
}
// Notification Received
$rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
alert(JSON.stringify([notification]));
if (ionic.Platform.isAndroid()) {
if (notification.event == "registered") {
alert("android notification received token - " + notification.regid);
}
}
else if (ionic.Platform.isIOS()) {
alert("ios notification received");
}
});
document.addEventListener('resume', function () {
$rootScope.$broadcast('resume');
});
document.addEventListener('pause', function () {
$rootScope.$broadcast('pause');
});
document.addEventListener('offline', function () {
$rootScope.$broadcast('offline');
});
document.addEventListener('online', function () {
$rootScope.$broadcast('online');
});
window.addEventListener('batterystatus', function (status) {
$rootScope.$broadcast('batterystatus', status);
});
});
});
【问题讨论】:
标签: javascript angularjs cordova push-notification ionic-framework