|
| 1 | +/*************************************************************************/ |
| 2 | +/* godot_user_notification_delegate.m */ |
| 3 | +/*************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/*************************************************************************/ |
| 8 | +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ |
| 9 | +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/*************************************************************************/ |
| 30 | + |
| 31 | +#import "godot_user_notification_delegate.h" |
| 32 | + |
| 33 | +@interface GodotUserNotificationDelegate () |
| 34 | + |
| 35 | +@end |
| 36 | + |
| 37 | +@implementation GodotUserNotificationDelegate |
| 38 | + |
| 39 | +static NSMutableArray<UserNotificationService *> *services = nil; |
| 40 | + |
| 41 | ++ (NSArray<UserNotificationService *> *)services { |
| 42 | + return services; |
| 43 | +} |
| 44 | + |
| 45 | ++ (void)load { |
| 46 | + services = [NSMutableArray new]; |
| 47 | +} |
| 48 | + |
| 49 | ++ (instancetype)shared { |
| 50 | + static GodotUserNotificationDelegate *sharedInstance = nil; |
| 51 | + static dispatch_once_t onceToken; |
| 52 | + dispatch_once(&onceToken, ^{ |
| 53 | + sharedInstance = [[GodotUserNotificationDelegate alloc] init]; |
| 54 | + }); |
| 55 | + return sharedInstance; |
| 56 | +} |
| 57 | + |
| 58 | ++ (void)addService:(UserNotificationService *)service { |
| 59 | + if (!services || !service) { |
| 60 | + return; |
| 61 | + } |
| 62 | + [services addObject:service]; |
| 63 | +} |
| 64 | + |
| 65 | +// MARK: Delegate |
| 66 | + |
| 67 | +// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user. |
| 68 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { |
| 69 | + for (UserNotificationService *service in services) { |
| 70 | + if (![service respondsToSelector:_cmd]) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + [service userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler]; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:. |
| 79 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { |
| 80 | + for (UserNotificationService *service in services) { |
| 81 | + if (![service respondsToSelector:_cmd]) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + [service userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler]; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// The method will be called on the delegate when the application is launched in response to the user's request to view in-app notification settings. |
| 90 | +// Add UNAuthorizationOptionProvidesAppNotificationSettings as an option in requestAuthorizationWithOptions:completionHandler: to add a button to inline notification settings view and the notification settings view in Settings. |
| 91 | +// The notification will be nil when opened from Settings. |
| 92 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification __API_AVAILABLE(macos(10.14), ios(12.0))__API_UNAVAILABLE(watchos, tvos) { |
| 93 | + for (UserNotificationService *service in services) { |
| 94 | + if (![service respondsToSelector:_cmd]) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + [service userNotificationCenter:center openSettingsForNotification:notification]; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +@end |
0 commit comments