Android中接入极光推送

    技术2022-07-11  92

    Android中接入极光推送的步骤大体如下:

    1.在主module的build.gradle中添加:

    android { defaultConfig { manifestPlaceholders = [ // app包名 JPUSH_PKGNAME: "com.jiguang.test", // JPush上注册的包名对应的appkey JPUSH_APPKEY : "898f0d6299c1666751ca40a1", // 道通,暂时填写默认值即可 JPUSH_CHANNEL: "developer-default", ] } } dependencies { ...... implementation 'cn.jiguang.sdk:jpush:3.5.8' implementation 'cn.jiguang.sdk:jcore:2.3.0' ...... }

    2.在主module的AndroidManifest.xml中添加:

    <!-- 极光推送 begin --> <!-- Since JCore2.0.0 Required SDK核心功能--> <!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false --> <!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 --> <service android:name=".service.JPushService" 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 --> <!-- category中的name为app的包名 --> <receiver android:name=".receiver.JPushReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <category android:name="com.jiguang.test" /> </intent-filter> </receiver> <!-- 极光推送 end -->

    3.自定义JCommonService类:

    package com.jiguang.test.service; import cn.jpush.android.service.JCommonService; /** * 极光推送服务 */ public class JPushService extends JCommonService { // 空实现即可 }

    4.自定义JPushMessageReceiver类:

    package com.jiguang.test.receiver; import android.content.Context; import android.text.TextUtils; import android.util.Base64; import android.util.Log; import com.haier.uhome.uplustv.data.JPushBean; import com.haier.uhome.uplustv.util.RemindUtil; import com.haier.uhome.uplustv.util.UIUtil; import org.json.JSONException; import org.json.JSONObject; import cn.jpush.android.api.CustomMessage; import cn.jpush.android.api.NotificationMessage; import cn.jpush.android.service.JPushMessageReceiver; /** * 极光推送的广播接收器 */ public class JPushReceiver extends JPushMessageReceiver { private static final String TAG = JPushReceiver.class.getSimpleName(); public JPushReceiver() { } /** * 自定义消息接收 * * @param context 上下文 * @param customMessage 自定义消息 */ @Override public void onMessage(Context context, CustomMessage customMessage) { Log.d(TAG, "onMessage customMessage=" + customMessage); } /** * 通知消息接收 * * @param context 上下文 * @param notificationMessage 通知消息 */ @Override public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) { Log.d(TAG, "onNotifyMessageArrived notificationMessage=" + notificationMessage); } /** * 点击通知消息 * * @param context 上下文 * @param notificationMessage 通知消息 */ @Override public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) { Log.d(TAG, "onNotifyMessageOpened notificationMessage=" + notificationMessage); } /** * 清除通知消息 * * @param context 上下文 * @param notificationMessage 通知消息 */ @Override public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) { Log.d(TAG, "onNotifyMessageDismiss notificationMessage=" + notificationMessage); } /** * 注册成功 * * @param context 上下文 * @param registrationId 注册id */ @Override public void onRegister(Context context, String registrationId) { Log.d(TAG, "onRegister registrationId=" + registrationId); } /** * 长连接状态变化 * * @param context 上下文 * @param isConnected 长连接状态,true表示已连接 */ @Override public void onConnected(Context context, boolean isConnected) { Log.d(TAG, "onConnected isConnected=" + isConnected); } }

    5.在主module的Application类的onCreate方法中添加:

    JPushInterface.setDebugMode(true); // debug开关,传true是打开debug模式 JPushInterface.init(this);

     

    Processed: 0.009, SQL: 9