Android之百度云推送(一)如何导入手机apk
2016-07-27 15:45
609 查看
Android之百度云推送(一)如何导入手机apk
进入控制台并创建应用
http://push.baidu.com/console/appAndroid应用配置
安卓应用需要配置应用的包名获取应用的ApiKey/SecretKey
我们先来测试一下
发送成功了:
手机通知信息
原来这样好玩。。。
百度官网demo怎么运行咱们不说了
看怎么自己写个demo步骤
导入云推送jar包和so文件:
配置AndroidManifest文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yang.baidupushdemo"> <!-- Push service 运行需要的权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" /> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <!-- 富媒体需要声明的权限 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /> <!-- 适配Android N系统必需的ContentProvider写权限声明,写权限包含应用包名--> <uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.yang.baidupushdemo" /> <permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.yang.baidupushdemo" android:protectionLevel="normal"> </permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- push富媒体,使用富媒体推送必须声明 --> <activity android:name="com.baidu.android.pushservice.richmedia.MediaViewActivity" android:configChanges="orientation|keyboardHidden" android:label="MediaViewActivity" > </activity> <activity android:name="com.baidu.android.pushservice.richmedia.MediaListActivity" android:configChanges="orientation|keyboardHidden" android:label="MediaListActivity" android:launchMode="singleTask" > </activity> <!-- push富媒体结束 --> <!-- push应用定义消息receiver声明 --> <receiver android:name="com.yang.baidupushdemo.MyPushMessageReceiver" > <intent-filter> <!-- 接收push消息 --> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <!-- 接收bind,unbind,fetch,delete等反馈消息 --> <action android:name="com.baidu.android.pushservice.action.RECEIVE" /> <action android:name="com.baidu.android.pushservice.action.notification.CLICK" /> </intent-filter> </receiver> <!-- push必须的receviver和service声明 --> <receiver android:name="com.baidu.android.pushservice.PushServiceReceiver" android:process=":bdservice_v1" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="com.baidu.android.pushservice.action.notification.SHOW" /> <action android:name="com.baidu.android.pushservice.action.media.CLICK" /> <!-- 以下四项为可选的action声明,可大大提高service存活率和消息到达速度 --> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> </intent-filter> </receiver> <receiver android:name="com.baidu.android.pushservice.RegistrationReceiver" android:process=":bdservice_v1" > <intent-filter> <action android:name="com.baidu.android.pushservice.action.METHOD" /> <action android:name="com.baidu.android.pushservice.action.BIND_SYNC" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <service android:name="com.baidu.android.pushservice.PushService" android:exported="true" android:process=":bdservice_v1" > <intent-filter> <action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" /> </intent-filter> </service> <service android:name="com.baidu.android.pushservice.CommandService" android:exported="true" /> <!-- 适配Android N系统必需的ContentProvider声明,写权限包含应用包名--> <provider android:name="com.baidu.android.pushservice.PushInfoProvider" android:authorities="com.yang.baidupushdemo.bdpush" android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.yang.baidupushdemo" android:exported="true" /> </application> </manifest>
启动云推送MainActivity
package com.yang.baidupushdemo; import android.app.Activity; import android.os.Bundle; import com.baidu.android.pushservice.PushConstants; import com.baidu.android.pushservice.PushManager; public class MainActivity extends Activity { public final String API_KEY = "YUG5N60Ga6Y28u0vPb8iGKG3"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, API_KEY); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.yang.baidupushdemo.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:id="@+id/id_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
MyPushMessageReceiver
package com.yang.baidupushdemo; /** * Created by yang_zzheng on 2016/7/27 * yangzhizheng2012@163.com */ import android.content.Context; import android.text.TextUtils; import android.util.Log; import com.baidu.android.pushservice.PushMessageReceiver; import org.json.JSONException; import org.json.JSONObject; import java.util.List; /** * Push消息处理receiver。请编写您需要的回调函数, 一般来说: onBind是必须的,用来处理startWork返回值; * onMessage用来接收透传消息; onSetTags、onDelTags、onListTags是tag相关操作的回调; * onNotificationClicked在通知被点击时回调; onUnbind是stopWork接口的返回值回调 * * 返回值中的errorCode,解释如下: * 0 - Success * 10001 - Network Problem * 30600 - Internal Server Error * 30601 - Method Not Allowed * 30602 - Request Params Not Valid * 30603 - Authentication Failed * 30604 - Quota Use Up Payment Required * 30605 - Data Required Not Found * 30606 - Request Time Expires Timeout * 30607 - Channel Token Timeout * 30608 - Bind Relation Not Found * 30609 - Bind Number Too Many * * 当您遇到以上返回错误时,如果解释不了您的问题,请用同一请求的返回值requestId和errorCode联系我们追查问题。 * */ public class MyPushMessageReceiver extends PushMessageReceiver { /** TAG to Log */ public static final String TAG = MyPushMessageReceiver.class.getSimpleName(); /** * 调用PushManager.startWork后,sdk将对push * server发起绑定请求,这个过程是异步的。绑定请求的结果通过onBind返回。 如果您需要用单播推送,需要把这里获取的channel * id和user id上传到应用server中,再调用server接口用channel id和user id给单个手机或者用户推送。 * * @param context BroadcastReceiver的执行Context * @param errorCode 绑定接口返回值,0 - 成功 * @param appid 应用id。errorCode非0时为null * @param userId 应用user id。errorCode非0时为null * @param channelId 应用channel id。errorCode非0时为null * @param requestId 向服务端发起的请求id。在追查问题时有用; * @return none */ @Override public void onBind(Context context, int errorCode, String appid, String userId, String channelId, String requestId) { String responseString = "onBind errorCode=" + errorCode + " appid=" + appid + " userId=" + userId + " channelId=" + channelId + " requestId=" + requestId; Log.d(TAG, responseString); if (errorCode == 0) { // 绑定成功 Log.d(TAG, "绑定成功"); } } /** * 接收透传消息的函数。 * * @param context 上下文 * @param message 推送的消息 * @param customContentString 自定义内容,为空或者json字符串 */ @Override public void onMessage(Context context, String message, String customContentString) { String messageString = "透传消息 message=\"" + message + "\" customContentString=" + customContentString; Log.d(TAG, messageString); // 自定义内容获取方式,mykey和myvalue对应透传消息推送时自定义内容中设置的键和值 if (!TextUtils.isEmpty(customContentString)) { JSONObject customJson = null; try { customJson = new JSONObject(customContentString); String myvalue = null; if (!customJson.isNull("mykey")) { myvalue = customJson.getString("mykey"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 接收通知点击的函数。 * * @param context 上下文 * @param title 推送的通知的标题 * @param description 推送的通知的描述 * @param customContentString 自定义内容,为空或者json字符串 */ @Override public void onNotificationClicked(Context context, String title, String description, String customContentString) { String notifyString = "通知点击 title=\"" + title + "\" description=\"" + description + "\" customContent=" + customContentString; Log.d(TAG, notifyString); // 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值 if (!TextUtils.isEmpty(customContentString)) { JSONObject customJson = null; try { customJson = new JSONObject(customContentString); String myvalue = null; if (!customJson.isNull("mykey")) { myvalue = customJson.getString("mykey"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 接收通知到达的函数。 * * @param context 上下文 * @param title 推送的通知的标题 * @param description 推送的通知的描述 * @param customContentString 自定义内容,为空或者json字符串 */ @Override public void onNotificationArrived(Context context, String title, String description, String customContentString) { String notifyString = "onNotificationArrived title=\"" + title + "\" description=\"" + description + "\" customContent=" + customContentString; Log.d(TAG, notifyString); // 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值 if (!TextUtils.isEmpty(customContentString)) { JSONObject customJson = null; try { customJson = new JSONObject(customContentString); String myvalue = null; if (!customJson.isNull("mykey")) { myvalue = customJson.getString("mykey"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * setTags() 的回调函数。 * * @param context 上下文 * @param errorCode 错误码。0表示某些tag已经设置成功;非0表示所有tag的设置均失败。 * @param failTags 设置失败的tag * @param requestId 分配给对云推送的请求的id */ @Override public void onSetTags(Context context, int errorCode, List<String> sucessTags, List<String> failTags, String requestId) { String responseString = "onSetTags errorCode=" + errorCode + " sucessTags=" + sucessTags + " failTags=" + failTags + " requestId=" + requestId; Log.d(TAG, responseString); } /** * delTags() 的回调函数。 * * @param context 上下文 * @param errorCode 错误码。0表示某些tag已经删除成功;非0表示所有tag均删除失败。 * @param failTags 删除失败的tag * @param requestId 分配给对云推送的请求的id */ @Override public void onDelTags(Context context, int errorCode, List<String> sucessTags, List<String> failTags, String requestId) { String responseString = "onDelTags errorCode=" + errorCode + " sucessTags=" + sucessTags + " failTags=" + failTags + " requestId=" + requestId; Log.d(TAG, responseString); } /** * listTags() 的回调函数。 * * @param context 上下文 * @param errorCode 错误码。0表示列举tag成功;非0表示失败。 * @param tags 当前应用设置的所有tag。 * @param requestId 分配给对云推送的请求的id */ @Override public void onListTags(Context context, int errorCode, List<String> tags, String requestId) { String responseString = "onListTags errorCode=" + errorCode + " tags=" + tags; Log.d(TAG, responseString); } /** * PushManager.stopWork() 的回调函数。 * * @param context 上下文 * @param errorCode 错误码。0表示从云推送解绑定成功;非0表示失败。 * @param requestId 分配给对云推送的请求的id */ @Override public void onUnbind(Context context, int errorCode, String requestId) { String responseString = "onUnbind errorCode=" + errorCode + " requestId = " + requestId; Log.d(TAG, responseString); if (errorCode == 0) { // 解绑定成功 Log.d(TAG, "解绑成功"); } } }
注意注意注意
运行一定要真机测试
网页测试吧
http://push.baidu.com/console/YUG5N60Ga6Y28u0vPb8iGKG3?device_type=3#/pushs看着不过瘾 咱们配置服务端
http://blog.csdn.net/xyzz609/article/details/52292589相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析