摘要于http://blog.csdn.net/wxwzy738/article/details/24494709
推送信息必须打包安装手机后才能使用,主要是通过client_id来进行对每个用户进行推送,首先我们需要在数据库的用户表内添加一个client_id 的字段(在用户注册的时候或者在每次登录的时候存入用户的新client_id,保证推送的有效性),为存放我们用户的client_id
1、后台实例
个推的作用:可以为手机端的app使用者推送消息,而不是通过手机上的app对用户发送消息。所以用户是被动的接收信息。当然不只是只有对用户弹出窗口的这种方式,也可以把信息推送给app,让app决定对用户实行怎么样的操作,例如在app的栏目中显示出有新信息的图标,以便提高用户体验。
个推的官网http://www.igetui.com/
该api支持Android和iso的推送。
下载个推的sdk,里面有各种不同文档,其中有android的apk的安装文件,可以进行推送的测试。
-
-
-
-
- public abstract class PushBase {
-
- protected static final String APPID = "b03c5cfef65ed30108f0a3fd82c3f6b4";
- protected static final String APPKEY = "110000";
- protected static final String MASTERSECRET = "a02a76119b20d4e31620d7597a3b4f35";
- protected static final String CLIENTID = "f8b14fc288a21bc3d675190e9a4db0a4";
- protected static final String API = "http://sdk.open.api.igexin.com/apiex.htm"; //OpenService接口地址
-
- protected static String getDate(){
- Date date = new Date();
- return date.toLocaleString();
- }
- }
对多个app进行推送:
- import java.util.ArrayList;
- import java.util.List;
- import com.gexin.rp.sdk.base.IIGtPush;
- import com.gexin.rp.sdk.base.IPushResult;
- import com.gexin.rp.sdk.base.impl.AppMessage;
- import com.gexin.rp.sdk.http.IGtPush;
- import com.gexin.rp.sdk.template.LinkTemplate;
-
- public class PushMessageToAppTest extends PushBase{
-
- public static void main(String[] args) {
-
-
- IIGtPush push = new IGtPush(API, APPKEY, MASTERSECRET);
-
- try {
-
- AppMessage message = new AppMessage();
-
-
-
- LinkTemplate template = new LinkTemplate();
-
- template.setAppId(APPID);
- template.setAppkey(APPKEY);
-
-
- template.setTitle("填写通知标题");
- template.setText("填写通知内容");
- template.setLogo("hello.png");
-
-
-
- template.setUrl("http://baidu.com"); //点击通知后打开的网页地址,你可以设定你希望跳转的网页地址如http://www.igetui.com
-
- message.setData(template);
-
-
-
- List<String> appIdList = new ArrayList<String>();
- appIdList.add(APPID);
-
- List<String> phoneTypeList = new ArrayList<String>();
- phoneTypeList.add("ANDROID");
-
- List<String> provinceList = new ArrayList<String>();
- provinceList.add("浙江");
- provinceList.add("上海");
- provinceList.add("北京");
-
- List<String> tagList = new ArrayList<String>();
- tagList.add("填写tags名称");
-
- message.setAppIdList(appIdList);
- message.setPhoneTypeList(phoneTypeList);
- message.setProvinceList(null);
- message.setTagList(null);
-
- IPushResult ret = push.pushMessageToApp(message);
-
- System.out.println(ret.getResponse().toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
对单个app的多个用户进行推送:
- import java.util.ArrayList;
- import java.util.List;
- import com.gexin.rp.sdk.base.IIGtPush;
- import com.gexin.rp.sdk.base.IPushResult;
- import com.gexin.rp.sdk.base.impl.ListMessage;
- import com.gexin.rp.sdk.base.impl.Target;
- import com.gexin.rp.sdk.http.IGtPush;
- import com.gexin.rp.sdk.template.NotificationTemplate;
-
-
- public class PushMessageToListTest extends PushBase{
-
- public static void main(String[] args) {
-
- System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
-
-
- IIGtPush push = new IGtPush(API, APPKEY, MASTERSECRET);
-
- try {
- ListMessage message = new ListMessage();
-
-
-
- NotificationTemplate template = new NotificationTemplate();
-
- template.setAppId(APPID);
- template.setAppkey(APPKEY);
-
-
- template.setTitle("此处填写通知标题"+getDate());
- template.setText("此处填写通知内容"+getDate());
- template.setLogo("push.png");
- template.setIsRing(false);
-
- template.setIsClearable(true);
-
- template.setTransmissionType(2);
- template.setTransmissionContent("你需要透传的内容"+getDate());
-
- message.setData(template);
-
-
-
-
- List<Target> targets = new ArrayList<Target>();
- Target target1 = new Target();
-
- target1.setAppId(APPID);
- target1.setClientId(CLIENTID);
-
-
-
-
-
- targets.add(target1);
-
-
-
- String contentId = push.getContentId(message);
- IPushResult ret = push.pushMessageToList(contentId, targets);
-
- System.out.println(ret.getResponse().toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
对单个app的单个用户进行推送:
- import com.gexin.rp.sdk.base.IIGtPush;
- import com.gexin.rp.sdk.base.IPushResult;
- import com.gexin.rp.sdk.base.impl.SingleMessage;
- import com.gexin.rp.sdk.base.impl.Target;
- import com.gexin.rp.sdk.http.IGtPush;
- import com.gexin.rp.sdk.template.TransmissionTemplate;
-
- public class PushMessageToSingleTest extends PushBase{
-
-
- public static void main(String[] args) {
-
- IIGtPush push = new IGtPush(API, APPKEY, MASTERSECRET);
-
- try {
-
- SingleMessage message = new SingleMessage();
-
-
-
- TransmissionTemplate template = new TransmissionTemplate();
- template.setAppId(APPID);
- template.setAppkey(APPKEY);
- template.setTransmissionContent("您需要透传的内容:"+getDate());
-
-
- template.setTransmissionType(1);
-
- message.setData(template);
-
-
-
- Target target1 = new Target();
- target1.setAppId(APPID);
- target1.setClientId(CLIENTID);
-
-
- IPushResult ret = push.pushMessageToSingle(message, target1);
-
- System.out.println(ret.getResponse().toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- import com.gexin.rp.sdk.base.IIGtPush;
- import com.gexin.rp.sdk.base.IPushResult;
- import com.gexin.rp.sdk.base.impl.SingleMessage;
- import com.gexin.rp.sdk.base.impl.Target;
- import com.gexin.rp.sdk.http.IGtPush;
- import com.gexin.rp.sdk.template.NotyPopLoadTemplate;
-
- public class PushMessageToSingleTest1 extends PushBase{
-
- public static void main(String[] args) {
-
- IIGtPush push = new IGtPush(API, APPKEY, MASTERSECRET);
-
- try {
-
-
- SingleMessage message = new SingleMessage();
-
-
-
- NotyPopLoadTemplate template = new NotyPopLoadTemplate();
-
- template.setActived(true);
-
- template.setAndroidMark("android_mark");
- template.setAppId(APPID);
- template.setAppkey(APPKEY);
-
- template.setAutoInstall(true);
-
- template.setBelled(true);
-
- template.setCleared(true);
-
- template.setIphoneMark("iphone_mark");
-
- template.setLoadIcon("");
-
- template.setLoadTitle("LoadTitle");
-
- template.setLoadUrl("http://dizhensubao.igexin.com/dl/com.ceic.apk");
-
- template.setNotyContent("NotyContent");
-
- template.setNotyIcon("");
-
- template.setNotyTitle("NotyTitle");
-
- template.setPopButton1("下载");
-
- template.setPopButton2("取消");
-
- template.setPopContent("popContent");
-
- template.setPopImage("");
-
- template.setPopTitle("PopTitle");
-
- template.setSymbianMark("symbian_mark");
-
- template.setVibrationed(true);
- message.setData(template);
- message.setOffline(true);
- message.setOfflineExpireTime(72 * 3600 * 1000);
-
- message.setPriority(1);
-
- Target target1 = new Target();
- target1.setAppId(APPID);
- target1.setClientId(CLIENTID);
-
- IPushResult ret = push.pushMessageToSingle(message, target1);
- System.out.println(ret.getResponse().toString());
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- }
工程下载地址:http://download.csdn.net/detail/wxwzy738/7263297
2、前台实例
摘要于http://ask.dcloud.net.cn/article/34?item_id=167
HBuilder 基座集成了推送功能,现已集成个推推送平台和Mkey 推送服务。个推是开放的推送平台。Mkey 推送服务是面向企业用户提供的一套私有推送解决方案。以后还会陆续添加对其他推送平台的支持。
使用须知
push是一个可用但不可依赖的功能。
1. 手机用户有自主关闭推送的权利,如果被关闭自然无法收到push。
2. Android的push更不可依赖,Android rom厂商为了省电会禁止push进程开机自启、三方清理软件会杀掉push进程。
不止是个推,所有非大厂的app,没有进入rom厂商和三方清理软件白名单的app,不管用哪个推送方案都可能会被杀。
本质上推送是一个有利于开发商但却很容易造成用户骚扰和费电的功能。
所以大多数主流app里的push的实际用处都是拉激活的非实时活动推送。
如果app主体在运行期且需要实时推送,应该使用js与服务器长链接或轮询比如socket.io方案。
开发指导
用户可以使用JS代码监听推送的消息,现在可以监听“receive”事件和“click”事件。如应用在前台时收到推送消息,在IOS平台会触发“receive”事件回调,在Android平台如发送的是透传消息并且消息不符合格式时会触发“receive”事件。
当用户点击消息中心里的消息时会启动应用,并且在监听push事件的页面触发“click”事件。
Push 运行原理
用户可以在事件触发的回调中获取“PushMessage”对象,如发送的是普通消息可以在对象中获取推送消息的“title”属性和“content”属性的值,消息为透传消息,则可以获取“payload”属性的值。
如服务发送的透传消息格式正确可以获得“PushMessage”对象的payload属性,如发送的消息符合指定格式则“payload”属性为json对象,如消息不符合格式则“payload”属性为string类型,内容和“content”节点内容相同。
透传消息的格式为{title:"通知标题",content:"通知内容",payload:"通知去干嘛这里可以自定义"}
使用个推平台推送消息,在ios平台和安卓平台的运行机制并不完全相同,下面对安卓平台和IOS平台进行分别描述
IOS平台
IOS平台在处理消息时会因为程序是否在线处理逻辑会有不同
应用在线:
应用在线时服务器推送的消息不会进入消息中心。如果页面中监听了“receive”事件则会触发事件的回调。如果消息是通过APS通道发送到终端,用户可以在“receive”事件回调的“PushMesage”对象中获取“aps”属性的值。
应用不在线:
如果当前应用不在线,服务器推送的消息会进入系统消息中心。当用户点击消息,则会启动HTML5+应用,并且在监听了“click”事件的页面中触发事件的回调。
本地消息:
当开发者创建本地消息时,本地消息会进入消息中心。如果开发者监听了“receive”事件,则会在监听页面触发receive事件回调。如需要区分收到的消息是否是本地消息,用户需要在payload节点添加标识。
Android平台:
对安卓平台发送的普通push消息和符合格式的透传消息都会进入系统的消息中心。透传消息如不符合格式则会触发监听页面的“receive”事件不会显示在消息中心。
安卓平台发送的普通消息都会进入消息中心,用户可以点击触发消息。
发送普通消息:
服务器可以向安卓平台发送三种类型的普通消息“通知”“下载”“网址”,发送“通知”消息会启动HTML5+应用,如果是“下载”消息则会调用系统的下载管理器下载指定文件。点击“网址”消息则会调用系统浏览器打开指定的网址。
发送透传消息:
当设备收到服务器发送的透传消息时,不会触发页面监听的receive事件。当用户点击消息中心消息时会触发监听页面的“click”事件。如果用户点击多个消息时,会多次触发“click”事件。
不符指定合格式的透传消息不会进入系统的消息中心,会在监听“receive”事件的页面触发事件回调。
应用开发的JS方法
1.配置推送平台信息。
监听收到消息事件和消息点击事件
2.添加推送事件监听。用户需要在plusready事件之后调用plus.push.addEventListener();监听推送消息或者监听推送消息点击事件。用户可以到消息回调中获取服务器发来的透传消息体。
receive事件监听:
如果客户端正在前台操作,并且已经监听了receive事件,则会触发这个事件。在IOS平台,应用在前台操作时消息不会进入消息中心。
Click事件监听:
应用在运行,用户点击了消息中心的消息,会触发click事件。
document.addEventListener( "plusready", function(){
message = document.getElementById("message");
// 监听点击消息事件
plus.push.addEventListener( "click", function( msg ) {
// 判断是从本地创建还是离线推送的消息
switch( msg.payload ) {
case "LocalMSG":
outSet( "点击本地创建消息启动:" );
break;
default:
outSet( "点击离线推送消息启动:");
break;
}
// 提示点击的内容
plus.ui.alert( msg.content );
// 处理其它数据
logoutPushMsg( msg );
}, false );
// 监听在线消息事件
plus.push.addEventListener( "receive", function( msg ) {
if ( msg.aps ) { // Apple APNS message
outSet( "接收到在线APNS消息:" );
} else {
outSet( "接收到在线透传消息:" );
}
logoutPushMsg( msg );
}, false );
}, false );
清空消息
清空消息可以调用接口plus.push.clear();,用户可以调用这个方法清空消息中心里推送给当前应用的消息。
function clearAllPush(){
plus.push.clear();
outSet( "清空所有推送消息成功!" );
创建本地消息
开发者如需创建本地消息可以调用“createMessage”接口,通过设置“MessageOption”用户可以指定消息的标题,显示推送的时间或者使用延迟时间。在IOS平台,本地消息也会触发监听的“receive”事件,用户需要通过标识来区分是否是本地创建的消息。
function createLocalPushMsg(){
var options = {cover:false};
var str = dateToStr(new Date());
str += ": 欢迎使用Html5 Plus创建本地消息!";
plus.push.createMessage( str, "LocalMSG", options );
outSet( "创建本地消息成功!" );
outLine( "请到系统消息中心查看!" );
if(plus.os.name=="iOS"){
outLine('*如果无法创建消息,请到"设置"->"通知"中配置应用在通知中心显示!');
}
获取所有推送消息
在安卓平台用户可以获取系统消息中心内本应用的全部消息内容,IOS平台不支持此方法。
function listAllPush(){
var msgs=null;
switch ( plus.os.name ) {
case "Android":
msgs = plus.push.getAllMessage();
break;
default:
break;
}
if ( !msgs ) {
outSet( "此平台不支持枚举推送消息列表!" );
return;
}
outSet( "枚举消息列表("+msgs.length+"):" );
for ( var i in msgs ) {
var msg = msgs[i];
outLine( i+": "+msg.title+" - "+msg.content );
}
}
获取客户端推送标识
开发者可以调用“getClientInfo”方法获取推送标识信息,如IOS平台的token,应用的APPID和设置的Appkey等信息。
function getPushInfo(){
var info = plus.push.getClientInfo();
outSet( "获取客户端推送标识信息:" );
outLine( "token: "+info.token );
outLine( "clientid: "+info.clientid );
outLine( "appid: "+info.appid );
outLine( "appkey: "+info.appkey );
}
使用推送插件的配置方法
在HBuilder中配置推送插件的步骤如下:
1. 双击应用的manifest.json文件,选中“使用第三方插件”
Ctrl+S保存。
2.点击manifest.json文件的“代码视图”,在permissions节点下添加push节点:
"Push": {
"description": "管理推送消息插件"
},
3.在plus -> distribute -> plugins 节点下添加push节点
"push": {
"mkeypush":{
"appid":"必选,mkey推送应用标示",
"appkey":"必选,mkey推送应用的appkey",
"server" :"必选,mkey推送服务器地址"
},
"igexin": {
"appid": "必选,个推应用标识",
"appkey": "必选,个推应用appkey",
"appsecret": "必选,个推应用安全标识"
}
},
4.添加不同推送平台的方法
a)个推推送平台
1.在plus->distribute->plugins->push节点下添加igexin节点。
appid属性,填写在个推平台创建的应用的appid
appkey属性,填写在个推平台创建的应用的appkey
appsecret属性,填写在个推平台创建的应用的appseret
b)MKey 推送服务
MKey推送服务为企业用户提供的一套私有的推送解决方案。目前暂不对公众用户开放。
1.在plus->distribute->plugins->push节点下添加mkeypush节点。
appid属性,填写MKey推送应用标识
appkey属性,填写MKey推送应用appkey
server属性,填写MKey推送服务器地址