百度和极光的推送

    xiaoxiao2026-06-07  1

    #Android 端如何集成推送服务 导语:

    目前本公司用到的推送服务 主要有两大厂商 1.百度推送;2.极光推送;市场上还是有很多的推送;比如腾讯的 讯鸽,还有个推,小米推送,华为推送;多不胜数。此次就主要讲解下百度推送与极光推送的使用。

    百度推送

    1.第一步就是在百度开发者服务管理中创建项目,然后拿到API key , Secret Key ;这个过程就不多说了,上官网直接申请就行

    2.下载云推送的客户端SDK,SDK的压缩文件中包含一个例子代码,一个用户手册,和所需的libs和资源等(其实直接看用户手册和Demo基本就没问题了)。

    3.根据百度平台开发文档,一步一步做,仔细,不要遗漏基本不会出什么问题。

    4.重点就介绍下 Android 端 接受推送过来消息的 类。 我在项目中 起名字为BaiDuPushReceiver 下面为BaiDuPushReceiver的详细内容

    /** * 百度推送的接受类 * * @ClassName: BaiDuPushReceiver * @Description: TODO( 百度推送的接受类) * @author EmperorBoBo * @date 2016-6-30 * @company: */ public class BaiDuPushReceiver extends PushMessageReceiver { private String TAG = "BaiDuPushReceiver"; private PushMessageBean model = null; /** * 调用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.e(TAG+"baidu-->", responseString); if (errorCode == 0) { // 绑定成功 Log.e(TAG, "绑定成功"); } // 还需处理 } /** * setTags() 的回调函数。 * * @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) { // TODO Auto-generated method stub } /** * 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) { // TODO Auto-generated method stub } /** * 接收透传消息的函数。 * * @param context * 上下文 * @param message * 推送的消息 * @param customContentString * 自定义内容,为空或者json字符串 */ @Override public void onMessage(Context context, String message, String customContentString) { } /** * 接收通知到达的函数。 * * @param context * 上下文 * @param title * 推送的通知的标题 * @param description * 推送的通知的描述 * @param customContentString * 自定义内容,为空或者json字符串 */ @Override public void onNotificationArrived(Context context, String title, String description, String customContentString) { // 这是用来接受自定义的内容的 } /** * 接收通知点击的函数。 * * @param context * 上下文 * @param title * 推送的通知的标题 * @param description * 推送的通知的描述 * @param customContentString * 自定义内容,为空或者json字符串 */ @Override public void onNotificationClicked(Context context, String title, String description, String customContentString) { //当通知被 点击时候再这个方法里进行逻辑处理 } /** * 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) { //通过标签推送接受的消息方法 } /** * PushManager.stopWork() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示从云推送解绑定成功;非0表示失败。 * @param requestId * 分配给对云推送的请求的id */ @Override public void onUnbind(Context arg0, int arg1, String arg2) { // TODO Auto-generated method stub } }

    此类当中 对个方法介绍的已经很详细了。

    极光推送

    导语:

    百度退送的集成步骤对于极光推送依然适用,再次不在赘述,重点介绍对推送过来的消息接受类的时候用。

    贴出代码

    /** * 自定义接收器 * * 如果不定义这个 Receiver,则: 1) 默认用户会打开主界面 2) 接收不到自定义消息 */ public class MyReceiver extends BroadcastReceiver { private static final String TAG = "JPush"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.d(TAG, "运行自定义的通知"); Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + "---->" + printBundle(bundle)); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { String regId = bundle .getString(JPushInterface.EXTRA_REGISTRATION_ID); Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId); // send the Registration Id to your server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent .getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent .getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的通知"); int notifactionId = bundle .getInt(JPushInterface.EXTRA_NOTIFICATION_ID); Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE); String content = bundle.getString(JPushInterface.EXTRA_ALERT); Log.d(TAG, "Title 标题:: " + title + " " + "Content : 内容:" + content); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent .getAction())) { Log.d(TAG, "[MyReceiver] 用户点击打开了通知"); JPushInterface.reportNotificationOpened(context, bundle.getString(JPushInterface.EXTRA_MSG_ID)); // 打开自定义的Activity Intent i = new Intent(context, TestActivity.class); i.putExtras(bundle); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent .getAction())) { Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA)); // 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, // 打开一个网页等.. } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent .getAction())) { boolean connected = intent.getBooleanExtra( JPushInterface.EXTRA_CONNECTION_CHANGE, false); Log.e(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected); } else { Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction()); } } // 打印所有的 intent extra 数据 private static String printBundle(Bundle bundle) { StringBuilder sb = new StringBuilder(); for (String key : bundle.keySet()) { if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) { sb.append("\nkey:" + key + ", value:" + bundle.getInt(key)); } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) { sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key)); } else { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } } return sb.toString(); } // send msg to MainActivity private void processCustomMessage(Context context, Bundle bundle) { if (MainActivity.isForeground) { String message = bundle.getString(JPushInterface.EXTRA_MESSAGE); String extras = bundle.getString(JPushInterface.EXTRA_EXTRA); Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION); msgIntent.putExtra(MainActivity.KEY_MESSAGE, message); if (!ExampleUtil.isEmpty(extras)) { try { JSONObject extraJson = new JSONObject(extras); if (null != extraJson && extraJson.length() > 0) { msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras); } } catch (JSONException e) { } } context.sendBroadcast(msgIntent); } } }

    Note 极光推送过来的消息,是封装在 Intent 的里面, 需要 根据, Intent.getAction()来进行取出 各种消息类型, 无论自定义的通知,还是自定义的消息等。

    结束语:

    两者各有优缺, 对于小米或者华为这种手机厂商的 推送, 对各自手机的支持很好,所以选择推送, 自己根据需求来定, 总之开发过程当中,遇到问题, 首先,查看官网给出的开发文档,其次,去他们相应的社区寻求帮助,再者他们会有相应的技术支持!

    转载请注明原文地址: https://ju.6miu.com/read-1310295.html
    最新回复(0)