快速集成推送通知功能---百度云推送
来源:互联网 发布:腾讯云和阿里云的区别 编辑:程序博客网 时间:2024/06/08 15:55
发现百度云推送简直太好集成了,一下午时间都用不上就搞定了
先看效果吧
发送通知
接收通知
接下来就是步骤了
1.先去百度云推送开发者平台注册账号
http://push.baidu.com/
然后创建应用,平台会分给你创建应用的app idapi key还有secret key
2.其实官网的api 就是集成手册已经写的很详细了
然后下载sdk+demo
解压后这样,把libs里面的so库和jar包添加到你的项目libs里,studio用户要在src/main 目录下创建一个JniLibs文件夹
然后jar包添加依赖
3.然后配置清单文件,需要注册一下广播和服务
注意服务广播务必写在application里
<!-- 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.YourPackageName" /><permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" android:protectionLevel="signature"></permission><!-- push service start --><!-- 用于接收系统消息以保证PushService正常运行 --><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><!-- Push服务接收客户端发送的各种请求--><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><!-- 4.4版本新增的CommandService声明,提升小米和魅族手机上的实际推送到达率 --><service android:name="com.baidu.android.pushservice.CommandService" android:exported="true" /><!-- 适配Android N系统必需的ContentProvider声明,写权限包含应用包名--><provider android:name="com.baidu.android.pushservice.PushInfoProvider" android:authorities="YourPackageName.bdpush" android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" android:protectionLevel = "signature" android:exported="true" /><!-- push结束 -->
这里把YourPackageName替换成你自己的包名,一共四处然后在你的主Activity的oncreate方法里添加这行代码一般是登录界面
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_login); PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
"tgFfUEwBRpQknfGGjqOYvVBtwVxvF77E"); }
参数
- context:当前执行Context
- loginType:绑定认证方式(无账号认证方式
PushConstants.LOGIN_TYPE_API_KEY
) - loginValue:应用的API Key
PushMessageReceiver
package com.hxzh.uniwill.lingjian.base;import android.content.Context;import android.util.Log;import com.baidu.android.pushservice.PushMessageReceiver;import com.hxzh.uniwill.lingjian.utils.LogUtil;import com.hxzh.uniwill.lingjian.utils.SharedPreferencesUtil;import java.util.List;/*** * ━━━━ Code is far away from ━━━━━━ * () () * ( ) ( ) * ( ) ( ) * ┏┛┻━━━┛┻┓ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┻ ┃ * ┗━┓ ┏━┛ * ┃ ┃ * ┃ ┗━━━┓ * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ * ━━━━ bug with the more protecting ━━━ * <p/> * Created by PangHaHa12138 on 2017/5/31. */public class PushTestReceiver extends PushMessageReceiver { /** * 调用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; LogUtil.d("通知回调",responseString); SharedPreferencesUtil.writeChannelid(channelId,context); } /** * PushManager.stopWork() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示从云推送解绑定成功;非0表示失败。 * @param requestId * 分配给对云推送的请求的id */ @Override public void onUnbind(Context context, int errorCode, String requestId) { } /** * setTags() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示某些tag已经设置成功;非0表示所有tag的设置均失败。 * @param successTags * 设置成功的tag * @param failTags * 设置失败的tag * @param requestId * 分配给对云推送的请求的id */ @Override public void onSetTags(Context context, int errorCode, List<String> successTags, List<String> failTags, String requestId) { } /** * delTags() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示某些tag已经删除成功;非0表示所有tag均删除失败。 * @param successTags * 成功删除的tag * @param failTags * 删除失败的tag * @param requestId * 分配给对云推送的请求的id */ @Override public void onDelTags(Context context, int errorCode, List<String> successTags, List<String> failTags, String requestId) { } /** * 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) { } /** * 接收透传消息的函数。 * * @param context * 上下文 * @param s * 推送的消息 * @param s1 * 自定义内容,为空或者json字符串 */ @Override public void onMessage(Context context, String s, String s1) { } /** * 接收通知点击的函数。 * * @param context * 上下文 * @param s * 推送的通知的标题 * @param s1 * 推送的通知的描述 * @param s2 * 自定义内容,为空或者json字符串 */ @Override public void onNotificationClicked(Context context, String s, String s1, String s2) { } /** * 接收通知到达的函数。 * * @param context * 上下文 * @param s * 推送的通知的标题 * @param s1 * 推送的通知的描述 * @param s2 * 自定义内容,为空或者json字符串 */ @Override public void onNotificationArrived(Context context, String s, String s1, String s2) { }}
要在清单文件声明这个广播接收者!
<!-- 此处Receiver名字修改为当前包名路径 --><receiver android:name=".base.PushTestReceiver"> <intent-filter> <!-- 接收push消息 --> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <!-- 接收bind、setTags等method的返回结果--> <action android:name="com.baidu.android.pushservice.action.RECEIVE" /> <!-- 接收通知点击事件,和通知自定义内容 --> <action android:name="com.baidu.android.pushservice.action.notification.CLICK" /> </intent-filter></receiver>
这个类是为了做通知回调用的,方法和参数注释都写的很详细
其实主要还是为了拿到百度返回的channelID
就是推送的设备唯一id 这样服务器这边集成需要做为登录参数
可以看到打印的log
这样就已经把设备channelid和通知id绑定了,在开发者管理平台创建通知或者后台发送通知都会收到了,如果需要自定义通知的话
自定义通知Builder-- BasicPushNotificationBuilder
函数原型
BasicPushNotificationBuilder();
功能
自定义通知状态栏构建类构造函数(定制通知栏基础样式) 。
自定义通知Builder-- CustomPushNotificationBuilder
函数原型
CustomPushNotificationBuilder(layoutId, layoutIconId, layoutTitleId, layoutTextId);
功能
自定义通知状态栏构建类构造函数(定制通知栏基础样式及layout)。
参数
- layoutId :自定义layout资源id
- layoutIconId :自定义layout中显示icon 的id
- layoutTitleId :自定义layout中显示标题的id
- layoutTextId :自定义layout中显示内容的id
设置通知flags-- setNotificationFlags
函数原型
public void setNotificationFlags (int flags);
功能
基类PushNotificationBuilder定义的方法,定制 Android Notification 里的flags。
参数
- flags :Android Notification flags
设置通知defaults-- setNotificationDefaults
函数原型
public void setNotificationDefaults (int defaults);
功能
基类PushNotificationBuilder定义的方法,定制 Android Notification 里的defaults。
参数
- defaults :Android Notification defaults
设置通知状态栏icon-- setStatusbarIcon
函数原型
public void setStatusbarIcon (int icon);
功能
基类PushNotificationBuilder类定义的方法,定制 Android Notification通知状态栏的icon图标。
参数
- icon :图标资源id
设置通知样式图片-- setLayoutDrawable
函数原型
public void setLayoutDrawable(int drawableId);
功能
CustomPushNotificationBuilder类定义的方法,定制自定义layout中显示的图片。
参数
drawableId :图标资源id
设置通知样式声音-- setNotificationSound
函数原型
public void setNotificationSound(String soundId);
功能
CustomPushNotificationBuilder类定义的方法,自定义推送的声音。
参数
soundId :声音资源路径
开启调试模式-- enableDebugMode
函数原型
public static void enableDebugMode(boolean debugEnabled);
功能
PushSettings类定义的方法,开启调试模式,会输出调试Log。
注意:发布应用时,请不要开启调试模式,否则会降低Push性能。
参数
- debugEnabled :true打开调试模式,false关闭调试模式。
开启精确LBS推送模式-- enableLbs
函数原型
public static void enableLbs(Context context);
功能
PushManager类定义的方法,开启精确LBS推送模式,覆盖最近半小时内在指定区域出现过的终端。
参数
- Context :android app运行上下文
关闭精确LBS推送模式-- disableLbs
函数原型
public static void disableLbs(Context context);
功能
PushManager类定义的方法,关闭精确LBS推送模式,关闭后,服务端将无法有效发送基于地理位置的定向推送。
参数
- Context :android app运行上下文
获取绑定请求的结果-- onBind
函数原型
public void onBind(Context context, int errorCode, String appid, String userId, String channelId, String requestId);
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。调用PushManager.startWork
后,sdk将对push server发起绑定请求,这个过程是异步的。绑定请求的结果通过onBind返回。 如果您需要用单播推送,需要把这里获取的channel id上传到应用server中,再调用server接口用channel id给单个手机或者用户推送。
参数
- context BroadcastReceiver的执行Context
- errorCode 绑定接口返回值,0 - 成功
- appid 应用id,errorCode非0时为null
- userId 应用user id,errorCode非0时为null
- channelId 应用channel id,errorCode非0时为null
- requestId 向服务端发起的请求id,在追查问题时有用
接收透传消息的函数-- onMessage
函数原型
public void onMessage(Context context, String message, String customContentString);
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。接收透传消息。
参数
- context 上下文
- message 推送的消息
- customContentString 自定义内容,为空或者json字符串
接收通知点击的函数-- onNotificationClicked
函数原型
public void onNotificationClicked(Context context, String title, String description, String customContentString)
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。接收通知点击的函数。
参数
- context 上下文
- title 推送的通知的标题
- description 推送的通知的描述
- customContentString 自定义内容,为空或者json字符串
接收通知到达的函数-- onNotificationArrived
函数原型
public void onNotificationArrived(Context context, String title, String description, String customContentString)
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。接收通知到达的函数。
参数
- context 上下文
- title 推送的通知的标题
- description 推送的通知的描述
- customContentString 自定义内容,为空或者json字符串
setTags的回调函数-- onSetTags
函数原型
public void onSetTags(Context context, int errorCode, List sucessTags, List failTags, String requestId);
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。setTags() 的回调函数。
参数
- context 上下文
- errorCode 错误码,0表示某些tag已经设置成功,非0表示所有tag的设置均失败
- successTags 设置成功的tag
- failTags 设置失败的tag
- requestId 分配给对云推送的请求的id
delTags的回调函数-- onDelTags
函数原型
public void onDelTags(Context context, int errorCode, List sucessTags, List failTags, String requestId)
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。delTags() 的回调函数。
参数
- context 上下文
- errorCode 错误码。0表示某些tag已经删除成功;非0表示所有tag均删除失败
- successTags 成功删除的tag
- failTags 删除失败的tag
- requestId 分配给对云推送的请求的id
listTags的回调函数-- onListTags
函数原型
public void onListTags(Context context, int errorCode, List tags, String requestId);
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。listTags() 的回调函数。
参数
- context 上下文
- errorCode 错误码。0表示列举tag成功;非0表示失败
- tags 当前应用设置的所有tag
- requestId 分配给对云推送的请求的id
stopWork的回调函数-- onUnbind
函数原型
public void onUnbind(Context context, int errorCode, String requestId);
功能
PushMessageReceiver的抽象方法,把receiver类继承PushMessageReceiver可以使用。PushManager.stopWork()
的回调函数。
参数
- context 上下文
- errorCode 错误码,0表示从云推送解绑定成功,非0表示失败
- requestId 分配给对云推送的请求的id
常量说明
Android SDK的常量定义都在PushConstants类中。如:Push发送给应用的Action的常量,都是String类型
ACTION_MESSAGE
接收消息时使用。
ACTION_RECEIVE
获取方法调用的返回值,包括绑定、设置Tag、删除Tag等方法。
ACTION_RECEIVER_NOTIFICATION_CLICK
通知点击事件的截获。
注意:通知发出时,应用从Intent中获取Extra的常量,均为String类型
EXTRA_PUSH_MESSAGE_STRING
消息内容,在ACTION_MESSAGE中使用。Extra获取方法
intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING)
EXTRA_METHOD
方法名,在ACTION_RECEIVE中使用。Extra获取方法
intent.getStringExtra(PushConstants.EXTRA_METHOD)
EXTRA_ERROR_CODE
方法错误码,在ACTION_RECEIVE中使用。Extra获取方法
intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE)
EXTRA_CONTENT
方法返回内容,在ACTION_RECEIVE中使用。Extra获取方法
intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT)
EXTRA_NOTIFICATION_TITLE
通知标题,在ACTION_RECEIVER_NOTIFICATION_CLICK中使用。Extra获取方法
intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE)
EXTRA_NOTIFICATION_CONTENT
通知内容,在ACTION_RECEIVER_NOTIFICATION_CLICK中使用。Extra获取方法
intent.getStringExtra(PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)
方法名常量,都是String类型
METHOD_BIND, METHOD_SET_TAGS, METHOD_DEL_TAGS
绑定方法名,在EXTRA_METHOD中取得的值是这三者之一。
最后----多谢阅读
- 快速集成推送通知功能---百度云推送
- android基于百度云消息推送通知集成
- maven集成百度云推送
- (4.2.12.1)浅谈第三方推送[快速集成]:百度推送、小米推送、华为推送、魅族推送
- 百度推送 集成实例
- 百度推送 项目集成
- 百度云推送自定义通知行为
- 消息推送之百度云推送Android集成与使用方法
- 快速集成极光推送
- 极光推送快速集成
- 百度云推送sdk集成问题
- phonegap 集成百度云推送插件详解
- 百度云推送集成之Maven方式
- Android Studio集成百度云推送
- 使用百度云推送实现推送功能详解
- Android推送 百度云推送
- Android推送 百度云推送
- 百度推送(Push)简单集成
- 如何解决xhprof的报错failed to shell execute cmd=" "/usr/bin/dot" -Tpng" proc_open() has been ...
- 2.2节
- 2013计算机视觉代码合集三
- 工厂模式
- 抽象工厂模式
- 快速集成推送通知功能---百度云推送
- php微信
- codevs1262 不要把球传我
- [学习笔记]Hadoop安装_配置
- hihoCoder 1014 Trie树 字典树模板
- 适配器模式
- 考研数学笔记 56~60
- 权限管理--------分配权限
- OpenJudge 1996:登山——题解