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中添加:
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);