顾海波

【需求】加入权限处理

... ... @@ -2,6 +2,7 @@ package com.ewin.flutter_push
import androidx.annotation.NonNull
import android.content.Context
import androidx.core.app.NotificationManagerCompat
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
... ... @@ -18,6 +19,14 @@ import com.ewin.flutter_push.enums.MixListenerTypeEnum
import com.ewin.flutter_push.utils.CommonUtil
import com.alibaba.fastjson.JSON
import com.mixpush.core.MixPushMessage
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
/** FlutterPushPlugin */
class FlutterPushPlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
... ... @@ -66,6 +75,18 @@ class FlutterPushPlugin: FlutterPlugin, MethodCallHandler {
}
})
result.success(true)
} else if(call.method == "checkPermission") {
result.success(NotificationManagerCompat.from(context).areNotificationsEnabled())
} else if(call.method == "getPermission") {
CommonUtil.runMainThread {
val intent = Intent().apply {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
}
result.success(true)
} else {
result.notImplemented()
}
... ...
... ... @@ -3,9 +3,11 @@ import 'flutter_push_platform_interface.dart';
import 'mix_push_plugin_listener.dart';
class FlutterPush {
Future<String?> getPlatformVersion() {
return FlutterPushPlatform.instance.getPlatformVersion();
}
Future<void> init({
String? defaultPlatform,
String? appId,
... ... @@ -13,6 +15,15 @@ class FlutterPush {
}) {
return FlutterPushPlatform.instance.init(defaultPlatform: defaultPlatform, appId: appId, appKey: appKey);
}
Future<bool> checkPermission() {
return FlutterPushPlatform.instance.checkPermission();
}
Future<bool> getPermission() {
return FlutterPushPlatform.instance.getPermission();
}
void addListener(ListenerValue func) {
FlutterPushPlatform.instance.addListener(func);
}
... ...
... ... @@ -33,6 +33,14 @@ class MethodChannelFlutterPush extends FlutterPushPlatform {
});
}
Future<bool> checkPermission() async {
return await methodChannel.invokeMethod('checkPermission');
}
Future<bool> getPermission() async {
return await methodChannel.invokeMethod('getPermission');
}
/// 添加消息监听
void addListener(ListenerValue func) {
listener ??= XiaoMiPushPluginListener(methodChannel);
... ...
... ... @@ -41,6 +41,14 @@ abstract class FlutterPushPlatform extends PlatformInterface {
throw UnimplementedError('platformVersion() has not been implemented.');
}
Future<bool> checkPermission() async {
throw UnimplementedError('platformVersion() has not been implemented.');
}
Future<bool> getPermission() async {
throw UnimplementedError('platformVersion() has not been implemented.');
}
/// 添加消息监听
void addListener(ListenerValue func) {
throw UnimplementedError('platformVersion() has not been implemented.');
... ...
... ... @@ -37,7 +37,7 @@ export default class PushPlugin implements FlutterPlugin {
Log.d(TAG, 'onAttachedToEngine connectivity plugin')
this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), "ewin:flutter_push", StandardMethodCodec.INSTANCE);
let methodChannelHandler = new PushPluginMethodChannelHandler(this.methodChannel);
let methodChannelHandler = new PushPluginMethodChannelHandler(this.methodChannel,binding.getApplicationContext());
this.methodChannel.setMethodCallHandler(methodChannelHandler);
this.pluginBing = binding;
}
... ...
... ... @@ -20,23 +20,33 @@ import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
import { MethodChannel } from '@ohos/flutter_ohos';
import { notificationManager } from '@kit.NotificationKit';
import { common } from '@kit.AbilityKit';
const TAG: string = "PushPluginMethodChannelHandler";
export default class PushPluginMethodChannelHandler implements MethodCallHandler {
private methodChannel: MethodChannel | null = null;
private applicationContext: common.Context | null = null;
constructor(methodChannel:MethodChannel) {
constructor(methodChannel:MethodChannel,applicationContext: common.Context) {
this.methodChannel = methodChannel
this.applicationContext = applicationContext;
}
onMethodCall(call: MethodCall, result: MethodResult): void {
Log.d(TAG, 'onMethodCall step in')
Log.d(TAG, 'onMethodCall step in' + call.method)
switch (call.method) {
case "init":
this.init(result);
break;
case "getRegId":
this.init(result);
break;
case "checkPermission":
this.checkPermission(result);
break;
case "getPermission":
this.getPermission(result);
break;
default:
result.notImplemented();
... ... @@ -44,6 +54,34 @@ export default class PushPluginMethodChannelHandler implements MethodCallHandler
}
}
async checkPermission(result: MethodResult): Promise<void> {
notificationManager.isNotificationEnabled().then((data: boolean) => {
result.success(data)
}).catch((err : BusinessError) => {
result.error(`${err.code}`,`${err.message}`,"")
});
}
async getPermission(result: MethodResult): Promise<void> {
let context = getContext(this.applicationContext) as common.UIAbilityContext;
notificationManager.isNotificationEnabled().then((data: boolean) => {
if(!data){
notificationManager.requestEnableNotification(context).then(() => {
result.success(true)
}).catch((err : BusinessError) => {
if(1600004 == err.code){
result.error(`${err.code}`,`${err.message}`,"")
} else {
result.error(`${err.code}`,`${err.message}`,"")
}
});
}
}).catch((err : BusinessError) => {
result.error(`${err.code}`,`${err.message}`,"")
});
}
async init(result: MethodResult): Promise<void> {
try {
const pushToken: string = await pushService.getToken();
... ... @@ -54,11 +92,12 @@ export default class PushPluginMethodChannelHandler implements MethodCallHandler
map.set("type","ReceiveRegisterResult")
map.set("params", JSON.stringify(params))
this.methodChannel?.invokeMethod("onListener",map)
result.success(true)
} catch (err) {
let e: BusinessError = err as BusinessError;
hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', e.code, e.message);
result.error(`${err.code}`,`${err.message}`,"")
}
result.success(true)
}
}
... ...