提交 fb0adbfc authored 作者: 张国庆's avatar 张国庆

修改 分离注册通道

上级 8cc79dfa
...@@ -29,6 +29,12 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler { ...@@ -29,6 +29,12 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler {
private val inHandler = Handler(Looper.getMainLooper()) private val inHandler = Handler(Looper.getMainLooper())
lateinit var gottenApplication: Context lateinit var gottenApplication: Context
@JvmStatic
fun initPushService(application: Application){
PushServiceFactory.init(application.applicationContext)
val pushService = PushServiceFactory.getCloudPushService()
pushService.setPushIntentService(RammusPushIntentService::class.java)
}
} }
...@@ -37,8 +43,29 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler { ...@@ -37,8 +43,29 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: Result) { override fun onMethodCall(call: MethodCall, result: Result) {
LogUtils.d(call.method) LogUtils.d(call.method)
when (call.method) { when (call.method) {
"register" -> register()
"initPushService" -> initPushService() "initPushService" -> initPushService()
"register" -> register()
"registerXiaomi" -> {
val appId = call.argument<String>("appId")
val appKey = call.argument<String>("appKey")
registerXiaomi(appId, appKey, result)
}
"registerHuaWei" -> {
registerHuaWei(result)
}
"registerOppo" -> {
val appId = call.argument<String>("appId")
val appKey = call.argument<String>("appKey")
registerOppo(appId, appKey, result)
}
"registerVivo" -> {
val appId = call.argument<String>("appId")
val appKey = call.argument<String>("appKey")
registerVivo(appId, appKey, result)
}
"registerHonor" -> {
registerHonor(result)
}
"deviceId" -> result.success(PushServiceFactory.getCloudPushService().deviceId) "deviceId" -> result.success(PushServiceFactory.getCloudPushService().deviceId)
"turnOnPushChannel" -> turnOnPushChannel(result) "turnOnPushChannel" -> turnOnPushChannel(result)
"turnOffPushChannel" -> turnOffPushChannel(result) "turnOffPushChannel" -> turnOffPushChannel(result)
...@@ -61,10 +88,10 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler { ...@@ -61,10 +88,10 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler {
} }
private fun register() { private fun register() {
val pushService = PushServiceFactory.getCloudPushService() val pushService = PushServiceFactory.getCloudPushService()
pushService.register(gottenApplication, object : CommonCallback { pushService.register(gottenApplication, object : CommonCallback {
override fun onSuccess(response: String?) { override fun onSuccess(response: String?) {
LogUtils.d("initCloudChannelResult${response}")
inHandler.postDelayed({ inHandler.postDelayed({
RammusPushHandler.methodChannel?.invokeMethod( RammusPushHandler.methodChannel?.invokeMethod(
"initCloudChannelResult", mapOf( "initCloudChannelResult", mapOf(
...@@ -76,6 +103,7 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler { ...@@ -76,6 +103,7 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler {
} }
override fun onFailed(errorCode: String?, errorMessage: String?) { override fun onFailed(errorCode: String?, errorMessage: String?) {
LogUtils.d("initCloudChannelResult${errorMessage}")
inHandler.postDelayed({ inHandler.postDelayed({
RammusPushHandler.methodChannel?.invokeMethod( RammusPushHandler.methodChannel?.invokeMethod(
"initCloudChannelResult", mapOf( "initCloudChannelResult", mapOf(
...@@ -87,51 +115,63 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler { ...@@ -87,51 +115,63 @@ class RammusPlugin : FlutterPlugin, MethodCallHandler {
}, 2000) }, 2000)
} }
}) })
val appInfo = gottenApplication.packageManager }
.getApplicationInfo(gottenApplication.packageName, PackageManager.GET_META_DATA)
val xiaomiAppId = appInfo.metaData.getFloat("com.xiaomi.push.client.app_id") // 注册小米推送服务
val xiaomiAppKey = appInfo.metaData.getInt("com.xiaomi.push.client.app_key") private fun registerXiaomi(xiaomiAppId: String?, xiaomiAppKey: String?, result: Result) {
LogUtils.d("小米:appId=$xiaomiAppId,appKey=$xiaomiAppKey") if (xiaomiAppId != null && xiaomiAppKey != null) {
if (xiaomiAppKey != 0) {
LogUtils.d("正在注册小米推送服务...") LogUtils.d("正在注册小米推送服务...")
MiPushRegister.register(gottenApplication, xiaomiAppId.toString(), xiaomiAppKey.toString()) val res = MiPushRegister.register(gottenApplication, xiaomiAppId, xiaomiAppKey)
} LogUtils.d("小米推送服务注册结果:$res")
val huaweiAppId = appInfo.metaData.getInt("com.huawei.hms.client.appid") result.success(res)
LogUtils.d("华为:appId=$huaweiAppId")
if (huaweiAppId != 0) {
LogUtils.d("正在注册华为推送服务...")
HuaWeiRegister.register(gottenApplication as Application)
} }
val oppoAppKey = appInfo.metaData.getString("com.oppo.push.client.app_key")
val oppoAppSecret = appInfo.metaData.getString("com.oppo.push.client.app_secret") }
LogUtils.d("oppo:oppoAppKey=$oppoAppKey,oppoAppSecret=$oppoAppSecret")
if ((oppoAppKey != null && oppoAppKey.isNotBlank()) //注册华为推送服务
&& (oppoAppSecret != null && oppoAppSecret.isNotBlank()) private fun registerHuaWei(result: Result) {
) { LogUtils.d("正在注册华为推送服务...")
val res = HuaWeiRegister.register(gottenApplication as Application)
LogUtils.d("华为推送服务注册结果:$res")
result.success(res)
}
//注册oppo推送服务
private fun registerOppo(oppoAppKey: String?, oppoAppSecret: String?, result: Result) {
if (oppoAppKey != null && oppoAppSecret != null) {
LogUtils.d("正在注册Oppo推送服务...") LogUtils.d("正在注册Oppo推送服务...")
OppoRegister.register(gottenApplication, oppoAppKey, oppoAppSecret) val res = OppoRegister.register(gottenApplication, oppoAppKey, oppoAppSecret)
LogUtils.d("Oppo推送服务注册结果:$res")
result.success(res)
} }
val vivoAppId = appInfo.metaData.getInt("com.vivo.push.app_id") }
val vivoApiKey = appInfo.metaData.getString("com.vivo.push.api_key")
LogUtils.d("vivo:vivoAppId=$vivoAppId,vivoApiKey=$vivoApiKey") //注册vivo推送服务
if ((vivoAppId != 0) && (vivoApiKey != null && vivoApiKey.isNotBlank()) private fun registerVivo(vivoAppId: String?, vivoApiKey: String?, result: Result) {
) { if (vivoAppId != null && vivoApiKey != null) {
LogUtils.d("正在注册vivo推送服务...") LogUtils.d("正在注册vivo推送服务...")
VivoRegister.register(gottenApplication) val res = VivoRegister.register(gottenApplication)
} LogUtils.d("vivo推送服务注册结果:$res")
val hihonorId = appInfo.metaData.getInt("com.hihonor.push.app_id") result.success(res)
LogUtils.d("荣耀:hihonorId=$hihonorId")
if (hihonorId != 0) {
LogUtils.d("正在注册荣耀推送服务...")
HonorRegister.register(gottenApplication as Application)
} }
} }
//注册荣耀推送服务
private fun registerHonor(result: Result) {
LogUtils.d("正在注册荣耀推送服务...")
val res = HonorRegister.register(gottenApplication as Application)
LogUtils.d("荣耀推送服务注册结果:$res")
result.success(res)
}
private fun initPushService() { private fun initPushService() {
LogUtils.d("initPushService")
PushServiceFactory.init(gottenApplication) PushServiceFactory.init(gottenApplication)
val pushService = PushServiceFactory.getCloudPushService() val pushService = PushServiceFactory.getCloudPushService()
pushService.setPushIntentService(RammusPushIntentService::class.java) pushService.setPushIntentService(RammusPushIntentService::class.java)
LogUtils.d("initPushService")
} }
......
package com.jarvanmo.rammus_example package com.jarvanmo.rammus_example
import com.jarvanmo.rammus.RammusPlugin
import io.flutter.app.FlutterApplication import io.flutter.app.FlutterApplication
/*** /***
* Created by mo on 2019-06-25 * Created by mo on 2019-06-25
...@@ -9,5 +10,6 @@ import io.flutter.app.FlutterApplication ...@@ -9,5 +10,6 @@ import io.flutter.app.FlutterApplication
class MyApplication:FlutterApplication() { class MyApplication:FlutterApplication() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
RammusPlugin.initPushService(this)
} }
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论