forked from zinwalin/flutter-sotrage-permission-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Baseflow#164 from Tong-Huang/add-notification-feature
Add notification feature
- Loading branch information
Showing
10 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters