我是 PhoneGap/Cordova 的新手,我希望向我的应用程序添加一些通知。
推送通知 - 因此当应用程序上发布新文章时,它会提醒用户。
本地通知 - 在设定的时间间隔(日期和时间),我可以提示用户我的应用程序上的最新文章。
我进行了大量搜索,但找不到可以直接导入项目然后进行修改的工作示例。
我已经尝试过以下插件但无法使其工作https://github.com/katzer/cordova-plugin-local-notifications/ https://github.com/katzer/cordova-plugin-local-notifications/
在项目中启用推送通知的步骤。
在中创建一个项目https://console.developers.google.com/ https://console.developers.google.com/与您的项目名称。
推送插件安装请参考以下链接https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md
-
Push.js 文件中的代码
参考https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md
//代码:推送通知初始化和注册。
if (!$window.PushNotification) {
return;
}
var push = $window.PushNotification.init({
android: {
senderID: "Include your app sender ID XXXXXXX”
},
ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
push.on('registration', function(data) {
// this gives the unique deviceId, Or you can maintain array of deviceIds to register multiple devices.
// if have specified single deviceId for example
// save this deviceId in database for further operations, i.e. push messages to those ids.
console.log(data.registrationId);
});
push.on('notification', function(data) {
console.log(data.message);
});
push.on('error', function(e) {
console.log(e.message);
});
push.off('notification', function(e) {
console.log('off notify');
});
-
有多种发送推送通知的方法。这里我借助 gcm-server 来通知。
您将需要安装node-gcm。
创建一个新的 server.js 文件。
var gcm = require('node-gcm');
var message = new gcm.Message();
//API Server Key
var sender = new gcm.Sender('GIVE_YOUR_SERVER_API_KEY');
var registrationIds = [];
// Value the payload data to send...
message.addData('message',"\u270C Peace, Love \u2764 and PhoneGap \u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3'); // Shows up in the notification in the status bar
message.addData('soundname','beep.wav'); //Sound to play
message.timeToLive = 3000;
// 至少需要一个 reg id
// 这里使用您在设备注册期间获得的注册 ID。
RegistrationIds.push('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
sender.send(消息,registrationIds,4,函数(结果){
控制台.log(结果);
});
Refer http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/ http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/为了清楚地了解通过 gcm-server 发送通知。这将在设备上显示通知。
您还可以使用 firebase 而不是 gcm。
启用本地通知的步骤:
使用以下命令将插件添加到您的项目中cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications
调用本地通知,如链接所示https://github.com/katzer/cordova-plugin-local-notifications/ https://github.com/katzer/cordova-plugin-local-notifications/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)