近段时间的项目,所用到的第三方推送服务-智游推送
最近要做推送的功能,于是去了解了下社区推荐的智游推送zypush
开发指南非常详细,按照这个配置,就可以出来了。
1.申请appkey
到www.zypush.com申请appkey
2.导入文件
将zhiyou_push.**.jar,liblptcpjin.so库以及push_notification.xml,push_download.xml文件导入到工程内部
3.配置AndroidManifest.xml文件
<注意:在配置过程中有些地方需要用到包名以及appkey>
代码块
<!--pushservice(必要)-->
<service
android:name=”com.joboevan.push.service.PushService”>
</service>
<!--应用包名.PushReceiver定义推送消息接收器,PushReceiver在SDK中有这个类-->
<!--<receiverandroid:name="YOUR_PACKAGENAME.PushReceiver">-->
<!--com.zypush.activity这是我自己的包名-->
<receiverandroid:name=”com.zypush.activity.PushRecevier”>
<intent-filter>
<!--<actionandroid:name=”com.zypush.android.intent.message.申请的appkey”/>-->
<!--用户接收消息的action-->
<actionandroid:name=”com.zypush.android.intent.message.f262516bedf4c0f567b5659840920f94”/>
<!--用户打开通知栏的action-->
<actionandroid:name=”com.zypush.android.intent.NOTIFICATION_OPENED.f262516bedf4c0f567b5659840920f94”/>
<!--需要上传经纬度的action-->
<actionandroid:name=”com.zypush.android.intent.LBS_PUSH.f262516bedf4c0f567b5659840920f94”/>
</intent-filter>
</receiver>
<!---心跳广播接收器注册(必要)-->
<receiver
android:name="com.joboevan.push.receiver.CheckConnectRecevier"
android:enabled="true"/>
<!--网络改变广播监听(必要)-->
<receiverandroid:name="com.joboevan.push.receiver.NetworkReceiver">
<intent-filter>
<actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<!--android:authorities="packagename"此处填写包名(必要)-->
<provider
android:authorities="com.zypush.activity"
android:name="com.joboevan.push.providers.downloads.DownloadProvider"/>
<!--富媒体下载服务(必要)-->
<serviceandroid:name="com.joboevan.push.providers.downloads.DownloadService"/>
<!--富媒体下载接收器(必要)-->
<receiver
android:exported="false"
android:name="com.joboevan.push.providers.downloads.DownloadReceiver">
<intent-filter>
<actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<!--配置的第三方参数属性测试账号:zypush密码:123456-->
<!--http://www.zypush.com-->
<meta-data
android:name="ZYPUSH_APPKEY"
android:value="f262516bedf4c0f567b5659840920f94"/>
推送服务运行所需要的权限
代码块
<!--自定义权限-->
<permissionandroid:name="com.zypush.permission.ACCESS_DOWNLOAD_MANAGER"/>
<!--网络权限-->
<uses-permissionandroid:name="android.permission.INTERNET"/>
<!--网络状态权限-->
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--往sdcard中写入数据的权限-->
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--获取手机设备号权限-->
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
<!--手机震动权限-->
<uses-permissionandroid:name="android.permission.VIBRATE"/>
<!--引用富媒体自定义权限-->
<uses-permissionandroid:name="com.zypush.permission.ACCESS_DOWNLOAD_MANAGER"/>
<!--唤醒权限-->
<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
<!--获取当前任务权限-->
<uses-permissionandroid:name="android.permission.GET_TASKS"/>
<!--悬浮窗体权限-->
<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<!--保留权限,后续添加功能需要用到-->
<!--network网络开关权限(可选权限)-->
<uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"/>
<!--获取基站需要的权限(可选权限)-->
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
4.智游推送API接口说明
4.1启动推送服务
函数原型
1
2
代码块
PushManager.getInstance().connect(Contextcontext,booleanisReconnect)
参数
context:Context对象
isReconnect:设置是否自动重连,true:表示断线以后自动重连,false标示断线以后不会自动重连
返回值
true:开启推送的条件都满足
false:条件不满足开启推送
示例
1
2
代码块
booleanresult=PushManager.getInstance().connect(getApplicationContext(),true);
4.2设置别名
需要注意的是:这个接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
一个用户只可以设置一个别名,相当于一个人的名字,为了确保其唯一性,一般是用用户名设置别名,推送可以根据别名进行推送到个人客户端。设置别名超时时间为15秒,别名的最大长度不得超过40个字节,可以在PushReceiver中接收设置别名返回给客户端的返回值
1
2
代码块
PushManager.getInstance().bindAlias(Contextcontext,Stringalias)
参数
context:Context对象
alias:设置的别名
返回值
无
示例
1
2
代码块
PushManager.getInstance().bindAlias(this,“别名”);
4.3设置标签
需要注意的是:这个接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖之前的设置。
标签的功能可以将一个整体划分成不同的群体,比如说将所有的用户划分成“男”用户和“女”用户,那么就可以给用户设置标签“男”或“女”。一个用户可以设置多个标签,多个标签的关系是或的关系。设置标签超时时间为15秒,单个标签的最大长度不得超过40个字节,并且不能为空,一个客户端最多可以设置100个标签。
函数原型
1
2
代码块
PushManager.getInstance().setTags(Contextcontext,List<String>tags)
参数
context:Context对象
tags:设置标签的List数组
返回值
无
示例
1
2
代码块
PushManager.getInstance().setTags(this,tags);
4.4设置通知栏图标
设置顶层通知栏图标
函数原型
1
2
代码块
PushManager.getInstance().setNotificationIcon(Contextcontext,intvalue);
参数
context:Context对象
value:通知栏图标的资源id
返回值
无
示例
1
2
代码块
PushManager.getInstance().setNotificationIcon(this,R.drawable.icon);
4.5设置是否开启智能提醒
是否开启智能提醒。
函数原型
1
2
代码块
PushManager.getInstance().isOpenRemind(Contextcontext,booleanstate,Stringcontent,intdays,intstartTime,intendTime);
参数
context:Context对象
state:设置是否开启智能提醒,true:表示开启智能提醒,false表示暂停智能提醒
content:设置提醒内容
days:相隔几天之后没有打开应用,进行提醒
startTime:在startTime之后进行提醒0-23
endTime:在endTime之前进行提醒0-23
返回值
true:开启智能提醒成功
false:开启智能提醒失败
示例
1
2
代码块
booleanresult=PushManager.getInstance().isOpenRemind(this,true,"这是一个智能提醒",3,0,23);
想要看更多的API说明,可以到www.zypush.com
5.接收消息PushReceiver
代码块
packagecom.zypush.activity;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.util.Log;
importcom.joboevan.push.bean.SendBroastBean;
importcom.joboevan.push.tool.Consts;
importcom.joboevan.push.tool.LBS;
importcom.joboevan.push.tool.Tool;
/**
*推送消息接收器
*
***/
publicclassPushReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
//TODOAuto-generatedmethodstub
Stringaction=intent.getAction();
if(Consts.getACTION_RECEIVER_KEY(context).equals(action)){
Bundlebundle=intent.getExtras();
Stringkey=bundle.getString("key");
if(Consts.MESSAGE_KEY_CONNECT.equals(key)){
//连接结果
intvalue=bundle.getInt("value");
switch(value){
caseConsts.RESULT_Y:
Log.d("Log","连接成功");
break;
caseConsts.RESULT_N:
Log.d("Log","连接失败");
break;
}
}elseif(Consts.MESSAGE_KEY_LOGIN.equals(key)){
//登陆结果
intvalue=bundle.getInt("value");
switch(value){
caseConsts.RESULT_Y:
Log.d("Log","登陆成功");
break;
caseConsts.RESULT_N:
Log.d("Log","登陆失败");
break;
}
}elseif(Consts.MESSAGE_KEY_SETALIAS.equals(key)){
intvalue=bundle.getInt("value");
switch(value){
caseConsts.RESULT_Y:
Log.d("Log","别名设置成功");
break;
caseConsts.RESULT_N:
Log.d("Log","别名设置失败");
break;
caseConsts.RESULT_I:
Log.d("Log","正在设置别名");
break;
default:
break;
}
}elseif(Consts.MESSAGE_KEY_CLEANALIAS.equals(key)){
intvalue=bundle.getInt("value");
switch(value){
caseConsts.RESULT_Y:
Log.d("Log","清除别名成功");
break;
caseConsts.RESULT_N:
Log.d("Log","清除别名失败");
break;
caseConsts.RESULT_I:
Log.d("Log","正在清除别名");
break;
default:
break;
}
}elseif(Consts.MESSAGE_KEY_SETTAGS.equals(key)){
intvalue=bundle.getInt("value");
switch(value){
caseConsts.RESULT_Y:
Log.d("Log","标签设置成功");
break;
caseConsts.RESULT_N:
Log.d("Log","标签设置失败");
break;
caseConsts.RESULT_I:
Log.d("Log","正在设置标签");
break;
default:
break;
}
}elseif(Consts.MESSAGE_KEY_CLEANTAGS.equals(key)){
intvalue=bundle.getInt("value");
switch(value){
caseConsts.RESULT_Y:
Log.d("Log","标签清除成功");
break;
caseConsts.RESULT_N:
Log.d("Log","标签清除失败");
break;
caseConsts.RESULT_I:
Log.d("Log","正在清除标签");
break;
default:
break;
}
}elseif(Consts.MESSAGE_KEY_CUSTOM.equals(key)){
//接收自定义推送信息
Stringvalue=bundle.getString("value");
Stringflag=bundle.getString(Consts.MESSAGE_BACK_FLAG);
Log.w("Log","推送自定义消息:"+value);
Log.w("Log","推送自定义消息id:"+flag);
//这个方法是推送内部封装生成通知的方法
CustomNotificationnotification=newCustomNotification();
notification.createNotification(context);
}elseif(Consts.MESSAGE_KEY_NOTIFICATION.equals(key)){//如需在工程中展示通知标题和内容,请设置是否展示通知内容
Stringtitle=bundle.getString(Consts.NOTIFICATION_TITLE);
Stringcontent=bundle.getString(Consts.NOTIFICATION_CONTENT);
Stringext=bundle.getString(Consts.NOTIFICATION_EXT);
Stringflag=bundle.getString(Consts.MESSAGE_BACK_FLAG);
Log.d("Log","通知标题:"+title);
Log.d("Log","通知内容:"+content);
Log.d("Log","通知附加字段:"+ext);
Log.d("Log","通知消息id:"+flag);
}elseif(Consts.MESSAGE_KEY_PUSHSTATECHANGED.equals(key)){//客户端与服务端的状态
intvalue=bundle.getInt("value");
switch(value){
caseConsts.PUSH_CONNECTSTATE_CONNECTING://通道连接
Log.d("Log","推送服务已经连接");
break;
caseConsts.PUSH_CONNECTSTATE_DISCONNECT://通道断开
Log.e("Log","推送服务已经断开");
break;
}
}elseif(Consts.ACTION_RECEIVER_VERSION.equals(key)){
intvalue=bundle.getInt("value");
switch(value){
caseConsts.VERSION_LATEST:
Log.d("Log","推送版本是最新版本:可用");
break;
caseConsts.VERSION_OLD_PERMIT:
Log.d("Log","推送版本是老版本:可用");
break;
caseConsts.VERSION_OLD_REFUSE:
Log.e("Log","推送版本是老版本:不可用");
break;
default:
break;
}
}elseif(Consts.CLIENT_DEVICE_ID.equals(key)){
Stringdevice_id=bundle.getString("value");
Log.d("Log","设备唯一标识:"+device_id);
}
}elseif(Consts.getActionNotificationOpened(context).equals(action)){
Log.d("Log","用户点击了通知");
Bundlebundle=intent.getExtras();
//通知标题
Stringtitle=bundle.getString(Consts.NOTIFICATION_TITLE);
//通知内容
Stringcontent=bundle.getString(Consts.NOTIFICATION_CONTENT);
//通知附加字段
Stringext=bundle.getString(Consts.NOTIFICATION_EXT);
//通知消息flag
Stringflag=bundle.getString(Consts.MESSAGE_BACK_FLAG);
Intenti=newIntent(context,TestActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}elseif(Consts.getActionLbsPush(context).equals(action)){//接收到广播,需要上传经纬度
Log.d("Log","需要上传经纬度信息");
//请调用上传经纬度的接口,此处是写的固定的百度经纬度
//参数分别为context对象,经度,纬度,类型(需要通过枚举类获取,分为GPS,百度,google三种经纬度类型)
Stringmethod=LBS.BAIDU.method();//获取经纬度类型
PushMethodImpl.getInstance().UploadGpsMessage(context,"0","0",method);
}
}
}