
本文共 7801 字,大约阅读时间需要 26 分钟。
简述:
概述
集成 JPush Android SDK 到其应用里,JPush Android SDK 创建到 JPush Cloud 的长连接,为 App 提供永远在线的能力。 当开发者想要及时地推送消息到达 App 时,只需要调用 JPush API 推送,或者使用其他方便的智能推送工具,即可轻松与用户交流。
图中红色部分,是 JPush 与 App 开发者的接触点。手机客户端侧,App 需要集成 JPush SDK;服务器端部分,开发者调用 JPush REST API 来进行推送。
使用该sdk的场景:
目前是四大国内Android手机厂商都各自使用对应提供的push sdk,除开这四大手机厂商外的机型,都默认使用的是极光推送。
支持的Android系统版本
1.目前 SDK 只支持 Android 2.3 或以上版本的手机系统。
2.富媒体信息流功能则需 Android 3.0 或以上版本的系统。
集成
有自动集成和手动集成两种,这里主要是介绍自动集成步骤。
jcenter自动集成步骤
1.在 module 的 gradle 中添加依赖和 AndroidManifest 的替换变量。
android {......defaultConfig { applicationId "com.xxx.xxx" //JPush 上注册的包名. ...... ndk { //选择要添加的对应 cpu 类型的 .so 库。 abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a' // 还可以添加 'x86', 'x86_64', 'mips', 'mips64' } manifestPlaceholders = [ JPUSH_PKGNAME : applicationId, JPUSH_APPKEY : "你的 Appkey ", //JPush 上注册的包名对应的 Appkey. JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可. ] ......}......}dependencies {......compile 'cn.jiguang.sdk:jpush:3.8.6' // 此处以JPush 3.8.6 版本为例。compile 'cn.jiguang.sdk:jcore:2.5.5' // 此处以JCore 2.5.5 版本为例。......}
2.添加必要的权限申请
在AndroidManifest.xml中添加以下权限
权限说明
权限 用途
1.You Package.permission.JPUSH_MESSAGE 官方定义的权限,允许应用接收 JPush 内部代码发送的广播消息。
2.RECEIVE_USER_PRESENT 允许应用可以接收点亮屏幕或解锁广播。
3.INTERNET 允许应用可以访问网络。
4.WAKE_LOCK 允许应用在手机屏幕关闭后后台进程仍然运行; 该权限从 JPush 3.1.5 版本开始变为可选权限,在 3.1.5 前的版本为必须权限。
5.READ_PHONE_STATE 允许应用访问手机状态。
6.WRITE_EXTERNAL_STORAGE 允许应用写入外部存储。
7.READ_EXTERNAL_STORAGE 允许应用读取外部存储。
8.WRITE_SETTINGS 允许应用读取系统设置项。 该权限从 JPush 3.3.2 版本开始变为可选权限,在 3.3.2 前版本为必须权限。
9.VIBRATE 允许应用震动。 该权限从 JPush 3.1.5 版本开始变为可选权限,在 3.1.5 前版本为必须权限。
10.MOUNT_UNMOUNT_FILESYSTEMS 允许应用挂载/卸载外部文件系统。
11.ACCESS_NETWORK_STATE 允许应用获取网络信息状态,如当前的网络连接是否有效。
3.配置 AndroidManifest.xml
####集成 JPush Android SDK 的混淆 ####
-dontoptimize-dontpreverify-dontwarn cn.jpush.**-keep class cn.jpush.** { *; }-keep class * extends cn.jpush.android.helpers.JPushMessageReceiver { *; }-dontwarn cn.jiguang.**-keep class cn.jiguang.** { *; }
添加代码
JPush SDK 提供的 API 接口,都主要集中在 cn.jpush.android.api.JPushInterface 类里。
在自定义的Application 中 onCreate 中调用
JPushInterface.setDebugMode(Constants.sIsDebug);JPushInterface.init(this);
代码解读:
1.init(): 初始化 SDK;
2.setDebugMode(): 设置调试模式。(注:该接口需在 init 接口之前调用,避免出现部分日志没打印的情况。)
基础 API 使用注意事项:
- 开发者如果主动调用了 init 方法 或者其它带 context 参数的方法,都会进行 SDK 初始化;
- 考虑 APP 上线合规,开发者必须在 APP 用户同意了隐私政策后,再调用初始化接口使用极光服务;
另外,可以通过一个继承JPushMessageReceiver的类,对推送通知进行自定义处理。
import android.content.Context;import android. content.Intent;import android.os.Bundle;import android.util.Log;import org.json.JSONException;import org.json.JSONObject;import cn.jpush.android.api.CmdMessage;import cn.jpush.android.api.CustomMessage;import cn.jpush.android.api.JPushInterface;import cn.jpush.android.api.JPushMessage;import cn.jpush.android.api.NotificationMessage;import cn.jpush.android.service.JPushMessageReceiver;public class PushMessageReceiver extends JPushMessageReceiver{private static final String TAG = "PushMessageReceiver";@Overridepublic void onMessage(Context context, CustomMessage customMessage) { Log.e(TAG,"[onMessage] "+customMessage); processCustomMessage(context,customMessage);}@Overridepublic void onNotifyMessageOpened(Context context, NotificationMessage message) { Log.e(TAG,"[onNotifyMessageOpened] "+message); try{ //打开自定义的Activity Intent i = new Intent(context, TestActivity.class); Bundle bundle = new Bundle(); bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE,message.notificationTitle); bundle.putString(JPushInterface.EXTRA_ALERT,message.notificationContent); i.putExtras(bundle); //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP ); context.startActivity(i); }catch (Throwable throwable){ }}@Overridepublic void onMultiActionClicked(Context context, Intent intent) { Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮"); String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA); //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。 if(nActionExtra==null){ Log.d(TAG,"ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null"); return; } if (nActionExtra.equals("my_extra1")) { Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一"); } else if (nActionExtra.equals("my_extra2")) { Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二"); } else if (nActionExtra.equals("my_extra3")) { Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三"); } else { Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义"); }}@Overridepublic void onNotifyMessageArrived(Context context, NotificationMessage message) { Log.e(TAG,"[onNotifyMessageArrived] "+message);}@Overridepublic void onNotifyMessageDismiss(Context context, NotificationMessage message) { Log.e(TAG,"[onNotifyMessageDismiss] "+message);}@Overridepublic void onRegister(Context context, String registrationId) { Log.e(TAG,"[onRegister] "+registrationId);}@Overridepublic void onConnected(Context context, boolean isConnected) { Log.e(TAG,"[onConnected] "+isConnected);}@Overridepublic void onCommandResult(Context context, CmdMessage cmdMessage) { Log.e(TAG,"[onCommandResult] "+cmdMessage);}@Overridepublic void onTagOperatorResult(Context context,JPushMessage jPushMessage) { TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage); super.onTagOperatorResult(context, jPushMessage);}@Overridepublic void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){ TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context,jPushMessage); super.onCheckTagOperatorResult(context, jPushMessage);}@Overridepublic void onAliasOperatorResult(Context context, JPushMessage jPushMessage) { TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context,jPushMessage); super.onAliasOperatorResult(context, jPushMessage);}@Overridepublic void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) { TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context,jPushMessage); super.onMobileNumberOperatorResult(context, jPushMessage);}//send msg to MainActivityprivate void processCustomMessage(Context context, CustomMessage customMessage) { if (MainActivity.isForeground) { String message = customMessage.message; String extras = customMessage.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 (extraJson.length() > 0) { msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras); } } catch (JSONException e) { } } LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent); }}@Overridepublic void onNotificationSettingsCheck(Context context, boolean isOn, int source) { super.onNotificationSettingsCheck(context, isOn, source); Log.e(TAG,"[onNotificationSettingsCheck] isOn:"+isOn+",source:"+source);}}
官方文档:
1.
2.
3.
发表评论
最新留言
关于作者
