到www.zypush.com申请appkeyandroid
将zhiyou_push.**.jar,liblptcpjin.so库以及push_notification.xml,push_download.xml文件导入到工程内部数组
<注意:在配置过程当中有些地方须要用到包名以及appkey>网络
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
代码块<!-- push service(必要) --><service android:name=”com.joboevan.push.service.PushService”></service><!--应用包名.PushReceiver 定义推送消息接收器,PushReceiver在SDK中有这个类--> <!-- <receiver android:name="YOUR_PACKAGENAME.PushReceiver" > --><!--com.zypush.activity 这是我本身的包名--> <receiver android:name=”com.zypush.activity.PushRecevier”> <intent-filter> <!-- <action android:name=”com.zypush.android.intent.message.申请的appkey”/> --> <!--用户接收消息的action--> <action android:name=”com.zypush.android.intent.message.f262516bedf4c0f567b5659840920f94”/> <!--用户打开通知栏的action--> <action android:name=”com.zypush.android.intent.NOTIFICATION_OPENED.f262516bedf4c0f567b5659840920f94”/> <!-- 须要上传经纬度的action --> <action android:name=”com.zypush.android.intent.LBS_PUSH.f262516bedf4c0f567b5659840920f94”/> </intent-filter></receiver><!--- 心跳广播接收器注册(必要) --><receiver android:name="com.joboevan.push.receiver.CheckConnectRecevier" android:enabled="true" /><!-- 网络改变广播监听 (必要)--> <receiver android:name="com.joboevan.push.receiver.NetworkReceiver" > <intent-filter > <action android: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" /><!-- 富媒体下载服务 (必要)--> <service android:name="com.joboevan.push.providers.downloads.DownloadService" /><!-- 富媒体下载接收器 (必要)--> <receiver android:exported="false" android:name="com.joboevan.push.providers.downloads.DownloadReceiver" > <intent-filter > <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver><!-- 配置的第三方参数属性 测试帐号:zypush 密码:123456 --> <!-- http://www.zypush.com --> <meta-data android:name="ZYPUSH_APPKEY" android:value="f262516bedf4c0f567b5659840920f94" /> |
推送服务运行所须要的权限app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
代码块 <!--自定义权限--> <permission android:name="com.zypush.permission.ACCESS_DOWNLOAD_MANAGER" /> <!-- 网络权限 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 网络状态权限 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 往sdcard中写入数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 获取手机设备号权限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 手机震动权限 --> <uses-permission android:name="android.permission.VIBRATE" /> <!-- 引用富媒体自定义权限 --> <uses-permission android:name="com.zypush.permission.ACCESS_DOWNLOAD_MANAGER" /> <!-- 唤醒权限 --> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- 获取当前任务权限 --> <uses-permission android:name="android.permission.GET_TASKS" /> <!-- 悬浮窗体权限--> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 保留权限,后续添加功能须要用到 --> <!-- network网络开关权限(可选权限) --> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <!-- 获取基站须要的权限 (可选权限)--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
4.1启动推送服务
函数原型tcp
1 2 |
代码块PushManager.getInstance().connect(Context context,boolean isReconnect) |
参数
context: Context对象
isReconnect: 设置是否自动重连,true:表示断线之后自动重连,false标示断线之后不会自动重连ide
返回值
true:开启推送的条件都知足
false:条件不知足开启推送函数
示例测试
1 2 |
代码块boolean result = PushManager.getInstance().connect(getApplicationContext(),true); |
4.2设置别名this
须要注意的是:这个接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖以前的设置。google
一个用户只能够设置一个别名,至关于一我的的名字,为了确保其惟一性,通常是用用户名设置别名,推送能够根据别名进行推送到我的客户端。设置别名超时时间为15秒,别名的最大长度不得超过40个字节,能够在PushReceiver中接收设置别名返回给客户端的返回值
1 2 |
代码块PushManager.getInstance().bindAlias(Context context, String alias) |
参数
context: Context对象
alias:设置的别名
返回值
无
示例
1 2 |
代码块PushManager.getInstance().bindAlias(this, “别名”); |
4.3设置标签
须要注意的是:这个接口是覆盖逻辑,而不是增量逻辑。即新的调用会覆盖以前的设置。
标签的功能能够将一个总体划分红不一样的群体,好比说将全部的用户划分红“男”用户和“女”用户,那么就能够给用户设置标签“男”或“女”。一个用户能够设置多个标签,多个标签的关系是或的关系。设置标签超时时间为15秒,单个标签的最大长度不得超过40个字节,而且不能为空,一个客户端最多能够设置100个标签。
函数原型
1 2 |
代码块PushManager.getInstance().setTags(Context context , List<String> tags) |
参数
context: Context对象
tags: 设置标签的List数组
返回值
无
示例
1 2 |
代码块PushManager.getInstance().setTags(this,tags); |
4.4设置通知栏图标
设置顶层通知栏图标
函数原型
1 2 |
代码块PushManager.getInstance().setNotificationIcon(Context context ,int value); |
参数
context: Context对象
value: 通知栏图标的资源id
返回值
无
示例
1 2 |
代码块PushManager.getInstance().setNotificationIcon(this , R.drawable.icon); |
4.5设置是否开启智能提醒
是否开启智能提醒。
函数原型
1 2 |
代码块PushManager.getInstance(). isOpenRemind(Context context , boolean state , String content , int days , int startTime , int endTime) ; |
参数
context: Context对象
state : 设置是否开启智能提醒,true:表示开启智能提醒,false表示暂停智能提醒
content:设置提醒内容
days:相隔几天以后没有打开应用,进行提醒
startTime:在startTime以后进行提醒0-23
endTime:在endTime以前进行提醒0-23
返回值
true:开启智能提醒成功
false:开启智能提醒失败
示例
1 2 |
代码块boolean result = PushManager.getInstance().isOpenRemind(this, true, "这是一个智能提醒", 3, 0, 23) ; |
想要看更多的API说明,能够到www.zypush.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
代码块package com.zypush.activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.util.Log;import com.joboevan.push.bean.SendBroastBean;import com.joboevan.push.tool.Consts;import com.joboevan.push.tool.LBS;import com.joboevan.push.tool.Tool;/** * 推送消息接收器 * * **/public class PushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (Consts.getACTION_RECEIVER_KEY(context).equals(action)) { Bundle bundle = intent.getExtras(); String key = bundle.getString("key"); if (Consts.MESSAGE_KEY_CONNECT.equals(key)) { // 链接结果 int value = bundle.getInt("value"); switch (value) { case Consts.RESULT_Y: Log.d("Log", "链接成功"); break; case Consts.RESULT_N: Log.d("Log", "链接失败"); break; } } else if (Consts.MESSAGE_KEY_LOGIN.equals(key)) { // 登录结果 int value = bundle.getInt("value"); switch (value) { case Consts.RESULT_Y: Log.d("Log", "登录成功"); break; case Consts.RESULT_N: Log.d("Log", "登录失败"); break; } } else if (Consts.MESSAGE_KEY_SETALIAS.equals(key)) { int value = bundle.getInt("value"); switch (value) { case Consts.RESULT_Y: Log.d("Log", "别名设置成功"); break; case Consts.RESULT_N: Log.d("Log", "别名设置失败"); break; case Consts.RESULT_I: Log.d("Log", "正在设置别名"); break; default: break; } } else if (Consts.MESSAGE_KEY_CLEANALIAS.equals(key)) { int value = bundle.getInt("value"); switch (value) { case Consts.RESULT_Y: Log.d("Log", "清除别名成功"); break; case Consts.RESULT_N: Log.d("Log", "清除别名失败"); break; case Consts.RESULT_I: Log.d("Log", "正在清除别名"); break; default: break; } } else if (Consts.MESSAGE_KEY_SETTAGS.equals(key)) { int value = bundle.getInt("value"); switch (value) { case Consts.RESULT_Y: Log.d("Log", "标签设置成功"); break; case Consts.RESULT_N: Log.d("Log", "标签设置失败"); break; case Consts.RESULT_I: Log.d("Log", "正在设置标签"); break; default: break; } } else if (Consts.MESSAGE_KEY_CLEANTAGS.equals(key)) { int value = bundle.getInt("value"); switch (value) { case Consts.RESULT_Y: Log.d("Log", "标签清除成功"); break; case Consts.RESULT_N: Log.d("Log", "标签清除失败"); break; case Consts.RESULT_I: Log.d("Log", "正在清除标签"); break; default: break; } } else if (Consts.MESSAGE_KEY_CUSTOM.equals(key)) { // 接收自定义推送信息 String value = bundle.getString("value"); String flag = bundle.getString(Consts.MESSAGE_BACK_FLAG); Log.w("Log", "推送自定义消息: " + value); Log.w("Log", "推送自定义消息id: " + flag); //这个方法是推送内部封装生成通知的方法 CustomNotification notification = new CustomNotification(); notification.createNotification(context); } else if (Consts.MESSAGE_KEY_NOTIFICATION.equals(key)) { // 如需在工程中展现通知标题和内容,请设置是否展现通知内容 String title = bundle.getString(Consts.NOTIFICATION_TITLE); String content = bundle.getString(Consts.NOTIFICATION_CONTENT); String ext = bundle.getString(Consts.NOTIFICATION_EXT); String flag = bundle.getString(Consts.MESSAGE_BACK_FLAG); Log.d("Log", "通知标题:" + title); Log.d("Log", "通知内容:" + content); Log.d("Log", "通知附加字段:" + ext); Log.d("Log", "通知消息id:" + flag); } else if (Consts.MESSAGE_KEY_PUSHSTATECHANGED.equals(key)) { // 客户端与服务端的状态 int value = bundle.getInt("value"); switch (value) { case Consts.PUSH_CONNECTSTATE_CONNECTING: // 通道链接 Log.d("Log", "推送服务已经链接"); break; case Consts.PUSH_CONNECTSTATE_DISCONNECT: // 通道断开 Log.e("Log", "推送服务已经断开"); break; } } else if (Consts.ACTION_RECEIVER_VERSION.equals(key)) { int value = bundle.getInt("value"); switch (value) { case Consts.VERSION_LATEST: Log.d("Log","推送版本是最新版本:可用"); break; case Consts.VERSION_OLD_PERMIT: Log.d("Log","推送版本是老版本:可用"); break; case Consts.VERSION_OLD_REFUSE: Log.e("Log","推送版本是老版本:不可用"); break; default: break; } } else if (Consts.CLIENT_DEVICE_ID.equals(key)) { String device_id = bundle.getString("value"); Log.d("Log", "设备惟一标识:" + device_id); } } else if (Consts.getActionNotificationOpened(context).equals(action)) { Log.d("Log", "用户点击了通知"); Bundle bundle = intent.getExtras() ; //通知标题 String title = bundle.getString(Consts.NOTIFICATION_TITLE); //通知内容 String content = bundle.getString(Consts.NOTIFICATION_CONTENT); //通知附加字段 String ext = bundle.getString(Consts.NOTIFICATION_EXT); //通知消息flag String flag = bundle.getString(Consts.MESSAGE_BACK_FLAG); Intent i = new Intent(context, TestActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } else if (Consts.getActionLbsPush(context).equals(action)) { // 接收到广播,须要上传经纬度 Log.d("Log", "须要上传经纬度信息"); // 请调用上传经纬度的接口,此处是写的固定的百度经纬度 // 参数分别为context对象,经度,纬度,类型(须要经过枚举类获取,分为GPS,百度,google三种经纬度类型) String method = LBS.BAIDU.method(); // 获取经纬度类型 PushMethodImpl.getInstance().UploadGpsMessage(context,"0", "0", method); } }} |