Commit 1b40fe53 authored by chengkun's avatar chengkun

add push policy and mipush sdk

parent 9bf2c4da
......@@ -55,7 +55,7 @@ android {
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
api fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
rootProject.ext.dependencies.other.each {
implementation project(it)
......
......@@ -2,4 +2,61 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yidian.shenghuoquan.bpush">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="com.shenghuoquan.business.permission.MIPUSH_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.shenghuoquan.business.permission.MIPUSH_RECEIVE" />
<application>
<receiver
android:name=".push.mipush.receiver.MiPushReceiver"
android:enabled="true"
android:exported="true">
</receiver>
<service
android:name="com.xiaomi.push.service.XMPushService"
android:enabled="true"
android:process=":pushservice" /> <!-- 注:此service必须在3.0.1版本以后(包括3.0.1版本)加入 -->
<service
android:name="com.xiaomi.push.service.XMJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":pushservice" />
<service
android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
android:enabled="true"
android:exported="true" /> <!-- 注:此service必须在2.2.5版本以后(包括2.2.5版本)加入 -->
<service
android:name="com.xiaomi.mipush.sdk.MessageHandleService"
android:enabled="true" />
<receiver
android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver
android:name="com.xiaomi.push.service.receivers.PingReceiver"
android:exported="false"
android:process=":pushservice">
<intent-filter>
<action android:name="com.xiaomi.push.PING_TIMER" />
</intent-filter>
</receiver>
</application>
</manifest>
\ No newline at end of file
package com.yidian.shenghuoquan.bpush.app
import android.content.Context
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.news.util.ProcessUtil
......@@ -10,8 +11,12 @@ import timber.log.Timber
@YacModuleSpec
class BPushApplication: YdBaseApplication() {
companion object {
lateinit var context:Context
}
override fun onCreate() {
super.onCreate()
context = this;
if (ProcessUtil.isMainProcess(this)) {
registerXpage()
initService()
......
package com.yidian.shenghuoquan.bpush.constant
object BrandConstant {
const val XIAOMI = "XIAOMI11"
const val HUAWEI = "HUAWEI"
const val OPPO = "OPPO"
const val VIVO = "VIVO"
const val UMENG = "UMENG"
}
package com.yidian.shenghuoquan.bpush.push
interface PushPolicy {
fun initPushSdk()
fun pubMessage(message:String)
}
package com.yidian.shenghuoquan.bpush.push
class PushSdkManager {
private val pushPolicy:PushPolicy
constructor(pushPolicy: PushPolicy) {
this.pushPolicy = pushPolicy
}
public fun initSpecealSdk() {
pushPolicy.initPushSdk()
}
public fun pushMessage(message:String) {
pushPolicy.pubMessage(message)
}
}
package com.yidian.shenghuoquan.bpush.push.huaweipush
import com.yidian.shenghuoquan.bpush.push.PushPolicy
class HuaWdeiPushPolicy: PushPolicy {
override fun initPushSdk() {
TODO("Not yet implemented")
}
override fun pubMessage(message: String) {
TODO("Not yet implemented")
}
}
package com.yidian.shenghuoquan.bpush.push.mipush
import android.app.ActivityManager
import android.content.Context
import android.os.Process
import com.xiaomi.mipush.sdk.MiPushClient
import com.yidian.shenghuoquan.bpush.app.BPushApplication
import com.yidian.shenghuoquan.bpush.push.PushPolicy
class MiPushPolicy: PushPolicy {
companion object {
const val appId = ""
const val appkey = ""
const val packageName = ""
}
override fun initPushSdk() {
if (shouldInit()) {
MiPushClient.registerPush(BPushApplication.context, appId, appkey)
}
}
override fun pubMessage(message: String) {
TODO("Not yet implemented")
}
private fun shouldInit(): Boolean {
val am = BPushApplication.context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val processInfos = am.runningAppProcesses
val mainProcessName: String = BPushApplication.context.getPackageName()
val myPid = Process.myPid()
for (info in processInfos) {
if (info.pid == myPid && mainProcessName == info.processName) {
return true
}
}
return false
}
}
package com.yidian.shenghuoquan.bpush.push.mipush.receiver
import android.annotation.SuppressLint
import android.content.Context
import android.os.Message
import android.text.TextUtils
import android.util.Log
import com.xiaomi.mipush.sdk.*
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*
class MiPushReceiver : PushMessageReceiver() {
private val TAG: String? = "MiPushReceiver"
private var mRegId: String? = null
private var mTopic: String? = null
private var mAlias: String? = null
private var mAccount: String? = null
private var mStartTime: String? = null
private var mEndTime: String? = null
override fun onReceivePassThroughMessage(context: Context, message: MiPushMessage) {
Timber.tag(TAG).i("onReceivePassThroughMessage is called. $message")
if (!TextUtils.isEmpty(message.topic)) {
mTopic = message.topic
} else if (!TextUtils.isEmpty(message.alias)) {
mAlias = message.alias
}
val msg = Message.obtain()
}
override fun onNotificationMessageClicked(context: Context, message: MiPushMessage) {
Timber.tag(TAG).i("onNotificationMessageClicked is called. $message")
if (!TextUtils.isEmpty(message.topic)) {
mTopic = message.topic
} else if (!TextUtils.isEmpty(message.alias)) {
mAlias = message.alias
}
val msg = Message.obtain()
}
override fun onNotificationMessageArrived(context: Context, message: MiPushMessage) {
Timber.tag(TAG).i("onNotificationMessageArrived is called. $message")
if (!TextUtils.isEmpty(message.topic)) {
mTopic = message.topic
} else if (!TextUtils.isEmpty(message.alias)) {
mAlias = message.alias
}
}
override fun onCommandResult(context: Context, message: MiPushCommandMessage) {
Timber.tag(TAG).i("onCommandResult is called. $message")
val command = message.command
val arguments = message.commandArguments
val cmdArg1 = if (arguments != null && arguments.size > 0) arguments[0] else null
val cmdArg2 = if (arguments != null && arguments.size > 1) arguments[1] else null
val log: String
if (MiPushClient.COMMAND_REGISTER == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mRegId = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_SET_ALIAS == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mAlias = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_UNSET_ALIAS == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mAlias = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_SET_ACCOUNT == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mAccount = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_UNSET_ACCOUNT == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mAccount = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mTopic = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mTopic = cmdArg1
} else {
}
} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mStartTime = cmdArg1
mEndTime = cmdArg2
} else {
}
} else {
log = message.reason
}
}
override fun onReceiveRegisterResult(context: Context, message: MiPushCommandMessage) {
Timber.tag(TAG).i("onReceiveRegisterResult is called. $message")
val command = message.command
val arguments = message.commandArguments
val cmdArg1 = if (arguments != null && arguments.size > 0) arguments[0] else null
val log: String
if (MiPushClient.COMMAND_REGISTER == command) {
if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
mRegId = cmdArg1
} else {
}
} else {
log = message.reason
}
}
@SuppressLint("SimpleDateFormat")
private fun getSimpleDate(): String {
return SimpleDateFormat("MM-dd hh:mm:ss").format(Date())
}
}
package com.yidian.shenghuoquan.bpush.push.oppopush
import com.yidian.shenghuoquan.bpush.push.PushPolicy
class OppoPushPolicy: PushPolicy {
override fun initPushSdk() {
TODO("Not yet implemented")
}
override fun pubMessage(message: String) {
TODO("Not yet implemented")
}
}
package com.yidian.shenghuoquan.bpush.push.umengpush
import com.yidian.shenghuoquan.bpush.push.PushPolicy
class UmengPushPolicy: PushPolicy {
override fun initPushSdk() {
TODO("Not yet implemented")
}
override fun pubMessage(message: String) {
TODO("Not yet implemented")
}
}
package com.yidian.shenghuoquan.bpush.push.vivopush
import com.yidian.shenghuoquan.bpush.push.PushPolicy
class ViVoPushPolicy: PushPolicy {
override fun initPushSdk() {
TODO("Not yet implemented")
}
override fun pubMessage(message: String) {
TODO("Not yet implemented")
}
}
package com.yidian.shenghuoquan.bpush.service
import android.util.Log
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.shenghuoquan.bpush.constant.BrandConstant
import com.yidian.shenghuoquan.bpush.push.*
import com.yidian.shenghuoquan.bpush.push.huaweipush.HuaWdeiPushPolicy
import com.yidian.shenghuoquan.bpush.push.mipush.MiPushPolicy
import com.yidian.shenghuoquan.bpush.push.oppopush.OppoPushPolicy
import com.yidian.shenghuoquan.bpush.push.umengpush.UmengPushPolicy
import com.yidian.shenghuoquan.bpush.push.vivopush.ViVoPushPolicy
import com.yidian.shenghuoquan.bpush.utils.SystemUtil
import com.yidian.xarc.xrouter.ResultResolver
import com.yidian.yac.core.zap.ZapService
import org.json.JSONObject
import timber.log.Timber
class PushService : ZapService() {
private val TAG = "PushService"
private lateinit var resolver: ResultResolver
private lateinit var pushSdkManager: PushSdkManager
override fun onAction(path: String, action: String, params: Any, options: Any, resolver: ResultResolver) {
Log.i("PushService", "aciton is " + action)
Timber.tag("PushService").i("action is " + action)
this.resolver = resolver
params as JSONObject
when(action) {
ZapServiceActionConstants.ActionPushMessage -> pushMessage(params.toString())
ZapServiceActionConstants.ActionInitPushService -> {
when(SystemUtil.getDeviceBrand()) {
BrandConstant.HUAWEI -> {
pushSdkManager = PushSdkManager(HuaWdeiPushPolicy())
}
BrandConstant.OPPO -> {
pushSdkManager = PushSdkManager(OppoPushPolicy())
}
BrandConstant.VIVO -> {
pushSdkManager = PushSdkManager(ViVoPushPolicy())
}
BrandConstant.XIAOMI -> {
pushSdkManager = PushSdkManager(MiPushPolicy())
}
BrandConstant.UMENG -> {
pushSdkManager = PushSdkManager(UmengPushPolicy())
}
else -> {
pushSdkManager = PushSdkManager(UmengPushPolicy())
}
}
pushSdkManager.initSpecealSdk()
}
}
}
......@@ -22,6 +54,7 @@ class PushService : ZapService() {
}
override fun onInit() {
Timber.tag(TAG).i("onInit")
}
private fun pushMessage(message:String) {
......
package com.yidian.shenghuoquan.bpush.utils
import android.os.Build
import java.util.*
/**
* 系统工具类
*/
object SystemUtil {
/**
* 获取当前手机系统语言。
*
* @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
*/
fun getSystemLanguage(): String? {
return Locale.getDefault().getLanguage()
}
/**
* 获取当前系统上的语言列表(Locale列表)
*
* @return 语言列表
*/
fun getSystemLanguageList(): Array<Locale?>? {
return Locale.getAvailableLocales()
}
/**
* 获取当前手机系统版本号
*
* @return 系统版本号
*/
fun getSystemVersion(): String? {
return Build.VERSION.RELEASE
}
/**
* 获取手机型号
*
* @return 手机型号
*/
fun getSystemModel(): String? {
return Build.MODEL
}
/**
* 获取手机厂商
*
* @return 手机厂商
*/
fun getDeviceBrand(): String {
return Build.BRAND
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment