第十五章:集成JPUSH
如果不想自己搭建push server,则可以借助于第三方的api来实现push的功能,本文主要介绍ionic如何集成jpush。
具体步骤如下:
- 创建ionic应用:ionic_jpush。
- 申请jpush账号:创建应用,上传开发者证书,并且记录jpush_api_appkey。
- 安装jpush plugin
first way: cordova plugin add jpush-phonegap-plugin --variable API_KEY=your_jpush_appkey
second way:cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable API_KEY=your_jpush_appkey
third way:
git clone https://github.com/jpush/jpush-phonegap-plugin.git
cordova plugin add $JPUSH_PLUGIN_DIR --variable API_KEY=your_jpush_appkey
- 修改配置
找到路径:ionic_jpush\plugins\cn.jpush.phonegap.JPushPlugin\src\ios\PushConfig.plist,修改jpush_api_key,并且填写如下部分:
<key>APS_FOR_PRODUCTION</key>
<string>0</string>
- 在services中添加push工厂:
.factory('Push', function() {
var push;
return {
setBadge: function(badge) {
if (push) {
console.log('jpush: set badge', badge);
plugins.jPushPlugin.setBadge(badge);
}
},
setAlias: function(alias) {
if (push) {
console.log('jpush: set alias', alias);
plugins.jPushPlugin.setAlias(alias);
}
},
check: function() {
if (window.jpush && push) {
plugins.jPushPlugin.receiveNotificationIniOSCallback(window.jpush);
window.jpush = null;
}
},
init: function(notificationCallback) {
console.log('jpush: start init-----------------------');
push = window.plugins && window.plugins.jPushPlugin;
if (push) {
console.log('jpush: init');
plugins.jPushPlugin.init();
plugins.jPushPlugin.setDebugMode(true);
plugins.jPushPlugin.openNotificationInAndroidCallback = notificationCallback;
plugins.jPushPlugin.receiveNotificationIniOSCallback = notificationCallback;
}
}
};
})
- 在app.js的run中初始化jpush并添加监听事件:其中请修改别名“12345678”成为你想要的name。
.run(function($ionicPlatform,Push) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
//jpush callback method
var notificationCallback = function(data) {
console.log('received data :' + data);
var notification = angular.fromJson(data);
//app 是否处于正在运行状态
var isActive = notification.notification;
// here add your code
//ios
if (ionic.Platform.isIOS()) {
window.alert(notification);
} else {
//非 ios(android)
}
};
//初始化
Push.init(notificationCallback);
//设置别名
Push.setAlias("12345678");
console.log('start to define addEventListener');
var onOpenNotification= function(event) {
console.log("JPushPlugin:onOpenNotification is triggered");
};
var onBackgroundNotification = function(event){
console.log("JPushPlugin:onBackgroundNotification is triggered");
};
var onReceiveNotification = function(event){
console.log("JPushPlugin:onReceiveNotification is triggered");
};
console.log('end to define addEventListener');
console.log('start to add addEventListener');
document.addEventListener("jpush.openNotification", onOpenNotification, false);
document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
document.addEventListener("jpush.backgroundNotification", onBackgroundNotification, false);
console.log('end to add addEventListener');
});
})
- 编译ios项目:
ionic build iOS
- 使用xcode打开工程:进行证书配置,确保使用的开发者证书有apns的权限。
- 在jpush官网中进行push模拟。
- 编译Android项目:
ionic build android
- 使用Android Studio打开工程:确保Jpush的包名和Project的包名是一样的。
- 在jpush官网进行push模拟。
常见问题:
- ios编译失败等一系列问题:最好请有经验的ios开发人员帮忙排查。
- 无法连接到jpush服务器:请检查api_key是否正确。
- 无法接收push信息:请检查jpush官网的push证书配置是否正确。
参考资料:
第十五章:集成JPUSH的更多相关文章
- Gradle 1.12 翻译——第十五章. 任务详述
有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...
- Gradle 1.12用户指南翻译——第二十五章. Scala 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- Gradle 1.12用户指南翻译——第三十五章. Sonar 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- Gradle 1.12用户指南翻译——第六十五章. Maven 发布(新)
其他章节的翻译请参见:http://blog.csdn.net/column/details/gradle-translation.html翻译项目请关注Github上的地址:https://gith ...
- 15第十五章UDF用户自定义函数(转载)
15第十五章UDF用户自定义函数 待补上 原文链接 本文由豆约翰博客备份专家远程一键发布
- 《Linux命令行与shell脚本编程大全》 第十五章 学习笔记
第十五章:控制脚本 处理信号 重温Linux信号 信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能 ...
- CSS3秘笈复习:十三章&十四章&十五章&十六章&十七章
第十三章 1.在使用浮动时,源代码的顺序非常重要.浮动元素的HTML必须处在要包围它的元素的HTML之前. 2.清楚浮动: (1).在外围div的底部添加一个清除元素:clear属性可以防止元素包围浮 ...
- Gradle 1.12用户指南翻译——第四十五章. 应用程序插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- “全栈2019”Java多线程第三十五章:如何获取线程被等待的时间?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十五章:生产者与消费者线程详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
随机推荐
- 异步 Action 的定义
ASP.NET MVC 3中的异步 Action 通过两个匹配的方法XxxAsync/XxxCompleted 来定义,这样的异步 Action 只能定义在继承自 AsyncConoller 的类型中 ...
- js任意位数求和
<script> //任意位数求和 function sum(){ if(arguments.length==1) { console.log(arguments[0]) return; ...
- 将某个类封装成XML形式返回
<?xml version="1.0" encoding="GBK" standalone="no"?><package& ...
- audio音频
html5的audio功能上已经非常强大,回放,跳转,缓冲等以前只能用flash才能实现的功能,html5的audio都能轻松搞定 最近的一个项目使用到了这个功能,把我使用的情况写下来,供大家参考, ...
- linux下文件权限的介绍
linux操作系统下,使用ll查看该目录下所有文件及其文件权限,以下是对文件权限的介绍 d代表的是目录(或称之为文件夹) 红框内的这3个是代表3个组的权限每组都是3个 第一组rwx代表是本用户的权 ...
- CPU 的工作原理
内部架构 CPU 的根本任务就是执行指令,对计算机来说最终都是一串由 0 和 1 组成的序列.CPU 从逻辑上可以划分成 3 个模块,分别是控制单元.运算单元和存储单元 .其内部架构如下: [1]控制 ...
- centos7用docker安装mysql5.7.24后配置主从
1)使用docker安装完成mysql5.7.24,我规划的是3台: 192.168.0.170(Master) 192.168.0.169(Slave) 192.168.0.168(Slave) 2 ...
- hadoop计算二度人脉关系推荐好友
https://www.jianshu.com/p/8707cd015ba1 问题描述: 以下是qq好友关系,进行好友推荐,比如:老王和二狗是好友 , 二狗和春子以及花朵是好友,那么老王和花朵 或者老 ...
- [jvm] JVM体系结构
一.jvm 运行时数据区域 程序计数器 java虚拟机栈 本地方法栈 java堆 方法区 运行时常量池 直接内存(非JVM区域) 二.深入探讨HotSpot虚拟机在Java堆中对象分配.布局和访问的全 ...
- Qt 学习之路 2(43):QStringListModel
Qt 学习之路 2(43):QStringListModel 豆子 2013年2月13日 Qt 学习之路 2 38条评论 上一章我们已经了解到有关 list.table 和 tree 三个最常用的视图 ...