|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2020 - Raw Material Software Limited
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 6 End-User License
- Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
-
- End User License Agreement: www.juce.com/juce-6-licence
- Privacy Policy: www.juce.com/juce-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- namespace PushNotificationsDelegateDetails
- {
- //==============================================================================
- using Action = PushNotifications::Settings::Action;
- using Category = PushNotifications::Settings::Category;
-
- void* actionToNSAction (const Action& a, bool iOSEarlierThan10)
- {
- if (iOSEarlierThan10)
- {
- auto action = [[UIMutableUserNotificationAction alloc] init];
-
- action.identifier = juceStringToNS (a.identifier);
- action.title = juceStringToNS (a.title);
- action.behavior = a.style == Action::text ? UIUserNotificationActionBehaviorTextInput
- : UIUserNotificationActionBehaviorDefault;
- action.parameters = varObjectToNSDictionary (a.parameters);
- action.activationMode = a.triggerInBackground ? UIUserNotificationActivationModeBackground
- : UIUserNotificationActivationModeForeground;
- action.destructive = (bool) a.destructive;
-
- [action autorelease];
-
- return action;
- }
- else
- {
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- if (a.style == Action::text)
- {
- return [UNTextInputNotificationAction actionWithIdentifier: juceStringToNS (a.identifier)
- title: juceStringToNS (a.title)
- options: NSUInteger (a.destructive << 1 | (! a.triggerInBackground) << 2)
- textInputButtonTitle: juceStringToNS (a.textInputButtonText)
- textInputPlaceholder: juceStringToNS (a.textInputPlaceholder)];
- }
-
- return [UNNotificationAction actionWithIdentifier: juceStringToNS (a.identifier)
- title: juceStringToNS (a.title)
- options: NSUInteger (a.destructive << 1 | (! a.triggerInBackground) << 2)];
- #else
- return nullptr;
- #endif
- }
- }
-
- void* categoryToNSCategory (const Category& c, bool iOSEarlierThan10)
- {
- if (iOSEarlierThan10)
- {
- auto category = [[UIMutableUserNotificationCategory alloc] init];
- category.identifier = juceStringToNS (c.identifier);
-
- auto actions = [NSMutableArray arrayWithCapacity: (NSUInteger) c.actions.size()];
-
- for (const auto& a : c.actions)
- {
- auto* action = (UIUserNotificationAction*) actionToNSAction (a, iOSEarlierThan10);
- [actions addObject: action];
- }
-
- [category setActions: actions forContext: UIUserNotificationActionContextDefault];
- [category setActions: actions forContext: UIUserNotificationActionContextMinimal];
-
- [category autorelease];
-
- return category;
- }
- else
- {
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- auto actions = [NSMutableArray arrayWithCapacity: (NSUInteger) c.actions.size()];
-
- for (const auto& a : c.actions)
- {
- auto* action = (UNNotificationAction*) actionToNSAction (a, iOSEarlierThan10);
- [actions addObject: action];
- }
-
- return [UNNotificationCategory categoryWithIdentifier: juceStringToNS (c.identifier)
- actions: actions
- intentIdentifiers: @[]
- options: c.sendDismissAction ? UNNotificationCategoryOptionCustomDismissAction : 0];
- #else
- return nullptr;
- #endif
- }
- }
-
- //==============================================================================
- UILocalNotification* juceNotificationToUILocalNotification (const PushNotifications::Notification& n)
- {
- auto notification = [[UILocalNotification alloc] init];
-
- notification.alertTitle = juceStringToNS (n.title);
- notification.alertBody = juceStringToNS (n.body);
- notification.category = juceStringToNS (n.category);
- notification.applicationIconBadgeNumber = n.badgeNumber;
-
- auto triggerTime = Time::getCurrentTime() + RelativeTime (n.triggerIntervalSec);
- notification.fireDate = [NSDate dateWithTimeIntervalSince1970: triggerTime.toMilliseconds() / 1000.];
- notification.userInfo = varObjectToNSDictionary (n.properties);
-
- auto soundToPlayString = n.soundToPlay.toString (true);
-
- if (soundToPlayString == "default_os_sound")
- notification.soundName = UILocalNotificationDefaultSoundName;
- else if (soundToPlayString.isNotEmpty())
- notification.soundName = juceStringToNS (soundToPlayString);
-
- return notification;
- }
-
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- UNNotificationRequest* juceNotificationToUNNotificationRequest (const PushNotifications::Notification& n)
- {
- // content
- auto content = [[UNMutableNotificationContent alloc] init];
-
- content.title = juceStringToNS (n.title);
- content.subtitle = juceStringToNS (n.subtitle);
- content.threadIdentifier = juceStringToNS (n.groupId);
- content.body = juceStringToNS (n.body);
- content.categoryIdentifier = juceStringToNS (n.category);
- content.badge = [NSNumber numberWithInt: n.badgeNumber];
-
- auto soundToPlayString = n.soundToPlay.toString (true);
-
- if (soundToPlayString == "default_os_sound")
- content.sound = [UNNotificationSound defaultSound];
- else if (soundToPlayString.isNotEmpty())
- content.sound = [UNNotificationSound soundNamed: juceStringToNS (soundToPlayString)];
-
- auto* propsDict = (NSMutableDictionary*) varObjectToNSDictionary (n.properties);
- [propsDict setObject: juceStringToNS (soundToPlayString) forKey: nsStringLiteral ("com.juce.soundName")];
- content.userInfo = propsDict;
-
- // trigger
- UNTimeIntervalNotificationTrigger* trigger = nil;
-
- if (std::abs (n.triggerIntervalSec) >= 0.001)
- {
- BOOL shouldRepeat = n.repeat && n.triggerIntervalSec >= 60;
- trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: n.triggerIntervalSec repeats: shouldRepeat];
- }
-
- // request
- // each notification on iOS 10 needs to have an identifier, otherwise it will not show up
- jassert (n.identifier.isNotEmpty());
- UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier: juceStringToNS (n.identifier)
- content: content
- trigger: trigger];
-
- [content autorelease];
-
- return request;
- }
- #endif
-
- String getUserResponseFromNSDictionary (NSDictionary* dictionary)
- {
- if (dictionary == nil || dictionary.count == 0)
- return {};
-
- jassert (dictionary.count == 1);
-
- for (NSString* key in dictionary)
- {
- const auto keyString = nsStringToJuce (key);
-
- id value = dictionary[key];
-
- if ([value isKindOfClass: [NSString class]])
- return nsStringToJuce ((NSString*) value);
- }
-
- jassertfalse;
- return {};
- }
-
- //==============================================================================
- var getNotificationPropertiesFromDictionaryVar (const var& dictionaryVar)
- {
- DynamicObject* dictionaryVarObject = dictionaryVar.getDynamicObject();
-
- if (dictionaryVarObject == nullptr)
- return {};
-
- const auto& properties = dictionaryVarObject->getProperties();
-
- DynamicObject::Ptr propsVarObject = new DynamicObject();
-
- for (int i = 0; i < properties.size(); ++i)
- {
- auto propertyName = properties.getName (i).toString();
-
- if (propertyName == "aps")
- continue;
-
- propsVarObject->setProperty (propertyName, properties.getValueAt (i));
- }
-
- return var (propsVarObject.get());
- }
-
- //==============================================================================
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- double getIntervalSecFromUNNotificationTrigger (UNNotificationTrigger* t)
- {
- if (t != nil)
- {
- if ([t isKindOfClass: [UNTimeIntervalNotificationTrigger class]])
- {
- auto* trigger = (UNTimeIntervalNotificationTrigger*) t;
- return trigger.timeInterval;
- }
- else if ([t isKindOfClass: [UNCalendarNotificationTrigger class]])
- {
- auto* trigger = (UNCalendarNotificationTrigger*) t;
- NSDate* date = [trigger.dateComponents date];
- NSDate* dateNow = [NSDate date];
- return [dateNow timeIntervalSinceDate: date];
- }
- }
-
- return 0.;
- }
-
- PushNotifications::Notification unNotificationRequestToJuceNotification (UNNotificationRequest* r)
- {
- PushNotifications::Notification n;
-
- n.identifier = nsStringToJuce (r.identifier);
- n.title = nsStringToJuce (r.content.title);
- n.subtitle = nsStringToJuce (r.content.subtitle);
- n.body = nsStringToJuce (r.content.body);
- n.groupId = nsStringToJuce (r.content.threadIdentifier);
- n.category = nsStringToJuce (r.content.categoryIdentifier);
- n.badgeNumber = r.content.badge.intValue;
-
- auto userInfoVar = nsDictionaryToVar (r.content.userInfo);
-
- if (auto* object = userInfoVar.getDynamicObject())
- {
- static const Identifier soundName ("com.juce.soundName");
- n.soundToPlay = URL (object->getProperty (soundName).toString());
- object->removeProperty (soundName);
- }
-
- n.properties = userInfoVar;
-
- n.triggerIntervalSec = getIntervalSecFromUNNotificationTrigger (r.trigger);
- n.repeat = r.trigger != nil && r.trigger.repeats;
-
- return n;
- }
-
- PushNotifications::Notification unNotificationToJuceNotification (UNNotification* n)
- {
- return unNotificationRequestToJuceNotification (n.request);
- }
- #endif
-
- PushNotifications::Notification uiLocalNotificationToJuceNotification (UILocalNotification* n)
- {
- PushNotifications::Notification notif;
-
- notif.title = nsStringToJuce (n.alertTitle);
- notif.body = nsStringToJuce (n.alertBody);
-
- if (n.fireDate != nil)
- {
- NSDate* dateNow = [NSDate date];
- NSDate* fireDate = n.fireDate;
-
- notif.triggerIntervalSec = [dateNow timeIntervalSinceDate: fireDate];
- }
-
- notif.soundToPlay = URL (nsStringToJuce (n.soundName));
- notif.badgeNumber = (int) n.applicationIconBadgeNumber;
- notif.category = nsStringToJuce (n.category);
- notif.properties = nsDictionaryToVar (n.userInfo);
-
- return notif;
- }
-
- Action uiUserNotificationActionToAction (UIUserNotificationAction* a)
- {
- Action action;
-
- action.identifier = nsStringToJuce (a.identifier);
- action.title = nsStringToJuce (a.title);
- action.style = a.behavior == UIUserNotificationActionBehaviorTextInput
- ? Action::text
- : Action::button;
-
- action.triggerInBackground = a.activationMode == UIUserNotificationActivationModeBackground;
- action.destructive = a.destructive;
- action.parameters = nsDictionaryToVar (a.parameters);
-
- return action;
- }
-
- Category uiUserNotificationCategoryToCategory (UIUserNotificationCategory* c)
- {
- Category category;
- category.identifier = nsStringToJuce (c.identifier);
-
- for (UIUserNotificationAction* a in [c actionsForContext: UIUserNotificationActionContextDefault])
- category.actions.add (uiUserNotificationActionToAction (a));
-
- return category;
- }
-
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- Action unNotificationActionToAction (UNNotificationAction* a)
- {
- Action action;
-
- action.identifier = nsStringToJuce (a.identifier);
- action.title = nsStringToJuce (a.title);
- action.triggerInBackground = ! (a.options & UNNotificationActionOptionForeground);
- action.destructive = a.options & UNNotificationActionOptionDestructive;
-
- if ([a isKindOfClass: [UNTextInputNotificationAction class]])
- {
- auto* textAction = (UNTextInputNotificationAction*)a;
-
- action.style = Action::text;
- action.textInputButtonText = nsStringToJuce (textAction.textInputButtonTitle);
- action.textInputPlaceholder = nsStringToJuce (textAction.textInputPlaceholder);
- }
- else
- {
- action.style = Action::button;
- }
-
- return action;
- }
-
- Category unNotificationCategoryToCategory (UNNotificationCategory* c)
- {
- Category category;
-
- category.identifier = nsStringToJuce (c.identifier);
- category.sendDismissAction = c.options & UNNotificationCategoryOptionCustomDismissAction;
-
- for (UNNotificationAction* a in c.actions)
- category.actions.add (unNotificationActionToAction (a));
-
- return category;
- }
- #endif
-
- PushNotifications::Notification nsDictionaryToJuceNotification (NSDictionary* dictionary)
- {
- const var dictionaryVar = nsDictionaryToVar (dictionary);
-
- const var apsVar = dictionaryVar.getProperty ("aps", {});
-
- if (! apsVar.isObject())
- return {};
-
- var alertVar = apsVar.getProperty ("alert", {});
-
- const var titleVar = alertVar.getProperty ("title", {});
- const var bodyVar = alertVar.isObject() ? alertVar.getProperty ("body", {}) : alertVar;
-
- const var categoryVar = apsVar.getProperty ("category", {});
- const var soundVar = apsVar.getProperty ("sound", {});
- const var badgeVar = apsVar.getProperty ("badge", {});
- const var threadIdVar = apsVar.getProperty ("thread-id", {});
-
- PushNotifications::Notification notification;
-
- notification.title = titleVar .toString();
- notification.body = bodyVar .toString();
- notification.groupId = threadIdVar.toString();
- notification.category = categoryVar.toString();
- notification.soundToPlay = URL (soundVar.toString());
- notification.badgeNumber = (int) badgeVar;
- notification.properties = getNotificationPropertiesFromDictionaryVar (dictionaryVar);
-
- return notification;
- }
- }
-
- //==============================================================================
- struct PushNotificationsDelegate
- {
- PushNotificationsDelegate() : delegate ([getClass().createInstance() init])
- {
- Class::setThis (delegate.get(), this);
-
- id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
-
- SEL selector = NSSelectorFromString (@"setPushNotificationsDelegateToUse:");
-
- if ([appDelegate respondsToSelector: selector])
- [appDelegate performSelector: selector withObject: delegate.get()];
- }
-
- virtual ~PushNotificationsDelegate() {}
-
- virtual void didRegisterUserNotificationSettings (UIUserNotificationSettings* notificationSettings) = 0;
-
- virtual void registeredForRemoteNotifications (NSData* deviceToken) = 0;
-
- virtual void failedToRegisterForRemoteNotifications (NSError* error) = 0;
-
- virtual void didReceiveRemoteNotification (NSDictionary* userInfo) = 0;
-
- virtual void didReceiveRemoteNotificationFetchCompletionHandler (NSDictionary* userInfo,
- void (^completionHandler)(UIBackgroundFetchResult result)) = 0;
-
- virtual void handleActionForRemoteNotificationCompletionHandler (NSString* actionIdentifier,
- NSDictionary* userInfo,
- NSDictionary* responseInfo,
- void (^completionHandler)()) = 0;
-
- virtual void didReceiveLocalNotification (UILocalNotification* notification) = 0;
-
- virtual void handleActionForLocalNotificationCompletionHandler (NSString* actionIdentifier,
- UILocalNotification* notification,
- void (^completionHandler)()) = 0;
-
- virtual void handleActionForLocalNotificationWithResponseCompletionHandler (NSString* actionIdentifier,
- UILocalNotification* notification,
- NSDictionary* responseInfo,
- void (^completionHandler)()) = 0;
-
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- virtual void willPresentNotificationWithCompletionHandler (UNNotification* notification,
- void (^completionHandler)(UNNotificationPresentationOptions options)) = 0;
-
- virtual void didReceiveNotificationResponseWithCompletionHandler (UNNotificationResponse* response,
- void (^completionHandler)()) = 0;
- #endif
-
- protected:
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- std::unique_ptr<NSObject<UIApplicationDelegate, UNUserNotificationCenterDelegate>, NSObjectDeleter> delegate;
- #else
- std::unique_ptr<NSObject<UIApplicationDelegate>, NSObjectDeleter> delegate;
- #endif
-
- private:
- //==============================================================================
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- struct Class : public ObjCClass<NSObject<UIApplicationDelegate, UNUserNotificationCenterDelegate>>
- {
- Class() : ObjCClass<NSObject<UIApplicationDelegate, UNUserNotificationCenterDelegate>> ("JucePushNotificationsDelegate_")
- #else
- struct Class : public ObjCClass<NSObject<UIApplicationDelegate>>
- {
- Class() : ObjCClass<NSObject<UIApplicationDelegate>> ("JucePushNotificationsDelegate_")
- #endif
- {
- addIvar<PushNotificationsDelegate*> ("self");
-
- addMethod (@selector (application:didRegisterUserNotificationSettings:), didRegisterUserNotificationSettings, "v@:@@");
- addMethod (@selector (application:didRegisterForRemoteNotificationsWithDeviceToken:), registeredForRemoteNotifications, "v@:@@");
- addMethod (@selector (application:didFailToRegisterForRemoteNotificationsWithError:), failedToRegisterForRemoteNotifications, "v@:@@");
- addMethod (@selector (application:didReceiveRemoteNotification:), didReceiveRemoteNotification, "v@:@@");
- addMethod (@selector (application:didReceiveRemoteNotification:fetchCompletionHandler:), didReceiveRemoteNotificationFetchCompletionHandler, "v@:@@@");
- addMethod (@selector (application:handleActionWithIdentifier:forRemoteNotification:withResponseInfo:completionHandler:), handleActionForRemoteNotificationCompletionHandler, "v@:@@@@@");
- addMethod (@selector (application:didReceiveLocalNotification:), didReceiveLocalNotification, "v@:@@");
- addMethod (@selector (application:handleActionWithIdentifier:forLocalNotification:completionHandler:), handleActionForLocalNotificationCompletionHandler, "v@:@@@@");
- addMethod (@selector (application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:), handleActionForLocalNotificationWithResponseCompletionHandler, "v@:@@@@@");
-
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- addMethod (@selector (userNotificationCenter:willPresentNotification:withCompletionHandler:), willPresentNotificationWithCompletionHandler, "v@:@@@");
- addMethod (@selector (userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:), didReceiveNotificationResponseWithCompletionHandler, "v@:@@@");
- #endif
-
- registerClass();
- }
-
- //==============================================================================
- static PushNotificationsDelegate& getThis (id self) { return *getIvar<PushNotificationsDelegate*> (self, "self"); }
- static void setThis (id self, PushNotificationsDelegate* d) { object_setInstanceVariable (self, "self", d); }
-
- //==============================================================================
- static void didRegisterUserNotificationSettings (id self, SEL, UIApplication*,
- UIUserNotificationSettings* settings) { getThis (self).didRegisterUserNotificationSettings (settings); }
- static void registeredForRemoteNotifications (id self, SEL, UIApplication*,
- NSData* deviceToken) { getThis (self).registeredForRemoteNotifications (deviceToken); }
-
- static void failedToRegisterForRemoteNotifications (id self, SEL, UIApplication*,
- NSError* error) { getThis (self).failedToRegisterForRemoteNotifications (error); }
-
- static void didReceiveRemoteNotification (id self, SEL, UIApplication*,
- NSDictionary* userInfo) { getThis (self).didReceiveRemoteNotification (userInfo); }
-
- static void didReceiveRemoteNotificationFetchCompletionHandler (id self, SEL, UIApplication*,
- NSDictionary* userInfo,
- void (^completionHandler)(UIBackgroundFetchResult result)) { getThis (self).didReceiveRemoteNotificationFetchCompletionHandler (userInfo, completionHandler); }
-
- static void handleActionForRemoteNotificationCompletionHandler (id self, SEL, UIApplication*,
- NSString* actionIdentifier,
- NSDictionary* userInfo,
- NSDictionary* responseInfo,
- void (^completionHandler)()) { getThis (self).handleActionForRemoteNotificationCompletionHandler (actionIdentifier, userInfo, responseInfo, completionHandler); }
-
- static void didReceiveLocalNotification (id self, SEL, UIApplication*,
- UILocalNotification* notification) { getThis (self).didReceiveLocalNotification (notification); }
-
- static void handleActionForLocalNotificationCompletionHandler (id self, SEL, UIApplication*,
- NSString* actionIdentifier,
- UILocalNotification* notification,
- void (^completionHandler)()) { getThis (self).handleActionForLocalNotificationCompletionHandler (actionIdentifier, notification, completionHandler); }
-
- static void handleActionForLocalNotificationWithResponseCompletionHandler (id self, SEL, UIApplication*,
- NSString* actionIdentifier,
- UILocalNotification* notification,
- NSDictionary* responseInfo,
- void (^completionHandler)()) { getThis (self). handleActionForLocalNotificationWithResponseCompletionHandler (actionIdentifier, notification, responseInfo, completionHandler); }
-
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- static void willPresentNotificationWithCompletionHandler (id self, SEL, UNUserNotificationCenter*,
- UNNotification* notification,
- void (^completionHandler)(UNNotificationPresentationOptions options)) { getThis (self).willPresentNotificationWithCompletionHandler (notification, completionHandler); }
-
- static void didReceiveNotificationResponseWithCompletionHandler (id self, SEL, UNUserNotificationCenter*,
- UNNotificationResponse* response,
- void (^completionHandler)()) { getThis (self).didReceiveNotificationResponseWithCompletionHandler (response, completionHandler); }
- #endif
- };
-
- //==============================================================================
- static Class& getClass()
- {
- static Class c;
- return c;
- }
- };
-
- //==============================================================================
- bool PushNotifications::Notification::isValid() const noexcept
- {
- const bool iOSEarlierThan10 = std::floor (NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max;
-
- if (iOSEarlierThan10)
- return title.isNotEmpty() && body.isNotEmpty() && category.isNotEmpty();
-
- return title.isNotEmpty() && body.isNotEmpty() && identifier.isNotEmpty() && category.isNotEmpty();
- }
-
- //==============================================================================
- struct PushNotifications::Pimpl : private PushNotificationsDelegate
- {
- Pimpl (PushNotifications& p)
- : owner (p)
- {
- }
-
- void requestPermissionsWithSettings (const PushNotifications::Settings& settingsToUse)
- {
- settings = settingsToUse;
-
- auto categories = [NSMutableSet setWithCapacity: (NSUInteger) settings.categories.size()];
-
- if (iOSEarlierThan10)
- {
- for (const auto& c : settings.categories)
- {
- auto* category = (UIUserNotificationCategory*) PushNotificationsDelegateDetails::categoryToNSCategory (c, iOSEarlierThan10);
- [categories addObject: category];
- }
-
- UIUserNotificationType type = NSUInteger ((bool)settings.allowBadge << 0
- | (bool)settings.allowSound << 1
- | (bool)settings.allowAlert << 2);
-
- UIUserNotificationSettings* s = [UIUserNotificationSettings settingsForTypes: type categories: categories];
- [[UIApplication sharedApplication] registerUserNotificationSettings: s];
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
- for (const auto& c : settings.categories)
- {
- auto* category = (UNNotificationCategory*) PushNotificationsDelegateDetails::categoryToNSCategory (c, iOSEarlierThan10);
- [categories addObject: category];
- }
-
- UNAuthorizationOptions authOptions = NSUInteger ((bool)settings.allowBadge << 0
- | (bool)settings.allowSound << 1
- | (bool)settings.allowAlert << 2);
-
- [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories: categories];
- [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions: authOptions
- completionHandler: ^(BOOL /*granted*/, NSError* /*error*/)
- {
- requestSettingsUsed();
- }];
- }
- #endif
-
- [[UIApplication sharedApplication] registerForRemoteNotifications];
- }
-
- void requestSettingsUsed()
- {
- if (iOSEarlierThan10)
- {
- UIUserNotificationSettings* s = [UIApplication sharedApplication].currentUserNotificationSettings;
-
- settings.allowBadge = s.types & UIUserNotificationTypeBadge;
- settings.allowSound = s.types & UIUserNotificationTypeSound;
- settings.allowAlert = s.types & UIUserNotificationTypeAlert;
-
- for (UIUserNotificationCategory *c in s.categories)
- settings.categories.add (PushNotificationsDelegateDetails::uiUserNotificationCategoryToCategory (c));
-
- owner.listeners.call ([&] (Listener& l) { l.notificationSettingsReceived (settings); });
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
-
- [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:
- ^(UNNotificationSettings* s)
- {
- [[UNUserNotificationCenter currentNotificationCenter] getNotificationCategoriesWithCompletionHandler:
- ^(NSSet<UNNotificationCategory*>* categories)
- {
- settings.allowBadge = s.badgeSetting == UNNotificationSettingEnabled;
- settings.allowSound = s.soundSetting == UNNotificationSettingEnabled;
- settings.allowAlert = s.alertSetting == UNNotificationSettingEnabled;
-
- for (UNNotificationCategory* c in categories)
- settings.categories.add (PushNotificationsDelegateDetails::unNotificationCategoryToCategory (c));
-
- owner.listeners.call ([&] (Listener& l) { l.notificationSettingsReceived (settings); });
- }
- ];
-
- }];
- }
- #endif
- }
-
- bool areNotificationsEnabled() const { return true; }
-
- void sendLocalNotification (const Notification& n)
- {
- if (iOSEarlierThan10)
- {
- auto* notification = PushNotificationsDelegateDetails::juceNotificationToUILocalNotification (n);
-
- [[UIApplication sharedApplication] scheduleLocalNotification: notification];
- [notification release];
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
-
- UNNotificationRequest* request = PushNotificationsDelegateDetails::juceNotificationToUNNotificationRequest (n);
-
- [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest: request
- withCompletionHandler: ^(NSError* error)
- {
- jassert (error == nil);
-
- if (error != nil)
- NSLog (nsStringLiteral ("addNotificationRequest error: %@"), error);
- }];
- }
- #endif
- }
-
- void getDeliveredNotifications() const
- {
- if (iOSEarlierThan10)
- {
- // Not supported on this platform
- jassertfalse;
- owner.listeners.call ([] (Listener& l) { l.deliveredNotificationsListReceived ({}); });
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
- [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:
- ^(NSArray<UNNotification*>* notifications)
- {
- Array<PushNotifications::Notification> notifs;
-
- for (UNNotification* n in notifications)
- notifs.add (PushNotificationsDelegateDetails::unNotificationToJuceNotification (n));
-
- owner.listeners.call ([&] (Listener& l) { l.deliveredNotificationsListReceived (notifs); });
- }];
- }
- #endif
- }
-
- void removeAllDeliveredNotifications()
- {
- if (iOSEarlierThan10)
- {
- // Not supported on this platform
- jassertfalse;
- }
- else
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- {
-
- [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
- }
- #endif
- }
-
- void removeDeliveredNotification (const String& identifier)
- {
- if (iOSEarlierThan10)
- {
- ignoreUnused (identifier);
- // Not supported on this platform
- jassertfalse;
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
-
- NSArray<NSString*>* identifiers = [NSArray arrayWithObject: juceStringToNS (identifier)];
-
- [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers: identifiers];
- }
- #endif
- }
-
- void setupChannels (const Array<ChannelGroup>& groups, const Array<Channel>& channels)
- {
- ignoreUnused (groups, channels);
- }
-
- void getPendingLocalNotifications() const
- {
- if (iOSEarlierThan10)
- {
- Array<PushNotifications::Notification> notifs;
-
- for (UILocalNotification* n in [UIApplication sharedApplication].scheduledLocalNotifications)
- notifs.add (PushNotificationsDelegateDetails::uiLocalNotificationToJuceNotification (n));
-
- owner.listeners.call ([&] (Listener& l) { l.pendingLocalNotificationsListReceived (notifs); });
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
-
- [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:
- ^(NSArray<UNNotificationRequest*>* requests)
- {
- Array<PushNotifications::Notification> notifs;
-
- for (UNNotificationRequest* r : requests)
- notifs.add (PushNotificationsDelegateDetails::unNotificationRequestToJuceNotification (r));
-
- owner.listeners.call ([&] (Listener& l) { l.pendingLocalNotificationsListReceived (notifs); });
- }
- ];
- }
- #endif
- }
-
- void removePendingLocalNotification (const String& identifier)
- {
- if (iOSEarlierThan10)
- {
- // Not supported on this platform
- jassertfalse;
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
-
- NSArray<NSString*>* identifiers = [NSArray arrayWithObject: juceStringToNS (identifier)];
-
- [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers: identifiers];
- }
- #endif
- }
-
- void removeAllPendingLocalNotifications()
- {
- if (iOSEarlierThan10)
- {
- [[UIApplication sharedApplication] cancelAllLocalNotifications];
- }
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- else
- {
- [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
- }
- #endif
- }
-
- String getDeviceToken()
- {
- // You need to call requestPermissionsWithSettings() first.
- jassert (initialised);
-
- return deviceToken;
- }
-
- //==============================================================================
- //PushNotificationsDelegate
- void didRegisterUserNotificationSettings (UIUserNotificationSettings*) override
- {
- requestSettingsUsed();
- }
-
- void registeredForRemoteNotifications (NSData* deviceTokenToUse) override
- {
- deviceToken = [deviceTokenToUse]() -> String
- {
- auto length = deviceTokenToUse.length;
-
- if (auto* buffer = (const unsigned char*) deviceTokenToUse.bytes)
- {
- NSMutableString* hexString = [NSMutableString stringWithCapacity: (length * 2)];
-
- for (NSUInteger i = 0; i < length; ++i)
- [hexString appendFormat:@"%02x", buffer[i]];
-
- return nsStringToJuce ([hexString copy]);
- }
-
- return {};
- }();
-
- initialised = true;
-
- owner.listeners.call ([&] (Listener& l) { l.deviceTokenRefreshed (deviceToken); });
- }
-
- void failedToRegisterForRemoteNotifications (NSError* error) override
- {
- ignoreUnused (error);
-
- deviceToken.clear();
- }
-
- void didReceiveRemoteNotification (NSDictionary* userInfo) override
- {
- auto n = PushNotificationsDelegateDetails::nsDictionaryToJuceNotification (userInfo);
-
- owner.listeners.call ([&] (Listener& l) { l.handleNotification (false, n); });
- }
-
- void didReceiveRemoteNotificationFetchCompletionHandler (NSDictionary* userInfo,
- void (^completionHandler)(UIBackgroundFetchResult result)) override
- {
- didReceiveRemoteNotification (userInfo);
- completionHandler (UIBackgroundFetchResultNewData);
- }
-
- void handleActionForRemoteNotificationCompletionHandler (NSString* actionIdentifier,
- NSDictionary* userInfo,
- NSDictionary* responseInfo,
- void (^completionHandler)()) override
- {
- auto n = PushNotificationsDelegateDetails::nsDictionaryToJuceNotification (userInfo);
- auto actionString = nsStringToJuce (actionIdentifier);
- auto response = PushNotificationsDelegateDetails::getUserResponseFromNSDictionary (responseInfo);
-
- owner.listeners.call ([&] (Listener& l) { l.handleNotificationAction (false, n, actionString, response); });
-
- completionHandler();
- }
-
- void didReceiveLocalNotification (UILocalNotification* notification) override
- {
- auto n = PushNotificationsDelegateDetails::uiLocalNotificationToJuceNotification (notification);
-
- owner.listeners.call ([&] (Listener& l) { l.handleNotification (true, n); });
- }
-
- void handleActionForLocalNotificationCompletionHandler (NSString* actionIdentifier,
- UILocalNotification* notification,
- void (^completionHandler)()) override
- {
- handleActionForLocalNotificationWithResponseCompletionHandler (actionIdentifier,
- notification,
- nil,
- completionHandler);
- }
-
- void handleActionForLocalNotificationWithResponseCompletionHandler (NSString* actionIdentifier,
- UILocalNotification* notification,
- NSDictionary* responseInfo,
- void (^completionHandler)()) override
- {
- auto n = PushNotificationsDelegateDetails::uiLocalNotificationToJuceNotification (notification);
- auto actionString = nsStringToJuce (actionIdentifier);
- auto response = PushNotificationsDelegateDetails::getUserResponseFromNSDictionary (responseInfo);
-
- owner.listeners.call ([&] (Listener& l) { l.handleNotificationAction (true, n, actionString, response); });
-
- completionHandler();
- }
-
- #if defined (__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- void willPresentNotificationWithCompletionHandler (UNNotification* notification,
- void (^completionHandler)(UNNotificationPresentationOptions options)) override
- {
- NSUInteger options = NSUInteger ((int)settings.allowBadge << 0
- | (int)settings.allowSound << 1
- | (int)settings.allowAlert << 2);
-
- ignoreUnused (notification);
-
- completionHandler (options);
- }
-
- void didReceiveNotificationResponseWithCompletionHandler (UNNotificationResponse* response,
- void (^completionHandler)()) override
- {
- const bool remote = [response.notification.request.trigger isKindOfClass: [UNPushNotificationTrigger class]];
-
- auto actionString = nsStringToJuce (response.actionIdentifier);
-
- if (actionString == "com.apple.UNNotificationDefaultActionIdentifier")
- actionString.clear();
- else if (actionString == "com.apple.UNNotificationDismissActionIdentifier")
- actionString = "com.juce.NotificationDeleted";
-
- auto n = PushNotificationsDelegateDetails::unNotificationToJuceNotification (response.notification);
-
- String responseString;
-
- if ([response isKindOfClass: [UNTextInputNotificationResponse class]])
- {
- UNTextInputNotificationResponse* textResponse = (UNTextInputNotificationResponse*)response;
- responseString = nsStringToJuce (textResponse.userText);
- }
-
- owner.listeners.call ([&] (Listener& l) { l.handleNotificationAction (! remote, n, actionString, responseString); });
- completionHandler();
- }
- #endif
-
- void subscribeToTopic (const String& topic) { ignoreUnused (topic); }
- void unsubscribeFromTopic (const String& topic) { ignoreUnused (topic); }
-
- void sendUpstreamMessage (const String& serverSenderId,
- const String& collapseKey,
- const String& messageId,
- const String& messageType,
- int timeToLive,
- const StringPairArray& additionalData)
- {
- ignoreUnused (serverSenderId, collapseKey, messageId, messageType);
- ignoreUnused (timeToLive, additionalData);
- }
-
- private:
- PushNotifications& owner;
-
- const bool iOSEarlierThan10 = std::floor (NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max;
-
- bool initialised = false;
- String deviceToken;
-
- PushNotifications::Settings settings;
- };
-
- } // namespace juce
|