|
| 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 "OSFlutterDebug.h" |
| 29 | +#import <OneSignalFramework/OneSignalFramework.h> |
| 30 | +#import "OSFlutterCategories.h" |
| 31 | + |
| 32 | +@implementation OSFlutterInAppMessages |
| 33 | + |
| 34 | ++ (instancetype)sharedInstance { |
| 35 | + static OSFlutterInAppMessages *sharedInstance = nil; |
| 36 | + static dispatch_once_t onceToken; |
| 37 | + dispatch_once(&onceToken, ^{ |
| 38 | + sharedInstance = [OSFlutterInAppMessages new]; |
| 39 | + sharedInstance.hasSetInAppMessageClickedHandler = false; |
| 40 | + }); |
| 41 | + return sharedInstance; |
| 42 | +} |
| 43 | + |
| 44 | ++ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { |
| 45 | + OSFlutterInAppMessages *instance = [OSFlutterInAppMessages new]; |
| 46 | + |
| 47 | + instance.channel = [FlutterMethodChannel |
| 48 | + methodChannelWithName:@"OneSignal#inappmessages" |
| 49 | + binaryMessenger:[registrar messenger]]; |
| 50 | + |
| 51 | + [registrar addMethodCallDelegate:instance channel:instance.channel]; |
| 52 | + |
| 53 | + [OneSignal.InAppMessages setLifecycleHandler:^(OSInAppMessageAction *action) { |
| 54 | + [self handleInAppMessageClicked:action]; |
| 55 | + }]; |
| 56 | +} |
| 57 | + |
| 58 | +- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { |
| 59 | + if ([@"OneSignal#addTrigger" isEqualToString:call.method]) { |
| 60 | + [self addTriggers:call withResult:result]; |
| 61 | + } else if ([@"OneSignal#addTriggers" isEqualToString:call.method]) { |
| 62 | + [self addTriggers:call withResult:result]; |
| 63 | + } else if ([@"OneSignal#removeTrigger" isEqualToString:call.method]) { |
| 64 | + [self removeTrigger:call withResult:result]; |
| 65 | + } else if ([@"OneSignal#removeTriggers" isEqualToString:call.method]) { |
| 66 | + [self removeTriggers:call withResult:result]; |
| 67 | + } else if ([@"OneSignal#clearTriggers" isEqualToString:call.method]) { |
| 68 | + result([OneSignal clearTriggers:call.arguments]); |
| 69 | + } else if ([@"OneSignal#paused" isEqualToString:call.method]) { |
| 70 | + [self paused:call withResult:result]; |
| 71 | + } else if ([@"OneSignal#arePaused" isEqualToString:call.method]) { |
| 72 | + result(@([OneSignal paused];)) |
| 73 | + } else if ([@"OneSignal#initInAppMessageClickedHandlerParams" isEqualToString:call.method]) |
| 74 | + [self initInAppMessageClickedHandlerParams];{ |
| 75 | + else |
| 76 | + result(FlutterMethodNotImplemented); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +- (void)addTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 81 | + NSDictionary *triggers = call.arguments; |
| 82 | + [OneSignal.InAppMessages addTriggers:triggers]; |
| 83 | + result(nil); |
| 84 | +} |
| 85 | + |
| 86 | +- (void)removeTrigger:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 87 | + NSString *key = call.arguments; |
| 88 | + [OneSignal.InAppMessages removeTrigger:key]; |
| 89 | + result(nil); |
| 90 | +} |
| 91 | + |
| 92 | +- (void)removeTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 93 | + NSArray *keys = call.arguments; |
| 94 | + [OneSignal.InAppMessages removeTriggersForKeys:keys]; |
| 95 | + result(nil); |
| 96 | +} |
| 97 | + |
| 98 | +- (void)clearTriggers:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 99 | + [OneSignal.InAppMessages clearTriggers]; |
| 100 | + result(nil); |
| 101 | +} |
| 102 | + |
| 103 | +- (void)paused:(FlutterMethodCall *)call withResult:(FlutterResult)result { |
| 104 | + BOOL pause = [call.arguments boolValue]; |
| 105 | + [OneSignal.InAppMessages paused:pause]; |
| 106 | + result(nil); |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +#pragma mark In App Message Click |
| 111 | +- (void)initInAppMessageClickedHandlerParams { |
| 112 | + _hasSetInAppMessageClickedHandler = true; |
| 113 | + |
| 114 | + if (self.inAppMessageClickedResult) { |
| 115 | + [self handleInAppMessageClicked:self.inAppMessageClickedResult]; |
| 116 | + self.inAppMessageClickedResult = nil; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +- (void)handleInAppMessageClicked:(OSInAppMessageAction *)action { |
| 121 | + if (!self.hasSetInAppMessageClickedHandler) { |
| 122 | + _inAppMessageClickedResult = action; |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + [self.channel invokeMethod:@"OneSignal#handleClickedInAppMessage" arguments:action.toJson]; |
| 127 | +} |
| 128 | + |
| 129 | +#pragma mark OSInAppMessageLifeCycleHandler |
| 130 | +- (void)onWillDisplayInAppMessage:(OSInAppMessage *) result { |
| 131 | + [self.channel invokeMethod:@"OneSignal#onWillDisplayInAppMessage" arguments:result.toJson]; |
| 132 | +} |
| 133 | + |
| 134 | +- (void)onDidDisplayInAppMessage:(OSInAppMessage *) result { |
| 135 | + [self.channel invokeMethod:@"OneSignal#onDidDisplayInAppMessage" arguments:result.toJson]; |
| 136 | +} |
| 137 | + |
| 138 | +- (void)onWillDismissInAppMessage:(OSInAppMessage *) result { |
| 139 | + [self.channel invokeMethod:@"OneSignal#onWillDismissInAppMessage" arguments:result.toJson]; |
| 140 | +} |
| 141 | + |
| 142 | +- (void)onDidDismissInAppMessage:(OSInAppMessage *) result { |
| 143 | + [self.channel invokeMethod:@"OneSignal#onDidDismissInAppMessage" arguments:result.toJson]; |
| 144 | +} |
| 145 | + |
| 146 | +@end |
0 commit comments