官网:https://www.jiguang.cn/ 1. implementation 'cn.jiguang.sdk:jpush:3.5.0' // 此处以JPush 3.4.1 版本为例。 implementation 'cn.jiguang.sdk:jcore:2.2.4' // 此处以JCore 2.2.4 版本为例。
2.application
JPushInterface.setDebugMode(true) JPushInterface.init(this)
3.
/** * @author Jasmine * @date 2019/12/19. * @description */ public class MyJPushReceiver extends JPushMessageReceiver { private String TAG = "MyJPushReceiver"; /** * TODO 连接极光服务器 */ @Override public void onConnected(Context context, boolean b) { super.onConnected(context, b); Log.e(TAG, "onConnected"); String id = (String) SPUtils.Companion.getInstance().getValue(SharedPreferencesFinal.ID,""); // 设置别名 JPushInterface.setAlias(context,3,id); } /** * TODO 注册极光时的回调 */ @Override public void onRegister(Context context, String s) { super.onRegister(context, s); Log.e(TAG, "onRegister" + s); } /** * TODO 注册以及解除注册别名时回调 */ @Override public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) { super.onAliasOperatorResult(context, jPushMessage); Log.e(TAG, jPushMessage.toString()); } /** * TODO 接收到推送下来的通知 * 可以利用附加字段(notificationMessage.notificationExtras)来区别Notication,指定不同的动作,附加字段是个json字符串 * 通知(Notification),指在手机的通知栏(状态栏)上会显示的一条通知信息 */ @Override public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) { super.onNotifyMessageArrived(context, notificationMessage); Log.e(TAG, "onNotifyMessageArrived"+notificationMessage.notificationContent); // 打印 GetOrder.print(notificationMessage.notificationContent); } /** * TODO 打开了通知 * notificationMessage.notificationExtras(附加字段)的内容处理代码 * 比如打开新的Activity, 打开一个网页等.. */ @Override public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) { super.onNotifyMessageOpened(context, notificationMessage); Log.e(TAG, notificationMessage.notificationExtras); } /** * TODO 接收到推送下来的自定义消息 * 自定义消息不是通知,默认不会被SDK展示到通知栏上,极光推送仅负责透传给SDK。其内容和展示形式完全由开发者自己定义。 * 自定义消息主要用于应用的内部业务逻辑和特殊展示需求 */ @Override public void onMessage(Context context, CustomMessage customMessage) { super.onMessage(context, customMessage); Log.e(TAG, "onMessage"+customMessage.message); } }
4.清单文件
<!-- Since JCore2.0.0 Required SDK核心功能--> <!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false --> <!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 --> <service android:name=".jiugang.MyJCoreService" android:enabled="true" android:exported="false" android:process=":pushcore"> <intent-filter> <action android:name="cn.jiguang.user.service.action" /> </intent-filter> </service> <!-- Required since 3.0.7 --> <!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 --> <!-- 3.3.0开始所有事件将通过该类回调 --> <!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter --> <receiver android:name=".jiugang.MyJPushReceiver" android:enabled="true" android:exported="false" > <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <category android:name="com.example.jiguang" /> </intent-filter> </receiver>