Skip to content

Commit

Permalink
Merge pull request Baseflow#164 from Tong-Huang/add-notification-feature
Browse files Browse the repository at this point in the history
Add notification feature
  • Loading branch information
mvanbeusekom authored Oct 31, 2019
2 parents f24a2aa + 0f630d5 commit 9dfed04
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ We really appreciate contributions via GitHub pull requests. To contribute take
* Apply your changes
* Verify your changes and fix potential warnings/ errors:
* Check formatting: `flutter format .`
* Run static analyses: `flutter analyzes`
* Run static analyses: `flutter analyze`
* Run unit-tests: `flutter test`
* Commit your changes: `git commit -am "<your informative commit message>"`
* Push changes to your fork: `git push origin <name_of_your_branch>`
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ enum PermissionGroup {
/// Android: None
/// iOS: MPMediaLibrary
mediaLibrary
/// Android: Check notification enable
/// iOS: Check and request notification permission
notification
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationManagerCompat;
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -60,7 +61,8 @@ public class PermissionHandlerPlugin implements MethodCallHandler {
private static final int PERMISSION_GROUP_SPEECH = 13;
private static final int PERMISSION_GROUP_STORAGE = 14;
private static final int PERMISSION_GROUP_IGNORE_BATTERY_OPTIMIZATIONS = 15;
private static final int PERMISSION_GROUP_UNKNOWN = 16;
private static final int PERMISSION_GROUP_NOTIFICATION = 16;
private static final int PERMISSION_GROUP_UNKNOWN = 17;

private PermissionHandlerPlugin(Registrar mRegistrar) {
this.mRegistrar = mRegistrar;
Expand All @@ -84,6 +86,7 @@ private PermissionHandlerPlugin(Registrar mRegistrar) {
PERMISSION_GROUP_SPEECH,
PERMISSION_GROUP_STORAGE,
PERMISSION_GROUP_IGNORE_BATTERY_OPTIMIZATIONS,
PERMISSION_GROUP_NOTIFICATION,
PERMISSION_GROUP_UNKNOWN,
})
private @interface PermissionGroup {
Expand Down Expand Up @@ -250,6 +253,16 @@ public void onMethodCall(MethodCall call, Result result) {

@PermissionStatus
private int checkPermissionStatus(@PermissionGroup int permission) {
final Context context = mRegistrar.activity() == null ? mRegistrar.activeContext() : mRegistrar.activity();
if (context == null) {
Log.d(LOG_TAG, "Unable to detect current Activity or App Context.");
return PERMISSION_STATUS_UNKNOWN;
}

if (permission == PERMISSION_GROUP_NOTIFICATION) {
return checkNotificationPermissionStatus(context);
}

final List<String> names = getManifestNames(permission);

if (names == null) {
Expand All @@ -264,12 +277,6 @@ private int checkPermissionStatus(@PermissionGroup int permission) {
return PERMISSION_STATUS_UNKNOWN;
}

final Context context = mRegistrar.activity() == null ? mRegistrar.activeContext() : mRegistrar.activity();
if (context == null) {
Log.d(LOG_TAG, "Unable to detect current Activity or App Context.");
return PERMISSION_STATUS_UNKNOWN;
}

final boolean targetsMOrHigher = context.getApplicationInfo().targetSdkVersion >= VERSION_CODES.M;

for (String name : names) {
Expand Down Expand Up @@ -655,6 +662,7 @@ private List<String> getManifestNames(@PermissionGroup int permission) {
permissionNames.add(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
break;

case PERMISSION_GROUP_NOTIFICATION:
case PERMISSION_GROUP_MEDIA_LIBRARY:
case PERMISSION_GROUP_PHOTOS:
case PERMISSION_GROUP_REMINDERS:
Expand Down Expand Up @@ -726,4 +734,13 @@ private boolean isLocationServiceEnabled(Context context) {
return !TextUtils.isEmpty(locationProviders);
}
}

private int checkNotificationPermissionStatus(Context context) {
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
boolean isGranted = manager.areNotificationsEnabled();
if (isGranted) {
return PERMISSION_STATUS_GRANTED;
}
return PERMISSION_STATUS_DENIED;
}
}
8 changes: 8 additions & 0 deletions example/ios/Runner/Runner.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array/>
</dict>
</plist>
1 change: 1 addition & 0 deletions ios/Classes/PermissionHandlerEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef NS_ENUM(int, PermissionGroup) {
PermissionGroupSpeech,
PermissionGroupStorage,
PermissionGroupIgnoreBatteryOptimizations,
PermissionGroupNotification,
PermissionGroupUnknown,
};

Expand Down
1 change: 1 addition & 0 deletions ios/Classes/PermissionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "SensorPermissionStrategy.h"
#import "SpeechPermissionStrategy.h"
#import "UnknownPermissionStrategy.h"
#import "NotificationPermissionStrategy.h"
#import "PermissionHandlerEnums.h"
#import "Codec.h"

Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/PermissionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ + (id)createPermissionStrategy:(PermissionGroup)permission {
return [SensorPermissionStrategy new];
case PermissionGroupSpeech:
return [SpeechPermissionStrategy new];
case PermissionGroupNotification:
return [NotificationPermissionStrategy new];
default:
return [UnknownPermissionStrategy new];
}
Expand Down
14 changes: 14 additions & 0 deletions ios/Classes/strategies/NotificationPermissionStrategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// NotificationPermissionStrategy.h
// permission_handler
//
// Created by Tong on 2019/10/21.
//

#import <Foundation/Foundation.h>
#import <UserNotifications/UserNotifications.h>
#import "PermissionStrategy.h"

@interface NotificationPermissionStrategy : NSObject <PermissionStrategy>

@end
75 changes: 75 additions & 0 deletions ios/Classes/strategies/NotificationPermissionStrategy.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// NotificationPermissionStrategy.m
// permission_handler
//
// Created by Tong on 2019/10/21.
//

#import "NotificationPermissionStrategy.h"

@implementation NotificationPermissionStrategy

- (PermissionStatus)checkPermissionStatus:(PermissionGroup)permission {
return [NotificationPermissionStrategy permissionStatus];
}

- (ServiceStatus)checkServiceStatus:(PermissionGroup)permission {
return ServiceStatusNotApplicable;
}

- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler {
PermissionStatus status = [self checkPermissionStatus:permission];
if (status != PermissionStatusUnknown) {
completionHandler(status);
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
if(@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions authorizationOptions = 0;
authorizationOptions += UNAuthorizationOptionSound;
authorizationOptions += UNAuthorizationOptionAlert;
authorizationOptions += UNAuthorizationOptionBadge;
[center requestAuthorizationWithOptions:(authorizationOptions) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted || error != nil) {
completionHandler(PermissionStatusDenied);
return;
}
}];
} else {
UIUserNotificationType notificationTypes = 0;
notificationTypes |= UIUserNotificationTypeSound;
notificationTypes |= UIUserNotificationTypeAlert;
notificationTypes |= UIUserNotificationTypeBadge;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
completionHandler(PermissionStatusGranted);
});
}

+ (PermissionStatus)permissionStatus {
__block PermissionStatus permissionStatus = PermissionStatusGranted;
if (@available(iOS 10 , *)) {
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
permissionStatus = PermissionStatusDenied;
} else if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
permissionStatus = PermissionStatusUnknown;
}
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
} else if (@available(iOS 8 , *)) {
UIUserNotificationSettings * setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (setting.types == UIUserNotificationTypeNone) permissionStatus = PermissionStatusDenied;
} else {
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (type == UIUserNotificationTypeNone) permissionStatus = PermissionStatusDenied;
}
return permissionStatus;
}

@end
8 changes: 7 additions & 1 deletion lib/src/permission_enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ class PermissionGroup {
static const PermissionGroup ignoreBatteryOptimizations =
PermissionGroup._(15);

/// Android: Notification
/// iOS: Notification
static const PermissionGroup notification = PermissionGroup._(16);

/// The unknown permission only used for return type, never requested
static const PermissionGroup unknown = PermissionGroup._(16);
static const PermissionGroup unknown = PermissionGroup._(17);

static const List<PermissionGroup> values = <PermissionGroup>[
calendar,
Expand All @@ -169,6 +173,7 @@ class PermissionGroup {
speech,
storage,
ignoreBatteryOptimizations,
notification,
unknown,
];

Expand All @@ -189,6 +194,7 @@ class PermissionGroup {
'speech',
'storage',
'ignoreBatteryOptimizations',
'notification',
'unknown',
];

Expand Down

0 comments on commit 9dfed04

Please sign in to comment.