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

修改推送插件

上级 b890cc7b
...@@ -10,4 +10,5 @@ build/ ...@@ -10,4 +10,5 @@ build/
*.iml *.iml
*.lock *.lock
\ No newline at end of file /android/gradlew.bat
...@@ -6,3 +6,5 @@ ...@@ -6,3 +6,5 @@
.DS_Store .DS_Store
/build /build
/captures /captures
/gradle/
/gradlew
...@@ -10,29 +10,28 @@ import android.content.Context ...@@ -10,29 +10,28 @@ import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.os.Handler import android.os.Handler
import android.os.Looper
import android.util.Log import android.util.Log
import com.alibaba.sdk.android.push.CommonCallback import com.alibaba.sdk.android.push.CommonCallback
import com.alibaba.sdk.android.push.HonorRegister
import com.alibaba.sdk.android.push.huawei.HuaWeiRegister import com.alibaba.sdk.android.push.huawei.HuaWeiRegister
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory
import com.alibaba.sdk.android.push.register.* import com.alibaba.sdk.android.push.register.*
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
class RammusPlugin(private val registrar: Registrar, private val methodChannel: MethodChannel) : MethodCallHandler {
class RammusPlugin :
FlutterPlugin, MethodCallHandler {
companion object { companion object {
private const val TAG = "RammusPlugin" private const val TAG = "RammusPlugin"
private val inHandler = Handler() private val inHandler = Handler(Looper.getMainLooper())
private var gottenApplication : Application? = null private var gottenApplication : Application? = null
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "com.jarvanmo/rammus")
RammusPushHandler.methodChannel = channel
channel.setMethodCallHandler(RammusPlugin(registrar, channel))
}
@JvmStatic @JvmStatic
fun initPushService(application: Application){ fun initPushService(application: Application){
gottenApplication = application gottenApplication = application
...@@ -41,6 +40,7 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -41,6 +40,7 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
pushService.setPushIntentService(RammusPushIntentService::class.java) pushService.setPushIntentService(RammusPushIntentService::class.java)
} }
} }
private lateinit var channel : MethodChannel
override fun onMethodCall(call: MethodCall, result: Result) { override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) { when (call.method) {
...@@ -69,10 +69,10 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -69,10 +69,10 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
private fun register() { private fun register() {
if (gottenApplication == null) { if (gottenApplication == null) {
Log.w(TAG, "注册推送服务失败,请检查是否在运行本语句前执行了`RammusPlugin.initPushService`.") Log.w(TAG, "注册推送服务失败,请检查是否在运行本语句前执行了`RammusPlugin.initPushService`.")
return; return
} }
val pushService = PushServiceFactory.getCloudPushService() val pushService = PushServiceFactory.getCloudPushService()
pushService.register(gottenApplication!!.applicationContext, object : CommonCallback { pushService.register(gottenApplication, object : CommonCallback {
override fun onSuccess(response: String?) { override fun onSuccess(response: String?) {
inHandler.postDelayed({ inHandler.postDelayed({
RammusPushHandler.methodChannel?.invokeMethod("initCloudChannelResult", mapOf( RammusPushHandler.methodChannel?.invokeMethod("initCloudChannelResult", mapOf(
...@@ -99,26 +99,31 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -99,26 +99,31 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
if ((xiaomiAppId != null && xiaomiAppId.isNotBlank()) if ((xiaomiAppId != null && xiaomiAppId.isNotBlank())
&& (xiaomiAppKey != null && xiaomiAppKey.isNotBlank())){ && (xiaomiAppKey != null && xiaomiAppKey.isNotBlank())){
Log.d(TAG, "正在注册小米推送服务...") Log.d(TAG, "正在注册小米推送服务...")
MiPushRegister.register(gottenApplication!!.applicationContext, xiaomiAppId, xiaomiAppKey) MiPushRegister.register(gottenApplication, xiaomiAppId, xiaomiAppKey)
} }
val huaweiAppId = appInfo.metaData.getString("com.huawei.hms.client.appid") val huaweiAppId = appInfo.metaData.getString("com.huawei.hms.client.appid")
if (huaweiAppId != null && huaweiAppId.toString().isNotBlank()){ if (huaweiAppId != null && huaweiAppId.toString().isNotBlank()){
Log.d(TAG, "正在注册华为推送服务...") Log.d(TAG, "正在注册华为推送服务...")
HuaWeiRegister.register(gottenApplication!!) HuaWeiRegister.register(gottenApplication)
} }
val oppoAppKey = appInfo.metaData.getString("com.oppo.push.client.app_key") val oppoAppKey = appInfo.metaData.getString("com.oppo.push.client.app_key")
val oppoAppSecret = appInfo.metaData.getString("com.oppo.push.client.app_secret") val oppoAppSecret = appInfo.metaData.getString("com.oppo.push.client.app_secret")
if ((oppoAppKey != null && oppoAppKey.isNotBlank()) if ((oppoAppKey != null && oppoAppKey.isNotBlank())
&& (oppoAppSecret != null && oppoAppSecret.isNotBlank())){ && (oppoAppSecret != null && oppoAppSecret.isNotBlank())){
Log.d(TAG, "正在注册Oppo推送服务...") Log.d(TAG, "正在注册Oppo推送服务...")
OppoRegister.register(gottenApplication!!.applicationContext, oppoAppKey, oppoAppSecret) OppoRegister.register(gottenApplication, oppoAppKey, oppoAppSecret)
} }
val vivoAppId = appInfo.metaData.getString("com.vivo.push.app_id") val vivoAppId = appInfo.metaData.getString("com.vivo.push.app_id")
val vivoApiKey = appInfo.metaData.getString("com.vivo.push.api_key") val vivoApiKey = appInfo.metaData.getString("com.vivo.push.api_key")
if ((vivoAppId != null && vivoAppId.isNotBlank()) if ((vivoAppId != null && vivoAppId.isNotBlank())
&& (vivoApiKey != null && vivoApiKey.isNotBlank())){ && (vivoApiKey != null && vivoApiKey.isNotBlank())){
Log.d(TAG, "正在注册Vivo推送服务...") Log.d(TAG, "正在注册Vivo推送服务...")
VivoRegister.register(gottenApplication!!.applicationContext) VivoRegister.register(gottenApplication)
}
val hihonorId = appInfo.metaData.getString("com.hihonor.push.app_id")
if (hihonorId != null && hihonorId.toString().isNotBlank()){
Log.d(TAG, "正在注册荣耀推送服务...")
HonorRegister.register(gottenApplication)
} }
} }
...@@ -431,15 +436,15 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -431,15 +436,15 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
private fun setupNotificationManager(call: MethodCall, result: Result) { private fun setupNotificationManager(call: MethodCall, result: Result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channels = call.arguments as List<Map<String, Any?>> val channels = call.arguments as List<Map<String, Any?>>
val mNotificationManager = registrar.context().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val mNotificationManager = gottenApplication?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationChannels = mutableListOf<NotificationChannel>() val notificationChannels = mutableListOf<NotificationChannel>()
for (channel in channels){ for (channel in channels){
// 通知渠道的id // 通知渠道的id
val id = channel["id"] ?: registrar.context().packageName val id = channel["id"] ?: gottenApplication?.packageName
// 用户可以看到的通知渠道的名字. // 用户可以看到的通知渠道的名字.
val name = channel["name"] ?: registrar.context().packageName val name = channel["name"] ?: gottenApplication?.packageName
// 用户可以看到的通知渠道的描述 // 用户可以看到的通知渠道的描述
val description = channel["description"] ?: registrar.context().packageName val description = channel["description"] ?:gottenApplication?.packageName
val importance = channel["importance"] ?: NotificationManager.IMPORTANCE_DEFAULT val importance = channel["importance"] ?: NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(id as String, name as String, importance as Int) val mChannel = NotificationChannel(id as String, name as String, importance as Int)
// 配置通知渠道的属性 // 配置通知渠道的属性
...@@ -460,7 +465,7 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -460,7 +465,7 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
val appInfo = gottenApplication!!.packageManager val appInfo = gottenApplication!!.packageManager
.getApplicationInfo(gottenApplication!!.packageName, PackageManager.GET_META_DATA) .getApplicationInfo(gottenApplication!!.packageName, PackageManager.GET_META_DATA)
var num = call.argument("num") as Int? ?: 0 val num = call.argument("num") as Int? ?: 0
setHuaWeiApplicationBadgeNumber(num,appInfo) setHuaWeiApplicationBadgeNumber(num,appInfo)
} }
...@@ -471,8 +476,8 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -471,8 +476,8 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
if (huaweiAppId != null && huaweiAppId.toString().isNotBlank()){ if (huaweiAppId != null && huaweiAppId.toString().isNotBlank()){
try { try {
val bundle = Bundle() val bundle = Bundle()
bundle.putString("package", registrar.context().packageName) // com.test.badge is your package name bundle.putString("package", gottenApplication?.packageName) // com.test.badge is your package name
bundle.putString("class", registrar.context().packageName+".MainActivity") // com.test. badge.MainActivity is your apk main activity bundle.putString("class", gottenApplication?.packageName+".MainActivity") // com.test. badge.MainActivity is your apk main activity
bundle.putInt("badgenumber", num) bundle.putInt("badgenumber", num)
gottenApplication!!.contentResolver.call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle) gottenApplication!!.contentResolver.call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle)
...@@ -483,4 +488,14 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel: ...@@ -483,4 +488,14 @@ class RammusPlugin(private val registrar: Registrar, private val methodChannel:
} }
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.jarvanmo/rammus")
channel.setMethodCallHandler(this)
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
} }
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:rammus_example/main.dart';
void main() {
testWidgets('Verify Platform version', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
});
}
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:rammus/rammus.dart';
void main() {
const MethodChannel channel = MethodChannel('rammus');
setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return '42';
});
});
tearDown(() {
channel.setMockMethodCallHandler(null);
});
test('getPlatformVersion', () async {
// expect(await Rammus.platformVersion, '42');
});
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论