The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

554 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace PushNotificationsDelegateDetailsOsx
  22. {
  23. using Action = PushNotifications::Notification::Action;
  24. //==============================================================================
  25. NSUserNotification* juceNotificationToNSUserNotification (const PushNotifications::Notification& n,
  26. bool isEarlierThanMavericks,
  27. bool isEarlierThanYosemite)
  28. {
  29. auto* notification = [[NSUserNotification alloc] init];
  30. notification.title = juceStringToNS (n.title);
  31. notification.subtitle = juceStringToNS (n.subtitle);
  32. notification.informativeText = juceStringToNS (n.body);
  33. notification.userInfo = varObjectToNSDictionary (n.properties);
  34. auto triggerTime = Time::getCurrentTime() + RelativeTime (n.triggerIntervalSec);
  35. notification.deliveryDate = [NSDate dateWithTimeIntervalSince1970: triggerTime.toMilliseconds() / 1000.];
  36. if (n.repeat && n.triggerIntervalSec >= 60)
  37. {
  38. auto* dateComponents = [[NSDateComponents alloc] init];
  39. auto intervalSec = NSInteger (n.triggerIntervalSec);
  40. dateComponents.second = intervalSec;
  41. dateComponents.nanosecond = NSInteger ((n.triggerIntervalSec - intervalSec) * 1000000000);
  42. notification.deliveryRepeatInterval = dateComponents;
  43. [dateComponents autorelease];
  44. }
  45. auto soundToPlayString = n.soundToPlay.toString (true);
  46. if (soundToPlayString == "default_os_sound")
  47. {
  48. notification.soundName = NSUserNotificationDefaultSoundName;
  49. }
  50. else if (soundToPlayString.isNotEmpty())
  51. {
  52. auto* soundName = juceStringToNS (soundToPlayString.fromLastOccurrenceOf ("/", false, false)
  53. .upToLastOccurrenceOf (".", false, false));
  54. notification.soundName = soundName;
  55. }
  56. notification.hasActionButton = n.actions.size() > 0;
  57. if (n.actions.size() > 0)
  58. notification.actionButtonTitle = juceStringToNS (n.actions.getReference (0).title);
  59. if (! isEarlierThanMavericks)
  60. {
  61. notification.identifier = juceStringToNS (n.identifier);
  62. if (n.actions.size() > 0)
  63. {
  64. notification.hasReplyButton = n.actions.getReference (0).style == Action::text;
  65. notification.responsePlaceholder = juceStringToNS (n.actions.getReference (0).textInputPlaceholder);
  66. }
  67. auto* imageDirectory = n.icon.contains ("/")
  68. ? juceStringToNS (n.icon.upToLastOccurrenceOf ("/", false, true))
  69. : [NSString string];
  70. auto* imageName = juceStringToNS (n.icon.fromLastOccurrenceOf ("/", false, false)
  71. .upToLastOccurrenceOf (".", false, false));
  72. auto* imageExtension = juceStringToNS (n.icon.fromLastOccurrenceOf (".", false, false));
  73. NSString* imagePath = nil;
  74. if ([imageDirectory length] == NSUInteger (0))
  75. {
  76. imagePath = [[NSBundle mainBundle] pathForResource: imageName
  77. ofType: imageExtension];
  78. }
  79. else
  80. {
  81. imagePath = [[NSBundle mainBundle] pathForResource: imageName
  82. ofType: imageExtension
  83. inDirectory: imageDirectory];
  84. }
  85. notification.contentImage = [[NSImage alloc] initWithContentsOfFile: imagePath];
  86. if (! isEarlierThanYosemite)
  87. {
  88. if (n.actions.size() > 1)
  89. {
  90. auto* additionalActions = [NSMutableArray arrayWithCapacity: (NSUInteger) n.actions.size() - 1];
  91. for (int a = 1; a < n.actions.size(); ++a)
  92. [additionalActions addObject: [NSUserNotificationAction actionWithIdentifier: juceStringToNS (n.actions[a].identifier)
  93. title: juceStringToNS (n.actions[a].title)]];
  94. notification.additionalActions = additionalActions;
  95. }
  96. }
  97. }
  98. [notification autorelease];
  99. return notification;
  100. }
  101. //==============================================================================
  102. PushNotifications::Notification nsUserNotificationToJuceNotification (NSUserNotification* n,
  103. bool isEarlierThanMavericks,
  104. bool isEarlierThanYosemite)
  105. {
  106. PushNotifications::Notification notif;
  107. notif.title = nsStringToJuce (n.title);
  108. notif.subtitle = nsStringToJuce (n.subtitle);
  109. notif.body = nsStringToJuce (n.informativeText);
  110. notif.repeat = n.deliveryRepeatInterval != nil;
  111. if (n.deliveryRepeatInterval != nil)
  112. {
  113. notif.triggerIntervalSec = n.deliveryRepeatInterval.second + (n.deliveryRepeatInterval.nanosecond / 1000000000.);
  114. }
  115. else
  116. {
  117. NSDate* dateNow = [NSDate date];
  118. notif.triggerIntervalSec = [dateNow timeIntervalSinceDate: n.deliveryDate];
  119. }
  120. notif.soundToPlay = URL (nsStringToJuce (n.soundName));
  121. notif.properties = nsDictionaryToVar (n.userInfo);
  122. if (! isEarlierThanMavericks)
  123. {
  124. notif.identifier = nsStringToJuce (n.identifier);
  125. if (n.contentImage != nil)
  126. notif.icon = nsStringToJuce ([n.contentImage name]);
  127. }
  128. Array<Action> actions;
  129. if (n.actionButtonTitle != nil)
  130. {
  131. Action action;
  132. action.title = nsStringToJuce (n.actionButtonTitle);
  133. if (! isEarlierThanMavericks)
  134. {
  135. if (n.hasReplyButton)
  136. action.style = Action::text;
  137. if (n.responsePlaceholder != nil)
  138. action.textInputPlaceholder = nsStringToJuce (n.responsePlaceholder);
  139. }
  140. actions.add (action);
  141. }
  142. if (! isEarlierThanYosemite)
  143. {
  144. if (n.additionalActions != nil)
  145. {
  146. for (NSUserNotificationAction* a in n.additionalActions)
  147. {
  148. Action action;
  149. action.identifier = nsStringToJuce (a.identifier);
  150. action.title = nsStringToJuce (a.title);
  151. actions.add (action);
  152. }
  153. }
  154. }
  155. return notif;
  156. }
  157. //==============================================================================
  158. var getNotificationPropertiesFromDictionaryVar (const var& dictionaryVar)
  159. {
  160. auto* dictionaryVarObject = dictionaryVar.getDynamicObject();
  161. if (dictionaryVarObject == nullptr)
  162. return {};
  163. const auto& properties = dictionaryVarObject->getProperties();
  164. DynamicObject::Ptr propsVarObject = new DynamicObject();
  165. for (int i = 0; i < properties.size(); ++i)
  166. {
  167. auto propertyName = properties.getName (i).toString();
  168. if (propertyName == "aps")
  169. continue;
  170. propsVarObject->setProperty (propertyName, properties.getValueAt (i));
  171. }
  172. return var (propsVarObject.get());
  173. }
  174. PushNotifications::Notification nsDictionaryToJuceNotification (NSDictionary* dictionary)
  175. {
  176. const var dictionaryVar = nsDictionaryToVar (dictionary);
  177. const var apsVar = dictionaryVar.getProperty ("aps", {});
  178. if (! apsVar.isObject())
  179. return {};
  180. var alertVar = apsVar.getProperty ("alert", {});
  181. const var titleVar = alertVar.getProperty ("title", {});
  182. const var bodyVar = alertVar.isObject() ? alertVar.getProperty ("body", {}) : alertVar;
  183. const var categoryVar = apsVar.getProperty ("category", {});
  184. const var soundVar = apsVar.getProperty ("sound", {});
  185. const var badgeVar = apsVar.getProperty ("badge", {});
  186. const var threadIdVar = apsVar.getProperty ("thread-id", {});
  187. PushNotifications::Notification notification;
  188. notification.title = titleVar .toString();
  189. notification.body = bodyVar .toString();
  190. notification.groupId = threadIdVar.toString();
  191. notification.category = categoryVar.toString();
  192. notification.soundToPlay = URL (soundVar.toString());
  193. notification.badgeNumber = (int) badgeVar;
  194. notification.properties = getNotificationPropertiesFromDictionaryVar (dictionaryVar);
  195. return notification;
  196. }
  197. }
  198. //==============================================================================
  199. struct PushNotificationsDelegate
  200. {
  201. PushNotificationsDelegate() : delegate ([getClass().createInstance() init])
  202. {
  203. Class::setThis (delegate.get(), this);
  204. id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
  205. SEL selector = NSSelectorFromString (@"setPushNotificationsDelegate:");
  206. if ([appDelegate respondsToSelector: selector])
  207. [appDelegate performSelector: selector withObject: delegate.get()];
  208. [NSUserNotificationCenter defaultUserNotificationCenter].delegate = delegate.get();
  209. }
  210. virtual ~PushNotificationsDelegate()
  211. {
  212. [NSUserNotificationCenter defaultUserNotificationCenter].delegate = nil;
  213. }
  214. virtual void registeredForRemoteNotifications (NSData* deviceToken) = 0;
  215. virtual void failedToRegisterForRemoteNotifications (NSError* error) = 0;
  216. virtual void didReceiveRemoteNotification (NSDictionary* userInfo) = 0;
  217. virtual void didDeliverNotification (NSUserNotification* notification) = 0;
  218. virtual void didActivateNotification (NSUserNotification* notification) = 0;
  219. virtual bool shouldPresentNotification (NSUserNotification* notification) = 0;
  220. protected:
  221. std::unique_ptr<NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>, NSObjectDeleter> delegate;
  222. private:
  223. struct Class : public ObjCClass<NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>>
  224. {
  225. Class() : ObjCClass<NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>> ("JucePushNotificationsDelegate_")
  226. {
  227. addIvar<PushNotificationsDelegate*> ("self");
  228. addMethod (@selector (application:didRegisterForRemoteNotificationsWithDeviceToken:), registeredForRemoteNotifications, "v@:@@");
  229. addMethod (@selector (application:didFailToRegisterForRemoteNotificationsWithError:), failedToRegisterForRemoteNotifications, "v@:@@");
  230. addMethod (@selector (application:didReceiveRemoteNotification:), didReceiveRemoteNotification, "v@:@@");
  231. addMethod (@selector (userNotificationCenter:didDeliverNotification:), didDeliverNotification, "v@:@@");
  232. addMethod (@selector (userNotificationCenter:didActivateNotification:), didActivateNotification, "v@:@@");
  233. addMethod (@selector (userNotificationCenter:shouldPresentNotification:), shouldPresentNotification, "B@:@@");
  234. registerClass();
  235. }
  236. //==============================================================================
  237. static PushNotificationsDelegate& getThis (id self) { return *getIvar<PushNotificationsDelegate*> (self, "self"); }
  238. static void setThis (id self, PushNotificationsDelegate* d) { object_setInstanceVariable (self, "self", d); }
  239. //==============================================================================
  240. static void registeredForRemoteNotifications (id self, SEL, NSApplication*,
  241. NSData* deviceToken) { getThis (self).registeredForRemoteNotifications (deviceToken); }
  242. static void failedToRegisterForRemoteNotifications (id self, SEL, NSApplication*,
  243. NSError* error) { getThis (self).failedToRegisterForRemoteNotifications (error); }
  244. static void didReceiveRemoteNotification (id self, SEL, NSApplication*,
  245. NSDictionary* userInfo) { getThis (self).didReceiveRemoteNotification (userInfo); }
  246. static void didDeliverNotification (id self, SEL, NSUserNotificationCenter*,
  247. NSUserNotification* notification) { getThis (self).didDeliverNotification (notification); }
  248. static void didActivateNotification (id self, SEL, NSUserNotificationCenter*,
  249. NSUserNotification* notification) { getThis (self).didActivateNotification (notification); }
  250. static bool shouldPresentNotification (id self, SEL, NSUserNotificationCenter*,
  251. NSUserNotification* notification) { return getThis (self).shouldPresentNotification (notification); }
  252. };
  253. //==============================================================================
  254. static Class& getClass()
  255. {
  256. static Class c;
  257. return c;
  258. }
  259. };
  260. //==============================================================================
  261. bool PushNotifications::Notification::isValid() const noexcept { return true; }
  262. //==============================================================================
  263. struct PushNotifications::Pimpl : private PushNotificationsDelegate
  264. {
  265. Pimpl (PushNotifications& p)
  266. : owner (p)
  267. {
  268. }
  269. void requestPermissionsWithSettings (const PushNotifications::Settings& settingsToUse)
  270. {
  271. if (isEarlierThanLion)
  272. return;
  273. settings = settingsToUse;
  274. NSRemoteNotificationType types = NSUInteger ((bool) settings.allowBadge);
  275. if (isAtLeastMountainLion)
  276. types |= ((bool) settings.allowSound << 1 | (bool) settings.allowAlert << 2);
  277. [[NSApplication sharedApplication] registerForRemoteNotificationTypes: types];
  278. }
  279. void requestSettingsUsed()
  280. {
  281. if (isEarlierThanLion)
  282. {
  283. // no settings available
  284. owner.listeners.call ([] (Listener& l) { l.notificationSettingsReceived ({}); });
  285. return;
  286. }
  287. settings.allowBadge = [NSApplication sharedApplication].enabledRemoteNotificationTypes & NSRemoteNotificationTypeBadge;
  288. if (isAtLeastMountainLion)
  289. {
  290. settings.allowSound = [NSApplication sharedApplication].enabledRemoteNotificationTypes & NSRemoteNotificationTypeSound;
  291. settings.allowAlert = [NSApplication sharedApplication].enabledRemoteNotificationTypes & NSRemoteNotificationTypeAlert;
  292. }
  293. owner.listeners.call ([&] (Listener& l) { l.notificationSettingsReceived (settings); });
  294. }
  295. bool areNotificationsEnabled() const { return true; }
  296. void sendLocalNotification (const Notification& n)
  297. {
  298. auto* notification = PushNotificationsDelegateDetailsOsx::juceNotificationToNSUserNotification (n, isEarlierThanMavericks, isEarlierThanYosemite);
  299. [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification: notification];
  300. }
  301. void getDeliveredNotifications() const
  302. {
  303. Array<PushNotifications::Notification> notifs;
  304. for (NSUserNotification* n in [NSUserNotificationCenter defaultUserNotificationCenter].deliveredNotifications)
  305. notifs.add (PushNotificationsDelegateDetailsOsx::nsUserNotificationToJuceNotification (n, isEarlierThanMavericks, isEarlierThanYosemite));
  306. owner.listeners.call ([&] (Listener& l) { l.deliveredNotificationsListReceived (notifs); });
  307. }
  308. void removeAllDeliveredNotifications()
  309. {
  310. [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications];
  311. }
  312. void removeDeliveredNotification (const String& identifier)
  313. {
  314. PushNotifications::Notification n;
  315. n.identifier = identifier;
  316. auto nsNotification = PushNotificationsDelegateDetailsOsx::juceNotificationToNSUserNotification (n, isEarlierThanMavericks, isEarlierThanYosemite);
  317. [[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification: nsNotification];
  318. }
  319. void setupChannels (const Array<ChannelGroup>& groups, const Array<Channel>& channels)
  320. {
  321. ignoreUnused (groups, channels);
  322. }
  323. void getPendingLocalNotifications() const
  324. {
  325. Array<PushNotifications::Notification> notifs;
  326. for (NSUserNotification* n in [NSUserNotificationCenter defaultUserNotificationCenter].scheduledNotifications)
  327. notifs.add (PushNotificationsDelegateDetailsOsx::nsUserNotificationToJuceNotification (n, isEarlierThanMavericks, isEarlierThanYosemite));
  328. owner.listeners.call ([&] (Listener& l) { l.pendingLocalNotificationsListReceived (notifs); });
  329. }
  330. void removePendingLocalNotification (const String& identifier)
  331. {
  332. PushNotifications::Notification n;
  333. n.identifier = identifier;
  334. auto nsNotification = PushNotificationsDelegateDetailsOsx::juceNotificationToNSUserNotification (n, isEarlierThanMavericks, isEarlierThanYosemite);
  335. [[NSUserNotificationCenter defaultUserNotificationCenter] removeScheduledNotification: nsNotification];
  336. }
  337. void removeAllPendingLocalNotifications()
  338. {
  339. for (NSUserNotification* n in [NSUserNotificationCenter defaultUserNotificationCenter].scheduledNotifications)
  340. [[NSUserNotificationCenter defaultUserNotificationCenter] removeScheduledNotification: n];
  341. }
  342. String getDeviceToken()
  343. {
  344. // You need to call requestPermissionsWithSettings() first.
  345. jassert (initialised);
  346. return deviceToken;
  347. }
  348. //==============================================================================
  349. //PushNotificationsDelegate
  350. void registeredForRemoteNotifications (NSData* deviceTokenToUse) override
  351. {
  352. auto* deviceTokenString = [[[[deviceTokenToUse description]
  353. stringByReplacingOccurrencesOfString: nsStringLiteral ("<") withString: nsStringLiteral ("")]
  354. stringByReplacingOccurrencesOfString: nsStringLiteral (">") withString: nsStringLiteral ("")]
  355. stringByReplacingOccurrencesOfString: nsStringLiteral (" ") withString: nsStringLiteral ("")];
  356. deviceToken = nsStringToJuce (deviceTokenString);
  357. initialised = true;
  358. owner.listeners.call ([&] (Listener& l) { l.deviceTokenRefreshed (deviceToken); });
  359. }
  360. void failedToRegisterForRemoteNotifications (NSError* error) override
  361. {
  362. ignoreUnused (error);
  363. deviceToken.clear();
  364. }
  365. void didReceiveRemoteNotification (NSDictionary* userInfo) override
  366. {
  367. auto n = PushNotificationsDelegateDetailsOsx::nsDictionaryToJuceNotification (userInfo);
  368. owner.listeners.call ([&] (Listener& l) { l.handleNotification (true, n); });
  369. }
  370. void didDeliverNotification (NSUserNotification* notification) override
  371. {
  372. ignoreUnused (notification);
  373. }
  374. void didActivateNotification (NSUserNotification* notification) override
  375. {
  376. auto n = PushNotificationsDelegateDetailsOsx::nsUserNotificationToJuceNotification (notification, isEarlierThanMavericks, isEarlierThanYosemite);
  377. if (notification.activationType == NSUserNotificationActivationTypeContentsClicked)
  378. {
  379. owner.listeners.call ([&] (Listener& l) { l.handleNotification (notification.remote, n); });
  380. }
  381. else
  382. {
  383. auto actionIdentifier = (! isEarlierThanYosemite && notification.additionalActivationAction != nil)
  384. ? nsStringToJuce (notification.additionalActivationAction.identifier)
  385. : nsStringToJuce (notification.actionButtonTitle);
  386. auto reply = notification.activationType == NSUserNotificationActivationTypeReplied
  387. ? nsStringToJuce ([notification.response string])
  388. : String();
  389. owner.listeners.call ([&] (Listener& l) { l.handleNotificationAction (notification.remote, n, actionIdentifier, reply); });
  390. }
  391. }
  392. bool shouldPresentNotification (NSUserNotification* notification) override { return true; }
  393. void subscribeToTopic (const String& topic) { ignoreUnused (topic); }
  394. void unsubscribeFromTopic (const String& topic) { ignoreUnused (topic); }
  395. void sendUpstreamMessage (const String& serverSenderId,
  396. const String& collapseKey,
  397. const String& messageId,
  398. const String& messageType,
  399. int timeToLive,
  400. const StringPairArray& additionalData)
  401. {
  402. ignoreUnused (serverSenderId, collapseKey, messageId, messageType);
  403. ignoreUnused (timeToLive, additionalData);
  404. }
  405. private:
  406. PushNotifications& owner;
  407. const bool isEarlierThanLion = std::floor (NSFoundationVersionNumber) < std::floor (NSFoundationVersionNumber10_7);
  408. const bool isAtLeastMountainLion = std::floor (NSFoundationVersionNumber) >= NSFoundationVersionNumber10_7;
  409. const bool isEarlierThanMavericks = std::floor (NSFoundationVersionNumber) < NSFoundationVersionNumber10_9;
  410. const bool isEarlierThanYosemite = std::floor (NSFoundationVersionNumber) <= NSFoundationVersionNumber10_9;
  411. bool initialised = false;
  412. String deviceToken;
  413. PushNotifications::Settings settings;
  414. };
  415. } // namespace juce