PushNotification未定义(ng-cordova) [英] PushNotification is undefined (ng-cordova)
问题描述
在我的离子应用程序中,当在浏览器中运行应用程序时,出现以下错误
In my ionic application when running the application in browser i get following error
ng-cordova.js:6378 Uncaught ReferenceError: PushNotification is not defined
在我的控制台中,以及为Android构建应用程序并运行时在我的手机中,它在那里也无法正常工作,即没有弹出
来注册ID: alert(data.registrationId);
。
in my console, as well as when i build the application for android and run in my phone it doesnot work there as well i.e there is no popup
for registration Id : alert(data.registrationId);
.
app.run(function($ionicPlatform, $cordovaPushV5) {
$ionicPlatform.ready(function() {
// For Push Notification
var options = {
android: {
senderID: "121XXXXXXX49"
},
ios: {
alert: "true",
badge: "true",
sound: "true"
}
};
// initialize
$cordovaPushV5.initialize(options).then(function() {
// start listening for new notifications
$cordovaPushV5.onNotification();
// start listening for errors
$cordovaPushV5.onError();
// register to get registrationId
$cordovaPushV5.register().then(function(data) {
// `data.registrationId` save it somewhere;
alert(data.registrationId);
})
});
// triggered every time notification received
$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data){
alert(data.title + ' - '+ data.message);
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});
// triggered every time error occurs
$rootScope.$on('$cordovaPushV5:errorOcurred', function(event, e){
alert(e.message);
// e.message
});
});
})
推荐答案
如果您在ngCordova页面上看到 Cordova插件在您的浏览器中开发时不起作用的消息。这就是为什么它无法在浏览器中工作的原因。
If you look at ngCordova page it is said "Cordova plugins do not work while developing in your browser". This is the reason why it is not working in the browser.
在此处查看链接: http:// ngcordova.com/docs/common-issues/
在初始化插件之前,您应该检查应用程序是否正在设备(不在浏览器中)和离子平台上运行准备好了。然后,如果您正确遵循链接,就可以了。
Before initializing the plugin you should check if app is running on device (not in browser) and ionic platform is ready. Then if you follow the link properly you are good to go.
链接: https://github.com/phonegap/phonegap-plugin-push
我的个人建议是您不要必须使用ng-cordova,因为那里有新的phonegap-push插件。您不需要使用其他任何东西。您基本上可以在不使用ng-cordova的情况下执行以下操作。
My personal advice is you don't have to use ng-cordova since new phonegap-push plugin is out there. You don't need to use any other things. You can basically do the following without using ng-cordova.
请确保检查ionic平台是否已准备好且该平台不是浏览器。
var pushConfig = {
android: {
senderID: "blabla"
},
ios: {
alert: true,
badge: true,
sound: true
}
};
var push = window.PushNotification.init(pushConfig);
push.on('registration', function(data) {
var token = data.registrationId
console.log('OK: register notfy ', token);
});
详细信息在这里: https://github.com/phonegap/phonegap-plugin-push
这篇关于PushNotification未定义(ng-cordova)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!