- 作者:Mr.Egg
- 狀態(tài):完成
- 轉(zhuǎn)載請注明
百度云推送是向開發(fā)者提供推送服務(wù)的SDK,懶著講廢話直接上干貨产园。
首先汞斧,注冊賬號,創(chuàng)建應(yīng)用什燕,申請API Key這些基礎(chǔ)的不說了粘勒。
在這些完成后可以跟著下載的Demo寫。
第一步在 app目錄下建立資源文件夾jniLibs屎即,把SDK中對應(yīng)lib拷入
第二步最煩的一步庙睡,在manifest文件中加入權(quán)限聲明
<!-- Push service 運行需要的權(quán)限 -->
<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" />
<!-- 富媒體需要聲明的權(quán)限 -->
<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系統(tǒng)必需的ContentProvider寫權(quán)限聲明,寫權(quán)限包含應(yīng)用包名-->
<uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER..yourpackage" />
<permission
android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.yourpackage"
android:protectionLevel="normal">
</permission>
接下來在<application>標(biāo)簽中加入
<!-- 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富媒體結(jié)束 -->
<!-- push應(yīng)用定義消息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存活率和消息到達(dá)速度 -->
<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系統(tǒng)必需的ContentProvider聲明,寫權(quán)限包含應(yīng)用包名-->
<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替換成項目對應(yīng)包名
注意替換Receiver
最后雕擂,在MainActivity中加入
PushManager .startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,"apikey");
把APIKEY替換成你自己的就行了
這些完成后去推送控制臺測試即可啡邑。
最后,留下我的manifest結(jié)構(gòu)(可以直接略過)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mregg.baidupushtest">
<!-- Push service 運行需要的權(quán)限 -->
<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" />
<!-- 富媒體需要聲明的權(quán)限 -->
<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系統(tǒng)必需的ContentProvider寫權(quán)限聲明井赌,寫權(quán)限包含應(yīng)用包名-->
<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富媒體結(jié)束 -->
<!-- push應(yīng)用定義消息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存活率和消息到達(dá)速度 -->
<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系統(tǒng)必需的ContentProvider聲明,寫權(quán)限包含應(yīng)用包名-->
<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>