|
| 1 | +/** |
| 2 | + * Modified MIT License |
| 3 | + * |
| 4 | + * Copyright 2023 OneSignal |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * 1. The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + * with services provided by OneSignal. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#import "OSFlutterNotifications.h" |
| 29 | +#import <OneSignalFramework/OneSignalFramework.h> |
| 30 | +#import <OneSignalNotifications/OneSignalNotifications.h> |
| 31 | +#import "OSFlutterCategories.h" |
| 32 | + |
| 33 | +@implementation OSFlutterNotifications |
| 34 | + |
| 35 | ++ (instancetype)sharedInstance { |
| 36 | + static OSFlutterNotifications *sharedInstance = nil; |
| 37 | + static dispatch_once_t onceToken; |
| 38 | + dispatch_once(&onceToken, ^{ |
| 39 | + sharedInstance = [OSFlutterNotifications new]; |
| 40 | + sharedInstance.hasSetNotificationWillShowInForegroundHandler = false; |
| 41 | + sharedInstance.receivedNotificationCache = [NSMutableDictionary new]; |
| 42 | + sharedInstance.notificationCompletionCache = [NSMutableDictionary new]; |
| 43 | + }); |
| 44 | + return sharedInstance; |
| 45 | +} |
| 46 | + |
| 47 | ++ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { |
| 48 | + OSFlutterNotifications *instance = [OSFlutterNotifications new]; |
| 49 | + |
| 50 | + instance.channel = [FlutterMethodChannel |
| 51 | + methodChannelWithName:@"OneSignal#notifications" |
| 52 | + binaryMessenger:[registrar messenger]]; |
| 53 | + |
| 54 | + [registrar addMethodCallDelegate:instance channel:instance.channel]; |
| 55 | + |
| 56 | + [OneSignal.Notifications setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) { |
| 57 | + [OSFlutterNotifications.sharedInstance handleNotificationWillShowInForeground:notification completion:completion]; |
| 58 | + }]; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { |
| 62 | + if ([@"OneSignal#permission" isEqualToString:call.method]) |
| 63 | + result(@([OneSignal.Notifications permission])); |
| 64 | + else if ([@"OneSignal#canRequest" isEqualToString:call.method]) |
| 65 | + result(@([OneSignal.Notifications canRequestPermission])); |
| 66 | + else if ([@"OneSignal#clearAll" isEqualToString:call.method]) |
| 67 | + [self clearAll:call withResult:result]; |
| 68 | + else if ([@"OneSignal#requestPermission" isEqualToString:call.method]) |
| 69 | + [self requestPermission:call withResult:result]; |
| 70 | + else if ([@"OneSignal#registerForProvisionalAuthorization" isEqualToString:call.method]) |
| 71 | + [self registerForProvisionalAuthorization:call withResult:result]; |
| 72 | + else if ([@"OneSignal#addPermissionObserver" isEqualToString:call.method]) |
| 73 | + [self addPermissionObserver:call withResult:result]; |
| 74 | + else if ([@"OneSignal#removePermissionObserver" isEqualToString:call.method]) |
| 75 | + [self removePermissionObserver:call withResult:result]; |
| 76 | + else if ([@"OneSignal#initNotificationWillShowInForegroundHandlerParams" isEqualToString:call.method]) |
| 77 | + [self initNotificationWillShowInForegroundHandlerParams]; |
| 78 | + else if ([@"OneSignal#initNotificationOpenedHandlerParams" isEqualToString:call.method]) |
| 79 | + [self initNotificationOpenedHandlerParams]; |
| 80 | + else |
| 81 | + result(FlutterMethodNotImplemented); |
| 82 | +} |
| 83 | + |
| 84 | +- (void)clearAll:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 85 | + [OneSignal.Notifications clearAll]; |
| 86 | + result(nil); |
| 87 | +} |
| 88 | + |
| 89 | +- (void)requestPermission:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 90 | + BOOL fallbackToSettings = [call.arguments[@"fallbackToSettings"] boolValue]; |
| 91 | + |
| 92 | + [OneSignal.Notifications requestPermission:^(BOOL accepted) { |
| 93 | + result(@(accepted)); |
| 94 | + } fallbackToSettings:fallbackToSettings]; |
| 95 | +} |
| 96 | + |
| 97 | +- (void)registerForProvisionalAuthorization:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 98 | + [OneSignal.Notifications registerForProvisionalAuthorization:^(BOOL accepted) { |
| 99 | + result(@(accepted)); |
| 100 | + }]; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +- (void)onOSPermissionChanged:(OSPermissionState*)state { |
| 106 | + [self.channel invokeMethod:@"OneSignal#OSPermissionChanged" arguments:state.jsonRepresentation]; |
| 107 | +} |
| 108 | + |
| 109 | +- (void)addPermissionObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 110 | + [OneSignal.Notifications addPermissionObserver:self]; |
| 111 | + result(nil); |
| 112 | +} |
| 113 | + |
| 114 | +// TODO: possibly don't need |
| 115 | +- (void)removePermissionObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 116 | + [OneSignal.Notifications removePermissionObserver:self]; |
| 117 | + result(nil); |
| 118 | +} |
| 119 | + |
| 120 | +#pragma mark Received in Foreground Notification |
| 121 | + |
| 122 | +- (void)initNotificationWillShowInForegroundHandlerParams { |
| 123 | + self.hasSetNotificationWillShowInForegroundHandler = YES; |
| 124 | +} |
| 125 | + |
| 126 | +- (void)handleNotificationWillShowInForeground:(OSNotification *)notification completion:(OSNotificationDisplayResponse)completion { |
| 127 | + if (!self.hasSetNotificationWillShowInForegroundHandler) { |
| 128 | + completion(notification); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + self.receivedNotificationCache[notification.notificationId] = notification; |
| 133 | + self.notificationCompletionCache[notification.notificationId] = completion; |
| 134 | + [self.channel invokeMethod:@"OneSignal#handleNotificationWillShowInForeground" arguments:notification.toJson]; |
| 135 | +} |
| 136 | + |
| 137 | +#pragma mark Opened Notification |
| 138 | + |
| 139 | +- (void)initNotificationOpenedHandlerParams { |
| 140 | + [OneSignal.Notifications setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) { |
| 141 | + [OSFlutterNotifications.sharedInstance handleNotificationOpened:result]; |
| 142 | + }]; |
| 143 | +} |
| 144 | + |
| 145 | +- (void)handleNotificationOpened:(OSNotificationOpenedResult *)result { |
| 146 | + [self.channel invokeMethod:@"OneSignal#handleOpenedNotification" arguments:result.toJson]; |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +@end |
0 commit comments