|
楼主 |
发表于 2016-9-26 14:38:11
|
显示全部楼层
我这样操作对吗?不执行啊!辛苦您了,帮下忙,js我是懂点的,望您指教,同时也能帮助其他人,我看帖子这两天浏览量还不少!
- <script type="text/javascript">
- $(function() {
- var JPushInstance = function() {
- if (window.plugins && window.plugins.jPushPlugin) {
- document.addEventListener("deviceready", this.onDeviceReady.bind(this), false);
- document.addEventListener("jpush.openNotification", this.onOpenNotification.bind(this), false);
- document.addEventListener("jpush.receiveNotification", this.onReceiveNotification.bind(this), false);
- document.addEventListener("jpush.receiveMessage", this.onReceiveMessage.bind(this), false);
- }
- };
-
- //hcr 解决第一次获取失败的问题
- JPushInstance.prototype.getRegistrationID = function(){
- var dtd = $.Deferred();
- if (this.registrationID){
- dtd.resolve(this.registrationID);
- }else{
- if (window.plugins && window.plugins.jPushPlugin){
- window.plugins.jPushPlugin.getRegistrationID(function(registrationID) {
- self.registrationID = registrationID;
- if (self.registrationID){
- dtd.resolve(self.registrationID);
- }else{
- dtd.reject();
- }
- });
- }else{
- dtd.reject();
- }
- }
- return dtd.promise();
- };
-
- JPushInstance.prototype.onDeviceReady = function() {
- var self = this;
- window.plugins.jPushPlugin.init();
- window.plugins.jPushPlugin.getRegistrationID(function(registrationID) {
- self.registrationID = registrationID;
- });
- if (device.platform == "Android") {
- window.plugins.jPushPlugin.setDebugMode(false);
- window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
- } else {
- window.plugins.jPushPlugin.setDebugMode(false);
- window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
- }
- };
- JPushInstance.prototype.onOpenNotification = function(event) {
- var alertContent;
- if (device.platform == "Android") {
- alertContent = window.plugins.jPushPlugin.openNotification.alert;
- } else {
- alertContent = event.aps.alert;
- }
- window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
- alert(alertContent);
- };
- JPushInstance.prototype.onReceiveNotification = function(event) {
- var alertContent;
- if(device.platform == "Android"){
- alertContent = window.plugins.jPushPlugin.receiveNotification.alert;
- }else{
- alertContent = event.aps.alert;
- }
- window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
- alert(alertContent);
- };
- JPushInstance.prototype.onReceiveMessage = function() {
- var message;
- if(device.platform == "Android"){
- message = window.plugins.jPushPlugin.receiveMessage.message;
- }else{
- message = event.content;
- }
- window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
- alert(message);
- };
- return new JPushInstance();
- });
- </script>
复制代码 |
|