今天集成极光推送,看了看极光的文档,头都大了,究其原因文档里有这么一句:
简直是胡说八道,我信了他的鬼,还是需要开发者辛辛苦苦的配置.
然后又看了看官方demo,注释少之又少,类乱七八糟,命名也一般般,感觉有很多冗余的代码.
官方文档还好意思说3分钟集成,明明是跑通官方demo,对开发者意义不大,还是先最快速度集成到自己项目才是王道,通过日志分析逻辑才是棒棒.
我一怒之下写了这篇文档,好让后人乘凉.
注:该项目由kotlin语言编写,还在用java开发的土boy请费心理解.
defaultConfig {
...
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
}
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId, //JPush上注册的包名.
JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
]
}
// 极光推送
implementation 'cn.jiguang.sdk:jpush:3.3.4'
implementation 'cn.jiguang.sdk:jcore:2.1.2'
/**
* 自定义JPush接收器
*
*
* 如果不定义这个 Receiver,则:
* 1) 默认用户会打开主界面
* 2) 接收不到自定义消息
*/
class JPushReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
try {
val bundle = intent.extras
Log.d(Tag, "[MyReceiver] onReceive - " + intent.action + ", extras: ")
if (JPushInterface.ACTION_REGISTRATION_ID == intent.action) {
val regId = bundle!!.getString(JPushInterface.EXTRA_REGISTRATION_ID)
Log.d(Tag, "[MyReceiver] 接收Registration Id : " + regId!!)
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED == intent.action) {
Log.d(Tag, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle!!.getString(JPushInterface.EXTRA_MESSAGE)!!)
// processCustomMessage(context, bundle)
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED == intent.action) {
Log.d(Tag, "[MyReceiver] 接收到推送下来的通知")
val notifactionId = bundle!!.getInt(JPushInterface.EXTRA_NOTIFICATION_ID)
Log.d(Tag, "[MyReceiver] 接收到推送下来的通知的ID: $notifactionId")
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED == intent.action) {
Log.d(Tag, "[MyReceiver] 用户点击打开了通知")
// //打开自定义的Activity
// val i = Intent(context, TestActivity::class.java)
// i.putExtras(bundle!!)
// //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
// context.startActivity(i)
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK == intent.action) {
Log.d(
Tag,
"[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle!!.getString(JPushInterface.EXTRA_EXTRA)!!
)
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if (JPushInterface.ACTION_CONNECTION_CHANGE == intent.action) {
val connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false)
Log.w(Tag, "[MyReceiver]" + intent.action + " connected state change to " + connected)
} else {
Log.d(Tag, "[MyReceiver] Unhandled intent - " + intent.action!!)
}
} catch (e: Exception) {
}
}
companion object {
private const val Tag = "===JPushReceiver==="
}
}
class JPushMsgReceiver : JPushMessageReceiver() {
override fun onMessage(context: Context?, customMessage: CustomMessage?) {
Log.e(Tag, "[onMessage] " + customMessage!!)
}
override fun onNotifyMessageOpened(context: Context?, message: NotificationMessage?) {
Log.e(Tag, "[onNotifyMessageOpened] " + message!!)
}
override fun onMultiActionClicked(context: Context, intent: Intent) {
Log.e(Tag, "[onMultiActionClicked] 用户点击了通知栏按钮")
val nActionExtra = intent.extras!!.getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA)
//开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
if (nActionExtra == null) {
Log.d(Tag, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null")
return
}
if (nActionExtra == "my_extra1") {
Log.e(Tag, "[onMultiActionClicked] 用户点击通知栏按钮一")
} else if (nActionExtra == "my_extra2") {
Log.e(Tag, "[onMultiActionClicked] 用户点击通知栏按钮二")
} else if (nActionExtra == "my_extra3") {
Log.e(Tag, "[onMultiActionClicked] 用户点击通知栏按钮三")
} else {
Log.e(Tag, "[onMultiActionClicked] 用户点击通知栏按钮未定义")
}
}
override fun onNotifyMessageArrived(context: Context?, message: NotificationMessage?) {
Log.e(Tag, "[onNotifyMessageArrived] " + message!!)
}
override fun onNotifyMessageDismiss(context: Context?, message: NotificationMessage?) {
Log.e(Tag, "[onNotifyMessageDismiss] " + message!!)
}
override fun onRegister(context: Context?, registrationId: String?) {
Log.e(Tag, "[onRegister] " + registrationId!!)
}
override fun onConnected(context: Context?, isConnected: Boolean) {
Log.e(Tag, "[onConnected] $isConnected")
// 这里可以设置alias
}
override fun onCommandResult(context: Context?, cmdMessage: CmdMessage?) {
Log.e(Tag, "[onCommandResult] " + cmdMessage!!)
}
override fun onTagOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
Log.d(Tag, "onTagOperatorResult!")
super.onTagOperatorResult(context, jPushMessage)
}
override fun onCheckTagOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
Log.d(Tag, "onCheckTagOperatorResult")
super.onCheckTagOperatorResult(context, jPushMessage)
}
override fun onAliasOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
Log.d(Tag, "onAliasOperatorResult==> alias is: ${jPushMessage?.alias}")
super.onAliasOperatorResult(context, jPushMessage)
}
override fun onMobileNumberOperatorResult(context: Context?, jPushMessage: JPushMessage?) {
Log.d(Tag, "onMobileNumberOperatorResult")
super.onMobileNumberOperatorResult(context, jPushMessage)
}
companion object {
private const val Tag = "===JPushMsgReceiver==="
}
}
class JPushService : JCommonService()
配置permission
配置star组件
当然在Activity里初始化也可以
JPushInterface.setDebugMode(true) // 设置开启日志,发布时请关闭日志
JPushInterface.init(this) // 初始化 JPush
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-keep class * extends cn.jpush.android.helpers.JPushMessageReceiver { *; }
-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }