Skip to content

Commit cdbdca7

Browse files
author
sergeysozinov
committed
Merge branch 'develop' of https://github.com/mindbox-cloud/flutter-sdk into develop
# Conflicts: # .github/workflows/publish.yml # README.md
2 parents 4257baf + 7638de4 commit cdbdca7

File tree

10 files changed

+76
-16
lines changed

10 files changed

+76
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ Reach out to us for further help and we'll be glad to assist.
4444

4545
The library is available as open source under the terms of the [License](https://github.com/mindbox-cloud/android-sdk/blob/develop/LICENSE.md).
4646

47-
For a better understanding of this content, please familiarize yourself with the Mindbox [Flutter SDK](https://developers.mindbox.ru/docs/flutter-sdk-integration) documentation.
47+
For a better understanding of this content, please familiarize yourself with the Mindbox [Flutter SDK](https://developers.mindbox.ru/docs/flutter-sdk-integration) documentation.

mindbox/lib/mindbox.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,20 @@ class Mindbox {
137137
void registerInAppCallback({required List<InAppCallback> callbacks}) {
138138
MindboxPlatform.instance.registerInAppCallbacks(inAppCallbacks: callbacks);
139139
}
140+
141+
/// Updates the notification permission status.
142+
///
143+
/// The [granted] parameter specifies whether the permission for notifications
144+
/// has been granted:
145+
///
146+
/// - `true` indicates that the user has granted permission.
147+
/// - `false` indicates that the user has denied permission.
148+
///
149+
/// Example usage:
150+
/// ```dart
151+
/// Mindbox.instance.updateNotificationPermissionStatus(granted: true);
152+
void updateNotificationPermissionStatus({required bool granted}) {
153+
MindboxPlatform.instance
154+
.updateNotificationPermissionStatus(granted: granted);
155+
}
140156
}

mindbox_android/android/src/main/kotlin/cloud/mindbox/mindbox_android/MindboxAndroidPlugin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class MindboxAndroidPlugin : FlutterPlugin, MethodCallHandler, ActivityAware, Ne
152152
}
153153
}
154154
}
155+
"updateNotificationPermissionStatus" -> {
156+
Mindbox.updateNotificationPermissionStatus(context = context)
157+
}
155158
else -> {
156159
result.notImplemented()
157160
}

mindbox_android/lib/src/mindbox_android_platform.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@ class MindboxAndroidPlatform extends MindboxPlatform {
9595
void registerInAppCallbacks({required List<InAppCallback> inAppCallbacks}) {
9696
_methodHandler.registerInAppCallbacks(callbacks: inAppCallbacks);
9797
}
98+
99+
// Method for to send notification permission status
100+
@override
101+
void updateNotificationPermissionStatus({required bool granted}) {
102+
_methodHandler.updateNotificationPermissionStatus(granted: granted);
103+
}
98104
}

mindbox_ios/ios/Classes/MindboxFlutterAppDelegate.m

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ - (BOOL)application:(UIApplication *)application
1010
// MINDBOX INTEGRATION
1111
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
1212
center.delegate = self;
13-
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
14-
if (!error) {
15-
dispatch_async(dispatch_get_main_queue(), ^{
16-
[[UIApplication sharedApplication] registerForRemoteNotifications];
17-
[[Mindbox shared] notificationsRequestAuthorizationWithGranted:granted];
18-
});
19-
}
20-
else {
21-
NSLog(@"NotificationsRequestAuthorization failed with error: %@", error.localizedDescription);
22-
}
23-
}];
24-
13+
if ([self shouldRegisterForRemoteNotifications]) {
14+
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound |
15+
UNAuthorizationOptionAlert |
16+
UNAuthorizationOptionBadge) completionHandler:^(
17+
BOOL granted, NSError *_Nullable error) {
18+
if (!error) {
19+
dispatch_async(dispatch_get_main_queue(), ^{
20+
[[UIApplication sharedApplication] registerForRemoteNotifications];
21+
[[Mindbox shared] notificationsRequestAuthorizationWithGranted:granted];
22+
});
23+
} else {
24+
NSLog(@"NotificationsRequestAuthorization failed with error: %@",
25+
error.localizedDescription);
26+
}
27+
}];
28+
}
2529
// MINDBOX INTEGRATION
2630
if (@available(iOS 13.0, *)) {
2731
[[Mindbox shared] registerBGTasks];
@@ -61,4 +65,8 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti
6165
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
6266
[[Mindbox shared] application:application performFetchWithCompletionHandler:completionHandler];
6367
}
68+
69+
- (BOOL)shouldRegisterForRemoteNotifications {
70+
return YES;
71+
}
6472
@end

