配置:
第一步:导入依赖
implementation 'cn.jiguang.sdk:jpush:3.8.5' // 此处以JPush 3.8.5 版本为例。
implementation 'cn.jiguang.sdk:jcore:2.5.5' // 此处以JCore 2.5.5 版本为例。
如果报错的话,需要在Project的gradle.properties文件中添加下面的代码:
android.useDeprecatedNdk=true
第二步:添加极光配置
//极光配置
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "97bc2e6e18d546619f759964",//值来自开发者平台取得的AppKey
JPUSH_CHANNEL : "default_developer",
//xiaomi_config_start
XIAOMI_APPID : "MI-小米的APPID",
XIAOMI_APPKEY : "MI-小米的APPKEY",
//xiaomi_config_end
//oppo_config_start
OPPO_APPKEY : "OP-oppo的APPKEY",
OPPO_APPID : "OP-oppo的APPID",
OPPO_APPSECRET: "OP-oppo的APPSECRET",
//oppo_config_end
//vivo_config_start
VIVO_APPKEY : "vivo的APPKEY",
VIVO_APPID : "vivo的APPID",
//vivo_config_end
]
第三步、初始化
新建一个MyApplication类(继承至Application),并在清单文件(AndroidManifest.xml)的application节点添加name属性关联MyApplication。在MyApplication的onCreate方法中加入初始化的代码:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化sdk
JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试
JPushInterface.init(this);
//添加tag标签,发送消息的之后就可以指定tag标签来发送了
//Set<String> set = new HashSet<>();
//set.add("andfixdemo");//名字任意,可多添加几个
//JPushInterface.setTags(this, set, null);//设置标签
}
}
第四步、创建自定义广播接收器
极光推送是需要自定义一个广播器来接收推送消息的。下面是官方demo的MyReceiver。
public class MyBroadcast extends JPushMessageReceiver {
private static final String TAG = "PushMessageReceiver";
@Override
public void onMessage(Context context, CustomMessage customMessage) {
Log.e(TAG,"[onMessage] "+customMessage);
processCustomMessage(context,customMessage);
}
@Override
public void onNotifyMessageOpened(Context context, NotificationMessage message) {
Log.e(TAG,"[onNotifyMessageOpened] "+message);
try{
//打开自定义的Activity
Intent i = new Intent(context, TestJPushActivity.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){
}
}
@Override
public 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] 用户点击通知栏按钮未定义");
}
}
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage message) {
Log.e(TAG,"[onNotifyMessageArrived] "+message);
}
@Override
public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
Log.e(TAG,"[onNotifyMessageDismiss] "+message);
}
@Override
public void onRegister(Context context, String registrationId) {
Log.e(TAG,"[onRegister] "+registrationId);
}
@Override
public void onConnected(Context context, boolean isConnected) {
Log.e(TAG,"[onConnected] "+isConnected);
}
@Override
public void onCommandResult(Context context, CmdMessage cmdMessage) {
Log.e(TAG,"[onCommandResult] "+cmdMessage);
}
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage);
super.onTagOperatorResult(context, jPushMessage);
}
@Override
public void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){
TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context,jPushMessage);
super.onCheckTagOperatorResult(context, jPushMessage);
}
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context,jPushMessage);
super.onAliasOperatorResult(context, jPushMessage);
}
@Override
public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context,jPushMessage);
super.onMobileNumberOperatorResult(context, jPushMessage);
}
//send msg to MainActivity
private void processCustomMessage(Context context, CustomMessage customMessage) {
if (TestJPushActivity.isForeground) {
String message = customMessage.message;
String extras = customMessage.extra;
Intent msgIntent = new Intent(TestJPushActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(TestJPushActivity.KEY_MESSAGE, message);
if (!ExampleUtil.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (extraJson.length() > 0) {
msgIntent.putExtra(TestJPushActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {
}
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
}
}
@Override
public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
super.onNotificationSettingsCheck(context, isOn, source);
Log.e(TAG,"[onNotificationSettingsCheck] isOn:"+isOn+",source:"+source);
}
}
第五步、创建自定义服务类继承JCommonService
public class MyService extends JCommonService {
}
第六步:清单文件配置推送必须组件
<service android:name="xx.xx.XService"
android:enabled="true"
android:exported="false"
android:process=":pushcore">
<intent-filter>
<action android:name="cn.jiguang.user.service.action" />
</intent-filter>
</service>
<receiver
android:name="自定义 Receiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="您应用的包名" />
</intent-filter>
</receiver>
网友评论