本文介绍了Android的推送通知的PhoneGap HTML5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我混淆有关使用PhoneGap的推送通知,我没有建立本土code(Android的一室公寓)的Android应用程序,它做工精细,但没有推送通知。
I confuse about push notification using PhoneGap, I build android apps without native code (Android Studio) and it work fine but without push notification.
我可以建立推送通知Android应用没有Android工作室?
Can I build Android apps with push notification without Android Studio?
推荐答案
当然可以。使用pushplugin科尔多瓦插件。
Yes you can. Use pushplugin cordova plugin.
样code:
PushCustom.js
PushCustom.js
// JavaScript Document
var pushNotification;
function onDeviceReady() {
document.addEventListener("backbutton", function(e) {}, false);
try {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android' || device.platform ==
'amazon-fireos') {
pushNotification.register(successHandler, errorHandler, {
"senderID": "xxxxxxxxxxxxx",
"ecb": "onNotification"
}); // required!
} else {
pushNotification.register(tokenHandler, errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
}); // required!
}
} catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
alert(txt);
}
}
// handle APNS notifications for iOS
function onNotificationAPN(e) {
if (e.alert) {
// showing an alert also requires the org.apache.cordova.dialogs plugin
navigator.notification.alert(e.alert);
}
if (e.sound) {
// playing a sound also requires the org.apache.cordova.media plugin
var snd = new Media(e.sound);
snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
// handle GCM notifications for Android
function onNotification(e) {
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
alert("regID = " + e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground) {
// on Android soundname is outside the payload.
// On Amazon FireOS all custom attributes are contained within payload
var soundfile = e.soundname || e.payload.sound;
// if the notification contains a soundname, play it.
// playing a sound also requires the org.apache.cordova.media plugin
var my_media = new Media("/android_asset/www/" + soundfile);
my_media.play();
}
break;
case 'error':
break;
default:
break;
}
}
function tokenHandler(result) {
alert('device token = ' + result);
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}
function successHandler(result) {}
function errorHandler(error) {}
document.addEventListener('deviceready', onDeviceReady, true);
index.html的
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<script type="text/javascript" src="js/PushNotification.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
</ion-content>
</ion-pane>
</body>
</html>
参考:http://blog.revivalx.com/2014/08/29/implement-push-notifications-for-android-and-ios-phonegap-part-1/
这篇关于Android的推送通知的PhoneGap HTML5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!