mindbox_ios/ios/Classes/MindboxFlutterAppDelegate.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import Mindbox
44
import MindboxNotifications
55

66
open class MindboxFlutterAppDelegate: FlutterAppDelegate{
7+
8+
open func shouldRegisterForRemoteNotifications() -> Bool {
9+
return true
10+
}
11+
712
open override func application(
813
_ application: UIApplication,
914
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
@@ -12,8 +17,10 @@ open class MindboxFlutterAppDelegate: FlutterAppDelegate{
1217
if #available(iOS 10.0, *) {
1318
UNUserNotificationCenter.current().delegate = self
1419
}
15-
registerForRemoteNotifications()
16-
20+
21+
if shouldRegisterForRemoteNotifications() {
22+
registerForRemoteNotifications()
23+
}
1724
// Регистрация фоновых задач для iOS выше 13
1825
if #available(iOS 13.0, *) {
1926
Mindbox.shared.registerBGTasks()
@@ -45,7 +52,6 @@ open class MindboxFlutterAppDelegate: FlutterAppDelegate{
4552
// MARK: registerForRemoteNotifications
4653
// Функция запроса разрешения на уведомления. В комплишн блоке надо передать статус разрешения в SDK Mindbox
4754
func registerForRemoteNotifications() {
48-
UNUserNotificationCenter.current().delegate = self
4955
DispatchQueue.main.async {
5056
UIApplication.shared.registerForRemoteNotifications()
5157
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in

mindbox_ios/ios/Classes/SwiftMindboxIosPlugin.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public class SwiftMindboxIosPlugin: NSObject, FlutterPlugin {
159159
} else {
160160
Mindbox.logger.log(level: .info, message: "Use default callback")
161161
}
162+
case "updateNotificationPermissionStatus":
163+
guard let granted = call.arguments as? Bool else {
164+
return
165+
}
166+
Mindbox.shared.notificationsRequestAuthorization(granted: granted)
162167
default:
163168
result(FlutterMethodNotImplemented)
164169
}

mindbox_ios/lib/src/mindbox_ios_platform.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@ class MindboxIosPlatform extends MindboxPlatform {
9595
void registerInAppCallbacks({required List<InAppCallback> inAppCallbacks}) {
9696
_methodHandler.registerInAppCallbacks(callbacks: inAppCallbacks);
9797
}
98+
99+
// Method for to send notification permission status
100+
@override
101+
void updateNotificationPermissionStatus({required bool granted}) {
102+
_methodHandler.updateNotificationPermissionStatus(granted: granted);
103+
}
98104
}

mindbox_platform_interface/lib/src/mindbox_platform.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ abstract class MindboxPlatform {
118118
void setLogLevel({required LogLevel logLevel}) =>
119119
throw UnimplementedError(
120120
'setLogLevel() has not been implemented.');
121+
122+
/// Method for sending notification permission status
123+
void updateNotificationPermissionStatus({required bool granted}) =>
124+
throw UnimplementedError(
125+
'updateNotificationPermissionStatus() has not been implemented');
121126
}
122127

123128

mindbox_platform_interface/lib/src/types/mindbox_method_handler.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class MindboxMethodHandler {
110110
await channel.invokeMethod('setLogLevel', logLevel.index);
111111
}
112112

113+
/// Method for sending notification permission status
114+
void updateNotificationPermissionStatus({required bool granted}) async {
115+
await channel.invokeMethod('updateNotificationPermissionStatus');
116+
}
117+
113118
/// Method for registers a list of InAppCallback instances to handle clicks
114119
/// and dismiss in in-apps.
115120
void registerInAppCallbacks({required List<InAppCallback> callbacks}) async {

0 commit comments

Comments
 (0)