Showing
6 changed files
with
92 additions
and
5 deletions
| @@ -2,6 +2,7 @@ package com.ewin.flutter_push | @@ -2,6 +2,7 @@ package com.ewin.flutter_push | ||
| 2 | 2 | ||
| 3 | import androidx.annotation.NonNull | 3 | import androidx.annotation.NonNull |
| 4 | import android.content.Context | 4 | import android.content.Context |
| 5 | +import androidx.core.app.NotificationManagerCompat | ||
| 5 | 6 | ||
| 6 | import io.flutter.embedding.engine.plugins.FlutterPlugin | 7 | import io.flutter.embedding.engine.plugins.FlutterPlugin |
| 7 | import io.flutter.plugin.common.MethodCall | 8 | import io.flutter.plugin.common.MethodCall |
| @@ -18,6 +19,14 @@ import com.ewin.flutter_push.enums.MixListenerTypeEnum | @@ -18,6 +19,14 @@ import com.ewin.flutter_push.enums.MixListenerTypeEnum | ||
| 18 | import com.ewin.flutter_push.utils.CommonUtil | 19 | import com.ewin.flutter_push.utils.CommonUtil |
| 19 | import com.alibaba.fastjson.JSON | 20 | import com.alibaba.fastjson.JSON |
| 20 | import com.mixpush.core.MixPushMessage | 21 | import com.mixpush.core.MixPushMessage |
| 22 | +import android.content.ComponentName; | ||
| 23 | +import android.content.Intent; | ||
| 24 | +import android.net.Uri; | ||
| 25 | +import android.os.Build; | ||
| 26 | +import android.provider.Settings; | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + | ||
| 21 | /** FlutterPushPlugin */ | 30 | /** FlutterPushPlugin */ |
| 22 | class FlutterPushPlugin: FlutterPlugin, MethodCallHandler { | 31 | class FlutterPushPlugin: FlutterPlugin, MethodCallHandler { |
| 23 | /// The MethodChannel that will the communication between Flutter and native Android | 32 | /// The MethodChannel that will the communication between Flutter and native Android |
| @@ -66,6 +75,18 @@ class FlutterPushPlugin: FlutterPlugin, MethodCallHandler { | @@ -66,6 +75,18 @@ class FlutterPushPlugin: FlutterPlugin, MethodCallHandler { | ||
| 66 | } | 75 | } |
| 67 | }) | 76 | }) |
| 68 | result.success(true) | 77 | result.success(true) |
| 78 | + } else if(call.method == "checkPermission") { | ||
| 79 | + result.success(NotificationManagerCompat.from(context).areNotificationsEnabled()) | ||
| 80 | + } else if(call.method == "getPermission") { | ||
| 81 | + CommonUtil.runMainThread { | ||
| 82 | + val intent = Intent().apply { | ||
| 83 | + action = Settings.ACTION_APP_NOTIFICATION_SETTINGS | ||
| 84 | + putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName) | ||
| 85 | + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
| 86 | + } | ||
| 87 | + context.startActivity(intent) | ||
| 88 | + } | ||
| 89 | + result.success(true) | ||
| 69 | } else { | 90 | } else { |
| 70 | result.notImplemented() | 91 | result.notImplemented() |
| 71 | } | 92 | } |
| @@ -3,9 +3,11 @@ import 'flutter_push_platform_interface.dart'; | @@ -3,9 +3,11 @@ import 'flutter_push_platform_interface.dart'; | ||
| 3 | import 'mix_push_plugin_listener.dart'; | 3 | import 'mix_push_plugin_listener.dart'; |
| 4 | 4 | ||
| 5 | class FlutterPush { | 5 | class FlutterPush { |
| 6 | + | ||
| 6 | Future<String?> getPlatformVersion() { | 7 | Future<String?> getPlatformVersion() { |
| 7 | return FlutterPushPlatform.instance.getPlatformVersion(); | 8 | return FlutterPushPlatform.instance.getPlatformVersion(); |
| 8 | } | 9 | } |
| 10 | + | ||
| 9 | Future<void> init({ | 11 | Future<void> init({ |
| 10 | String? defaultPlatform, | 12 | String? defaultPlatform, |
| 11 | String? appId, | 13 | String? appId, |
| @@ -13,6 +15,15 @@ class FlutterPush { | @@ -13,6 +15,15 @@ class FlutterPush { | ||
| 13 | }) { | 15 | }) { |
| 14 | return FlutterPushPlatform.instance.init(defaultPlatform: defaultPlatform, appId: appId, appKey: appKey); | 16 | return FlutterPushPlatform.instance.init(defaultPlatform: defaultPlatform, appId: appId, appKey: appKey); |
| 15 | } | 17 | } |
| 18 | + | ||
| 19 | + Future<bool> checkPermission() { | ||
| 20 | + return FlutterPushPlatform.instance.checkPermission(); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + Future<bool> getPermission() { | ||
| 24 | + return FlutterPushPlatform.instance.getPermission(); | ||
| 25 | + } | ||
| 26 | + | ||
| 16 | void addListener(ListenerValue func) { | 27 | void addListener(ListenerValue func) { |
| 17 | FlutterPushPlatform.instance.addListener(func); | 28 | FlutterPushPlatform.instance.addListener(func); |
| 18 | } | 29 | } |
| @@ -33,6 +33,14 @@ class MethodChannelFlutterPush extends FlutterPushPlatform { | @@ -33,6 +33,14 @@ class MethodChannelFlutterPush extends FlutterPushPlatform { | ||
| 33 | }); | 33 | }); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | + Future<bool> checkPermission() async { | ||
| 37 | + return await methodChannel.invokeMethod('checkPermission'); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + Future<bool> getPermission() async { | ||
| 41 | + return await methodChannel.invokeMethod('getPermission'); | ||
| 42 | + } | ||
| 43 | + | ||
| 36 | /// 添加消息监听 | 44 | /// 添加消息监听 |
| 37 | void addListener(ListenerValue func) { | 45 | void addListener(ListenerValue func) { |
| 38 | listener ??= XiaoMiPushPluginListener(methodChannel); | 46 | listener ??= XiaoMiPushPluginListener(methodChannel); |
| @@ -41,6 +41,14 @@ abstract class FlutterPushPlatform extends PlatformInterface { | @@ -41,6 +41,14 @@ abstract class FlutterPushPlatform extends PlatformInterface { | ||
| 41 | throw UnimplementedError('platformVersion() has not been implemented.'); | 41 | throw UnimplementedError('platformVersion() has not been implemented.'); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | + Future<bool> checkPermission() async { | ||
| 45 | + throw UnimplementedError('platformVersion() has not been implemented.'); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + Future<bool> getPermission() async { | ||
| 49 | + throw UnimplementedError('platformVersion() has not been implemented.'); | ||
| 50 | + } | ||
| 51 | + | ||
| 44 | /// 添加消息监听 | 52 | /// 添加消息监听 |
| 45 | void addListener(ListenerValue func) { | 53 | void addListener(ListenerValue func) { |
| 46 | throw UnimplementedError('platformVersion() has not been implemented.'); | 54 | throw UnimplementedError('platformVersion() has not been implemented.'); |
| @@ -37,7 +37,7 @@ export default class PushPlugin implements FlutterPlugin { | @@ -37,7 +37,7 @@ export default class PushPlugin implements FlutterPlugin { | ||
| 37 | Log.d(TAG, 'onAttachedToEngine connectivity plugin') | 37 | Log.d(TAG, 'onAttachedToEngine connectivity plugin') |
| 38 | 38 | ||
| 39 | this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), "ewin:flutter_push", StandardMethodCodec.INSTANCE); | 39 | this.methodChannel = new MethodChannel(binding.getBinaryMessenger(), "ewin:flutter_push", StandardMethodCodec.INSTANCE); |
| 40 | - let methodChannelHandler = new PushPluginMethodChannelHandler(this.methodChannel); | 40 | + let methodChannelHandler = new PushPluginMethodChannelHandler(this.methodChannel,binding.getApplicationContext()); |
| 41 | this.methodChannel.setMethodCallHandler(methodChannelHandler); | 41 | this.methodChannel.setMethodCallHandler(methodChannelHandler); |
| 42 | this.pluginBing = binding; | 42 | this.pluginBing = binding; |
| 43 | } | 43 | } |
| @@ -20,23 +20,33 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; | @@ -20,23 +20,33 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; | ||
| 20 | import { BusinessError } from '@kit.BasicServicesKit'; | 20 | import { BusinessError } from '@kit.BasicServicesKit'; |
| 21 | import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; | 21 | import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; |
| 22 | import { MethodChannel } from '@ohos/flutter_ohos'; | 22 | import { MethodChannel } from '@ohos/flutter_ohos'; |
| 23 | - | 23 | +import { notificationManager } from '@kit.NotificationKit'; |
| 24 | +import { common } from '@kit.AbilityKit'; | ||
| 24 | const TAG: string = "PushPluginMethodChannelHandler"; | 25 | const TAG: string = "PushPluginMethodChannelHandler"; |
| 25 | 26 | ||
| 26 | export default class PushPluginMethodChannelHandler implements MethodCallHandler { | 27 | export default class PushPluginMethodChannelHandler implements MethodCallHandler { |
| 27 | private methodChannel: MethodChannel | null = null; | 28 | private methodChannel: MethodChannel | null = null; |
| 29 | + private applicationContext: common.Context | null = null; | ||
| 28 | 30 | ||
| 29 | - constructor(methodChannel:MethodChannel) { | 31 | + constructor(methodChannel:MethodChannel,applicationContext: common.Context) { |
| 30 | this.methodChannel = methodChannel | 32 | this.methodChannel = methodChannel |
| 33 | + this.applicationContext = applicationContext; | ||
| 31 | } | 34 | } |
| 32 | 35 | ||
| 33 | onMethodCall(call: MethodCall, result: MethodResult): void { | 36 | onMethodCall(call: MethodCall, result: MethodResult): void { |
| 34 | - Log.d(TAG, 'onMethodCall step in') | 37 | + Log.d(TAG, 'onMethodCall step in' + call.method) |
| 35 | switch (call.method) { | 38 | switch (call.method) { |
| 36 | case "init": | 39 | case "init": |
| 37 | this.init(result); | 40 | this.init(result); |
| 38 | break; | 41 | break; |
| 39 | case "getRegId": | 42 | case "getRegId": |
| 43 | + this.init(result); | ||
| 44 | + break; | ||
| 45 | + case "checkPermission": | ||
| 46 | + this.checkPermission(result); | ||
| 47 | + break; | ||
| 48 | + case "getPermission": | ||
| 49 | + this.getPermission(result); | ||
| 40 | break; | 50 | break; |
| 41 | default: | 51 | default: |
| 42 | result.notImplemented(); | 52 | result.notImplemented(); |
| @@ -44,6 +54,34 @@ export default class PushPluginMethodChannelHandler implements MethodCallHandler | @@ -44,6 +54,34 @@ export default class PushPluginMethodChannelHandler implements MethodCallHandler | ||
| 44 | } | 54 | } |
| 45 | } | 55 | } |
| 46 | 56 | ||
| 57 | + async checkPermission(result: MethodResult): Promise<void> { | ||
| 58 | + notificationManager.isNotificationEnabled().then((data: boolean) => { | ||
| 59 | + result.success(data) | ||
| 60 | + }).catch((err : BusinessError) => { | ||
| 61 | + result.error(`${err.code}`,`${err.message}`,"") | ||
| 62 | + }); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + async getPermission(result: MethodResult): Promise<void> { | ||
| 66 | + let context = getContext(this.applicationContext) as common.UIAbilityContext; | ||
| 67 | + notificationManager.isNotificationEnabled().then((data: boolean) => { | ||
| 68 | + if(!data){ | ||
| 69 | + notificationManager.requestEnableNotification(context).then(() => { | ||
| 70 | + result.success(true) | ||
| 71 | + }).catch((err : BusinessError) => { | ||
| 72 | + if(1600004 == err.code){ | ||
| 73 | + result.error(`${err.code}`,`${err.message}`,"") | ||
| 74 | + } else { | ||
| 75 | + result.error(`${err.code}`,`${err.message}`,"") | ||
| 76 | + } | ||
| 77 | + }); | ||
| 78 | + } | ||
| 79 | + }).catch((err : BusinessError) => { | ||
| 80 | + result.error(`${err.code}`,`${err.message}`,"") | ||
| 81 | + }); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + | ||
| 47 | async init(result: MethodResult): Promise<void> { | 85 | async init(result: MethodResult): Promise<void> { |
| 48 | try { | 86 | try { |
| 49 | const pushToken: string = await pushService.getToken(); | 87 | const pushToken: string = await pushService.getToken(); |
| @@ -54,11 +92,12 @@ export default class PushPluginMethodChannelHandler implements MethodCallHandler | @@ -54,11 +92,12 @@ export default class PushPluginMethodChannelHandler implements MethodCallHandler | ||
| 54 | map.set("type","ReceiveRegisterResult") | 92 | map.set("type","ReceiveRegisterResult") |
| 55 | map.set("params", JSON.stringify(params)) | 93 | map.set("params", JSON.stringify(params)) |
| 56 | this.methodChannel?.invokeMethod("onListener",map) | 94 | this.methodChannel?.invokeMethod("onListener",map) |
| 95 | + result.success(true) | ||
| 57 | } catch (err) { | 96 | } catch (err) { |
| 58 | let e: BusinessError = err as BusinessError; | 97 | let e: BusinessError = err as BusinessError; |
| 59 | hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', e.code, e.message); | 98 | hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', e.code, e.message); |
| 99 | + result.error(`${err.code}`,`${err.message}`,"") | ||
| 60 | } | 100 | } |
| 61 | - result.success(true) | ||
| 62 | } | 101 | } |
| 63 | } | 102 | } |
| 64 | 103 |
-
Please register or login to post a comment