Android 如何在Cordova平台中实现推送通知?
我正在尝试在我的应用程序中实现推送通知的概念。 成功安装插件后,Android 如何在Cordova平台中实现推送通知?,android,ios,cordova,notifications,Android,Ios,Cordova,Notifications,我正在尝试在我的应用程序中实现推送通知的概念。 成功安装插件后,push.on(注册)方法未调用 我的项目结构是projectname/platforms/android/assets/www 在www文件夹中包含所有html、js、css文件 notification.js文件,我已经调用了homepage.html 我在notification.js中编写的代码是: document.addEventListener('deviceready', pushNotification, fals
push.on(注册)
方法未调用
我的项目结构是projectname/platforms/android/assets/www
在www
文件夹中包含所有html、js、css
文件
notification.js
文件,我已经调用了homepage.html
我在notification.js中编写的代码是:
document.addEventListener('deviceready', pushNotification, false);
function PushNotification(){
var push = PushNotification.init({ "android": {"senderID": "GCMProjectId(123456789)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
push.on('registration', function(data) {
alert("registration id is:"+data.registrationId);
var id = localStorage.getItem("userId");
var notifyInput = {
"token":data.registrationId,
"type":"android",
"uid":id
}
});
push.on('notification', function(data) {
alert(data.message);
});
push.on('error', function(e) {
// e.message
alert("error function calling on push notifications");
});
}
这里,
push.on(注册)和push.on(通知)
方法没有调用请告诉我们获取特定设备通知的可能方式成功安装推送通知插件后,我已在Android中收到通知
过程:
通过使用下面的链接,我安装了推送通知插件
cordova插件添加https://github.com/phonegap/phonegap-plugin-push --变量发送方\u ID=“XXXXXXXXXXXXX”
安装要求:
-Android版本>6.0.0-iOS版本>4.3.0更好 对于iOS版本,需要pod。因此我们需要安装pod
sudo gem install cocoapods
GCM注册:
成功安装后,将创建pod文件。完成后,打开project.xcworkspace
文件。然后ios应用程序将正常运行
如果您在应用程序中间调用通知,请编写addEventListener
方法
document.addEventListener('deviceready', pushNotification, false);
function PushNotification(){
var push = PushNotification.init({ "android": {"senderID": "xxxxxx(refers project number in GCM)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
push.on('registration', function(data) {
alert("registration id is:"+data.registrationId);
// registration id need to pass your notification server
});
push.on('notification', function(data) {
alert(data.message);
// you receive the notification
});
push.on('error', function(e) {
// e.message
alert("error function calling on push notifications");
});
}