- 作者:Mr.Egg
- 状态:完成
- 转载请注明
百度云推送是向开发者提供推送服务的SDK,懒着讲废话直接上干货。
首先,注册账号,创建应用,申请API Key这些基础的不说了。
在这些完成后可以跟着下载的Demo写。
第一步在 app目录下建立资源文件夹jniLibs,把SDK中对应lib拷入
第二步最烦的一步,在manifest文件中加入权限声明
<!-- 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.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.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<!-- 适配Android N系统必需的ContentProvider写权限声明,写权限包含应用包名-->
<uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER..yourpackage" />
<permission
android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.yourpackage"
android:protectionLevel="normal">
</permission>
接下来在<application>标签中加入
<!-- 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替换成继承PushMessageReceiver的类-->
<receiver android:name="yourpackage.Receiver" >
<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="yourpackage.bdpush"
android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.yourpackage"
android:protectionLevel = "signature"
android:exported="true" />
以上注意把yourpackage替换成项目对应包名
注意替换Receiver
最后,在MainActivity中加入
PushManager .startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,"apikey");
把APIKEY替换成你自己的就行了
这些完成后去推送控制台测试即可。
最后,留下我的manifest结构(可以直接略过)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mregg.baidupushtest">
<!-- 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.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.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<!-- 适配Android N系统必需的ContentProvider写权限声明,写权限包含应用包名-->
<uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.example.mregg.baidupushtest" />
<permission
android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.example.mregg.baidupushtest"
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.example.mregg.baidupushtest.PushReceiver" >
<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.example.mregg.baidupushtest.bdpush"
android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.com.example.mregg.baidupushtest"
android:protectionLevel = "signature"
android:exported="true" />
</application>
</manifest>