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.

1245 lines
57KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: PushNotificationsDemo
  20. version: 2.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases push notifications features. To run this demo you must enable the
  24. "Push Notifications Capability" option in the Projucer exporter.
  25. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  26. juce_audio_processors, juce_audio_utils, juce_core,
  27. juce_data_structures, juce_events, juce_graphics,
  28. juce_gui_basics, juce_gui_extra
  29. exporters: xcode_mac, xcode_iphone, androidstudio
  30. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  31. JUCE_PUSH_NOTIFICATIONS=1
  32. type: Component
  33. mainClass: PushNotificationsDemo
  34. useLocalCopy: 1
  35. END_JUCE_PIP_METADATA
  36. *******************************************************************************/
  37. #pragma once
  38. #include "../Assets/DemoUtilities.h"
  39. /*
  40. To finish the setup of this demo, do the following:
  41. 1. Download google_services.json from your Firebase project.
  42. 2. Update "Remote Notifications Config File" path in Android exporter (this can be different for debug and release)
  43. to point to that json file.
  44. 3. Add image and sound resources by adding the following to "Extra Android Raw Resources" in Projucer:
  45. ../../Assets/Notifications/images/ic_stat_name.png
  46. ../../Assets/Notifications/images/ic_stat_name2.png
  47. ../../Assets/Notifications/images/ic_stat_name3.png
  48. ../../Assets/Notifications/images/ic_stat_name4.png
  49. ../../Assets/Notifications/images/ic_stat_name5.png
  50. ../../Assets/Notifications/images/ic_stat_name6.png
  51. ../../Assets/Notifications/images/ic_stat_name7.png
  52. ../../Assets/Notifications/images/ic_stat_name8.png
  53. ../../Assets/Notifications/images/ic_stat_name9.png
  54. ../../Assets/Notifications/images/ic_stat_name10.png
  55. ../../Assets/Notifications/sounds/demonstrative.mp3
  56. ../../Assets/Notifications/sounds/isntit.mp3
  57. ../../Assets/Notifications/sounds/jinglebellssms.mp3
  58. ../../Assets/Notifications/sounds/served.mp3
  59. ../../Assets/Notifications/sounds/solemn.mp3
  60. 4. Set "Remote Notifications" to enabled in Projucer Android exporter.
  61. To verify that remote notifications are configured properly, go to Remote tab in the demo and press "GetDeviceToken"
  62. button, a dialog with your token (also printed to console in debug build) should show up.
  63. The following steps are only necessary if you have a custom activity defined:
  64. 5. Ensure that its launchMode is set to "singleTop" or "singleTask" in Android manifest. This is the default behaviour
  65. in JUCE so you only need to do it if you have custom Android manifest content. You can do it from Projucer by
  66. ensuring that "Custom Manifest XML Content" contains:
  67. <manifest>
  68. <application>
  69. <activity android:launchMode="singleTask">
  70. </activity>
  71. </application>
  72. </manifest>
  73. 6. Ensure that you override onNewIntent() function in the same way as it is done in JuceActivity.java:
  74. package com.rmsl.juce;
  75. import android.app.Activity;
  76. import android.content.Intent;
  77. //==============================================================================
  78. public class JuceActivity extends Activity
  79. {
  80. //==============================================================================
  81. private native void appNewIntent (Intent intent);
  82. @Override
  83. protected void onNewIntent (Intent intent)
  84. {
  85. super.onNewIntent(intent);
  86. setIntent(intent);
  87. appNewIntent (intent);
  88. }
  89. }
  90. */
  91. //==============================================================================
  92. class PushNotificationsDemo : public Component,
  93. private ChangeListener,
  94. private ComponentListener,
  95. private PushNotifications::Listener
  96. {
  97. public:
  98. //==============================================================================
  99. PushNotificationsDemo()
  100. {
  101. setupControls();
  102. distributeControls();
  103. #if JUCE_PUSH_NOTIFICATIONS
  104. addAndMakeVisible (headerLabel);
  105. addAndMakeVisible (mainTabs);
  106. addAndMakeVisible (sendButton);
  107. #else
  108. addAndMakeVisible (notAvailableYetLabel);
  109. #endif
  110. headerLabel .setJustificationType (Justification::centred);
  111. notAvailableYetLabel.setJustificationType (Justification::centred);
  112. #if JUCE_MAC
  113. StringArray tabNames { "Params1", "Params2", "Params3", "Params4" };
  114. #else
  115. StringArray tabNames { "Req. params", "Opt. params1", "Opt. params2", "Opt. params3" };
  116. #endif
  117. auto colour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId);
  118. localNotificationsTabs.addTab (tabNames[0], colour, &paramsOneView, false);
  119. localNotificationsTabs.addTab (tabNames[1], colour, &paramsTwoView, false);
  120. #if JUCE_ANDROID
  121. localNotificationsTabs.addTab (tabNames[2], colour, &paramsThreeView, false);
  122. localNotificationsTabs.addTab (tabNames[3], colour, &paramsFourView, false);
  123. #endif
  124. localNotificationsTabs.addTab ("Aux. actions", colour, &auxActionsView, false);
  125. mainTabs.addTab ("Local", colour, &localNotificationsTabs, false);
  126. mainTabs.addTab ("Remote", colour, &remoteView, false);
  127. auto userArea = Desktop::getInstance().getDisplays().getPrimaryDisplay()->userArea;
  128. #if JUCE_ANDROID || JUCE_IOS
  129. setSize (userArea.getWidth(), userArea.getHeight());
  130. #else
  131. setSize (userArea.getWidth() / 2, userArea.getHeight() / 2);
  132. #endif
  133. sendButton.onClick = [this] { sendLocalNotification(); };
  134. auxActionsView.getDeliveredNotificationsButton .onClick = []
  135. { PushNotifications::getInstance()->getDeliveredNotifications(); };
  136. auxActionsView.removeDeliveredNotifWithIdButton.onClick = [this]
  137. { PushNotifications::getInstance()->removeDeliveredNotification (auxActionsView.deliveredNotifIdentifier.getText()); };
  138. auxActionsView.removeAllDeliveredNotifsButton .onClick = []
  139. { PushNotifications::getInstance()->removeAllDeliveredNotifications(); };
  140. #if JUCE_IOS || JUCE_MAC
  141. auxActionsView.getPendingNotificationsButton .onClick = []
  142. { PushNotifications::getInstance()->getPendingLocalNotifications(); };
  143. auxActionsView.removePendingNotifWithIdButton.onClick = [this]
  144. { PushNotifications::getInstance()->removePendingLocalNotification (auxActionsView.pendingNotifIdentifier.getText()); };
  145. auxActionsView.removeAllPendingNotifsButton .onClick = []
  146. { PushNotifications::getInstance()->removeAllPendingLocalNotifications(); };
  147. #endif
  148. remoteView.getDeviceTokenButton.onClick = []
  149. {
  150. String token = PushNotifications::getInstance()->getDeviceToken();
  151. DBG ("token = " + token);
  152. if (token.isEmpty())
  153. showRemoteInstructions();
  154. else
  155. NativeMessageBox::showAsync (MessageBoxOptions()
  156. .withIconType (MessageBoxIconType::InfoIcon)
  157. .withTitle ("Device token")
  158. .withMessage (token),
  159. nullptr);
  160. };
  161. #if JUCE_ANDROID
  162. remoteView.sendRemoteMessageButton.onClick = []
  163. {
  164. StringPairArray data;
  165. data.set ("key1", "value1");
  166. data.set ("key2", "value2");
  167. static int id = 100;
  168. PushNotifications::getInstance()->sendUpstreamMessage ("872047750958",
  169. "com.juce.pushnotificationsdemo",
  170. String (id++),
  171. "standardType",
  172. 3600,
  173. data);
  174. };
  175. remoteView.subscribeToSportsButton .onClick = []
  176. { PushNotifications::getInstance()->subscribeToTopic ("sports"); };
  177. remoteView.unsubscribeFromSportsButton.onClick = []
  178. { PushNotifications::getInstance()->unsubscribeFromTopic ("sports"); };
  179. #endif
  180. paramControls.accentColourButton.onClick = [this] { setupAccentColour(); };
  181. paramControls.ledColourButton .onClick = [this] { setupLedColour(); };
  182. jassert (PushNotifications::getInstance()->areNotificationsEnabled());
  183. PushNotifications::getInstance()->addListener (this);
  184. #if JUCE_IOS || JUCE_MAC
  185. paramControls.fireInComboBox.onChange = [this] { delayNotification(); };
  186. PushNotifications::getInstance()->requestPermissionsWithSettings (getNotificationSettings());
  187. #elif JUCE_ANDROID
  188. PushNotifications::ChannelGroup cg { "demoGroup", "demo group" };
  189. PushNotifications::getInstance()->setupChannels ({ { cg } }, getAndroidChannels());
  190. #endif
  191. #if JUCE_IOS || JUCE_ANDROID
  192. setPortraitOrientationEnabled (true);
  193. #endif
  194. }
  195. ~PushNotificationsDemo() override
  196. {
  197. PushNotifications::getInstance()->removeListener (this);
  198. #if JUCE_IOS || JUCE_ANDROID
  199. setPortraitOrientationEnabled (false);
  200. #endif
  201. }
  202. void setPortraitOrientationEnabled (bool shouldBeEnabled)
  203. {
  204. auto allowedOrientations = Desktop::getInstance().getOrientationsEnabled();
  205. if (shouldBeEnabled)
  206. allowedOrientations |= Desktop::upright;
  207. else
  208. allowedOrientations &= ~Desktop::upright;
  209. Desktop::getInstance().setOrientationsEnabled (allowedOrientations);
  210. }
  211. void paint (Graphics& g) override
  212. {
  213. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  214. }
  215. void resized() override
  216. {
  217. auto bounds = getLocalBounds().reduced (getWidth() / 20, getHeight() / 40);
  218. headerLabel.setBounds (bounds.removeFromTop (bounds.proportionOfHeight (0.1f)));
  219. mainTabs .setBounds (bounds.removeFromTop (bounds.proportionOfHeight (0.8f)));
  220. sendButton .setBounds (bounds);
  221. notAvailableYetLabel.setBounds (getLocalBounds());
  222. }
  223. private:
  224. void delayNotification()
  225. {
  226. auto repeatsAllowed = paramControls.fireInComboBox.getSelectedItemIndex() >= 6;
  227. paramControls.repeatButton.setEnabled (repeatsAllowed);
  228. if (! repeatsAllowed)
  229. paramControls.repeatButton.setToggleState (false, NotificationType::sendNotification);
  230. }
  231. void sendLocalNotification()
  232. {
  233. PushNotifications::Notification n;
  234. fillRequiredParams (n);
  235. fillOptionalParamsOne (n);
  236. #if JUCE_ANDROID
  237. fillOptionalParamsTwo (n);
  238. fillOptionalParamsThree (n);
  239. #endif
  240. if (! n.isValid())
  241. {
  242. #if JUCE_IOS
  243. String requiredFields = "identifier (from iOS 10), title, body and category";
  244. #elif JUCE_ANDROID
  245. String requiredFields = "channel ID (from Android O), title, body and icon";
  246. #else
  247. String requiredFields = "all required fields";
  248. #endif
  249. NativeMessageBox::showAsync (MessageBoxOptions()
  250. .withIconType (MessageBoxIconType::InfoIcon)
  251. .withTitle ("Incorrect notifications setup")
  252. .withMessage ("Please make sure that " + requiredFields + " are set."),
  253. nullptr);
  254. return;
  255. }
  256. PushNotifications::getInstance()->sendLocalNotification (n);
  257. }
  258. void fillRequiredParams (PushNotifications::Notification& n)
  259. {
  260. n.identifier = paramControls.identifierEditor.getText();
  261. n.title = paramControls.titleEditor .getText();
  262. n.body = paramControls.bodyEditor .getText();
  263. #if JUCE_IOS
  264. n.category = paramControls.categoryComboBox.getText();
  265. #elif JUCE_ANDROID || JUCE_MAC
  266. #if JUCE_MAC
  267. String prefix = "Notifications/images/";
  268. String extension = ".png";
  269. #else
  270. String prefix;
  271. String extension;
  272. #endif
  273. if (paramControls.iconComboBox.getSelectedItemIndex() == 0)
  274. n.icon = prefix + "ic_stat_name" + extension;
  275. else if (paramControls.iconComboBox.getSelectedItemIndex() == 1)
  276. n.icon = prefix + "ic_stat_name2" + extension;
  277. else if (paramControls.iconComboBox.getSelectedItemIndex() == 2)
  278. n.icon = prefix + "ic_stat_name3" + extension;
  279. else if (paramControls.iconComboBox.getSelectedItemIndex() == 3)
  280. n.icon = prefix + "ic_stat_name4" + extension;
  281. else if (paramControls.iconComboBox.getSelectedItemIndex() == 4)
  282. n.icon = prefix + "ic_stat_name5" + extension;
  283. #endif
  284. #if JUCE_ANDROID
  285. // Note: this is not strictly speaking required param, just doing it here because it is the fastest way!
  286. n.publicVersion.reset (new PushNotifications::Notification());
  287. n.publicVersion->identifier = "blahblahblah";
  288. n.publicVersion->title = "Public title!";
  289. n.publicVersion->body = "Public body!";
  290. n.publicVersion->icon = n.icon;
  291. n.channelId = String (paramControls.channelIdComboBox.getSelectedItemIndex() + 1);
  292. #endif
  293. }
  294. void fillOptionalParamsOne (PushNotifications::Notification& n)
  295. {
  296. n.subtitle = paramControls.subtitleEditor.getText();
  297. n.badgeNumber = paramControls.badgeNumberComboBox.getSelectedItemIndex();
  298. if (paramControls.soundToPlayComboBox.getSelectedItemIndex() > 0)
  299. n.soundToPlay = URL (paramControls.soundToPlayComboBox.getItemText (paramControls.soundToPlayComboBox.getSelectedItemIndex()));
  300. n.properties = JSON::parse (paramControls.propertiesEditor.getText());
  301. #if JUCE_IOS || JUCE_MAC
  302. n.triggerIntervalSec = double (paramControls.fireInComboBox.getSelectedItemIndex() * 10);
  303. n.repeat = paramControls.repeatButton.getToggleState();
  304. #elif JUCE_ANDROID
  305. if (paramControls.largeIconComboBox.getSelectedItemIndex() == 1)
  306. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name6.png");
  307. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 2)
  308. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name7.png");
  309. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 3)
  310. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name8.png");
  311. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 4)
  312. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name9.png");
  313. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 5)
  314. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name10.png");
  315. n.badgeIconType = (PushNotifications::Notification::BadgeIconType) paramControls.badgeIconComboBox.getSelectedItemIndex();
  316. n.tickerText = paramControls.tickerTextEditor.getText();
  317. n.shouldAutoCancel = paramControls.autoCancelButton .getToggleState();
  318. n.alertOnlyOnce = paramControls.alertOnlyOnceButton.getToggleState();
  319. #endif
  320. #if JUCE_ANDROID || JUCE_MAC
  321. if (paramControls.actionsComboBox.getSelectedItemIndex() == 1)
  322. {
  323. PushNotifications::Notification::Action a, a2;
  324. a .style = PushNotifications::Notification::Action::button;
  325. a2.style = PushNotifications::Notification::Action::button;
  326. a .title = a .identifier = "Ok";
  327. a2.title = a2.identifier = "Cancel";
  328. n.actions.add (a);
  329. n.actions.add (a2);
  330. }
  331. else if (paramControls.actionsComboBox.getSelectedItemIndex() == 2)
  332. {
  333. PushNotifications::Notification::Action a, a2;
  334. a .title = a .identifier = "Input Text Here";
  335. a2.title = a2.identifier = "No";
  336. a .style = PushNotifications::Notification::Action::text;
  337. a2.style = PushNotifications::Notification::Action::button;
  338. a .icon = "ic_stat_name4";
  339. a2.icon = "ic_stat_name5";
  340. a.textInputPlaceholder = "placeholder text ...";
  341. n.actions.add (a);
  342. n.actions.add (a2);
  343. }
  344. else if (paramControls.actionsComboBox.getSelectedItemIndex() == 3)
  345. {
  346. PushNotifications::Notification::Action a, a2;
  347. a .title = a .identifier = "Ok";
  348. a2.title = a2.identifier = "Cancel";
  349. a .style = PushNotifications::Notification::Action::button;
  350. a2.style = PushNotifications::Notification::Action::button;
  351. a .icon = "ic_stat_name4";
  352. a2.icon = "ic_stat_name5";
  353. n.actions.add (a);
  354. n.actions.add (a2);
  355. }
  356. else if (paramControls.actionsComboBox.getSelectedItemIndex() == 4)
  357. {
  358. PushNotifications::Notification::Action a, a2;
  359. a .title = a .identifier = "Input Text Here";
  360. a2.title = a2.identifier = "No";
  361. a .style = PushNotifications::Notification::Action::text;
  362. a2.style = PushNotifications::Notification::Action::button;
  363. a .icon = "ic_stat_name4";
  364. a2.icon = "ic_stat_name5";
  365. a.textInputPlaceholder = "placeholder text ...";
  366. a.allowedResponses.add ("Response 1");
  367. a.allowedResponses.add ("Response 2");
  368. a.allowedResponses.add ("Response 3");
  369. n.actions.add (a);
  370. n.actions.add (a2);
  371. }
  372. #endif
  373. }
  374. void fillOptionalParamsTwo (PushNotifications::Notification& n)
  375. {
  376. using Notification = PushNotifications::Notification;
  377. Notification::Progress progress;
  378. progress.max = paramControls.progressMaxComboBox .getSelectedItemIndex() * 10;
  379. progress.current = paramControls.progressCurrentComboBox.getSelectedItemIndex() * 10;
  380. progress.indeterminate = paramControls.progressIndeterminateButton.getToggleState();
  381. n.progress = progress;
  382. n.person = paramControls.personEditor.getText();
  383. n.type = Notification::Type (paramControls.categoryComboBox .getSelectedItemIndex());
  384. n.priority = Notification::Priority (paramControls.priorityComboBox .getSelectedItemIndex() - 2);
  385. n.lockScreenAppearance = Notification::LockScreenAppearance (paramControls.lockScreenVisibilityComboBox.getSelectedItemIndex() - 1);
  386. n.groupId = paramControls.groupIdEditor.getText();
  387. n.groupSortKey = paramControls.sortKeyEditor.getText();
  388. n.groupSummary = paramControls.groupSummaryButton.getToggleState();
  389. n.groupAlertBehaviour = Notification::GroupAlertBehaviour (paramControls.groupAlertBehaviourComboBox.getSelectedItemIndex());
  390. }
  391. void fillOptionalParamsThree (PushNotifications::Notification& n)
  392. {
  393. n.accentColour = paramControls.accentColourButton.findColour (TextButton::buttonColourId, false);
  394. n.ledColour = paramControls.ledColourButton .findColour (TextButton::buttonColourId, false);
  395. using Notification = PushNotifications::Notification;
  396. Notification::LedBlinkPattern ledBlinkPattern;
  397. ledBlinkPattern.msToBeOn = paramControls.ledMsToBeOnComboBox .getSelectedItemIndex() * 200;
  398. ledBlinkPattern.msToBeOff = paramControls.ledMsToBeOffComboBox.getSelectedItemIndex() * 200;
  399. n.ledBlinkPattern = ledBlinkPattern;
  400. Array<int> vibrationPattern;
  401. if (paramControls.vibratorMsToBeOnComboBox .getSelectedItemIndex() > 0 &&
  402. paramControls.vibratorMsToBeOffComboBox.getSelectedItemIndex() > 0)
  403. {
  404. vibrationPattern.add (paramControls.vibratorMsToBeOffComboBox.getSelectedItemIndex() * 500);
  405. vibrationPattern.add (paramControls.vibratorMsToBeOnComboBox .getSelectedItemIndex() * 500);
  406. vibrationPattern.add (2 * paramControls.vibratorMsToBeOffComboBox.getSelectedItemIndex() * 500);
  407. vibrationPattern.add (2 * paramControls.vibratorMsToBeOnComboBox .getSelectedItemIndex() * 500);
  408. }
  409. n.vibrationPattern = vibrationPattern;
  410. n.localOnly = paramControls.localOnlyButton.getToggleState();
  411. n.ongoing = paramControls.ongoingButton.getToggleState();
  412. n.timestampVisibility = Notification::TimestampVisibility (paramControls.timestampVisibilityComboBox.getSelectedItemIndex());
  413. if (paramControls.timeoutAfterComboBox.getSelectedItemIndex() > 0)
  414. {
  415. auto index = paramControls.timeoutAfterComboBox.getSelectedItemIndex();
  416. n.timeoutAfterMs = index * 1000 + 4000;
  417. }
  418. }
  419. void setupAccentColour()
  420. {
  421. auto accentColourSelector = std::make_unique<ColourSelector>();
  422. accentColourSelector->setName ("accent colour");
  423. accentColourSelector->setCurrentColour (paramControls.accentColourButton.findColour (TextButton::buttonColourId));
  424. accentColourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  425. accentColourSelector->setSize (200, 200);
  426. accentColourSelector->addComponentListener (this);
  427. accentColourSelector->addChangeListener (this);
  428. paramControls.accentColourSelector = accentColourSelector.get();
  429. CallOutBox::launchAsynchronously (std::move (accentColourSelector), paramControls.accentColourButton.getScreenBounds(), nullptr);
  430. }
  431. void setupLedColour()
  432. {
  433. auto ledColourSelector = std::make_unique<ColourSelector>();
  434. ledColourSelector->setName ("led colour");
  435. ledColourSelector->setCurrentColour (paramControls.ledColourButton.findColour (TextButton::buttonColourId));
  436. ledColourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  437. ledColourSelector->setSize (200, 200);
  438. ledColourSelector->addComponentListener (this);
  439. ledColourSelector->addChangeListener (this);
  440. paramControls.ledColourSelector = ledColourSelector.get();
  441. CallOutBox::launchAsynchronously (std::move (ledColourSelector), paramControls.accentColourButton.getScreenBounds(), nullptr);
  442. }
  443. void changeListenerCallback (ChangeBroadcaster* source) override
  444. {
  445. if (source == paramControls.accentColourSelector)
  446. {
  447. auto c = paramControls.accentColourSelector->getCurrentColour();
  448. paramControls.accentColourButton.setColour (TextButton::buttonColourId, c);
  449. }
  450. else if (source == paramControls.ledColourSelector)
  451. {
  452. auto c = paramControls.ledColourSelector->getCurrentColour();
  453. paramControls.ledColourButton.setColour (TextButton::buttonColourId, c);
  454. }
  455. }
  456. void componentBeingDeleted (Component& component) override
  457. {
  458. if (&component == paramControls.accentColourSelector)
  459. paramControls.accentColourSelector = nullptr;
  460. else if (&component == paramControls.ledColourSelector)
  461. paramControls.ledColourSelector = nullptr;
  462. }
  463. void handleNotification (bool isLocalNotification, const PushNotifications::Notification& n) override
  464. {
  465. ignoreUnused (isLocalNotification);
  466. NativeMessageBox::showAsync (MessageBoxOptions()
  467. .withIconType (MessageBoxIconType::InfoIcon)
  468. .withTitle ("Received notification")
  469. .withMessage ("ID: " + n.identifier
  470. + ", title: " + n.title
  471. + ", body: " + n.body),
  472. nullptr);
  473. }
  474. void handleNotificationAction (bool isLocalNotification,
  475. const PushNotifications::Notification& n,
  476. const juce::String& actionIdentifier,
  477. const juce::String& optionalResponse) override
  478. {
  479. ignoreUnused (isLocalNotification);
  480. NativeMessageBox::showAsync (MessageBoxOptions()
  481. .withIconType (MessageBoxIconType::InfoIcon)
  482. .withTitle ("Received notification action")
  483. .withMessage ("ID: " + n.identifier
  484. + ", title: " + n.title
  485. + ", body: " + n.body
  486. + ", action: " + actionIdentifier
  487. + ", optionalResponse: " + optionalResponse),
  488. nullptr);
  489. PushNotifications::getInstance()->removeDeliveredNotification (n.identifier);
  490. }
  491. void localNotificationDismissedByUser (const PushNotifications::Notification& n) override
  492. {
  493. NativeMessageBox::showAsync (MessageBoxOptions()
  494. .withIconType (MessageBoxIconType::InfoIcon)
  495. .withTitle ("Notification dismissed by a user")
  496. .withMessage ("ID: " + n.identifier
  497. + ", title: " + n.title
  498. + ", body: " + n.body),
  499. nullptr);
  500. }
  501. void deliveredNotificationsListReceived (const Array<PushNotifications::Notification>& notifs) override
  502. {
  503. String text = "Received notifications: ";
  504. for (auto& n : notifs)
  505. text << "(" << n.identifier << ", " << n.title << ", " << n.body << "), ";
  506. NativeMessageBox::showAsync (MessageBoxOptions()
  507. .withIconType (MessageBoxIconType::InfoIcon)
  508. .withTitle ("Received notification list")
  509. .withMessage (text),
  510. nullptr);
  511. }
  512. void pendingLocalNotificationsListReceived (const Array<PushNotifications::Notification>& notifs) override
  513. {
  514. String text = "Pending notifications: ";
  515. for (auto& n : notifs)
  516. text << "(" << n.identifier << ", " << n.title << ", " << n.body << "), ";
  517. NativeMessageBox::showAsync (MessageBoxOptions()
  518. .withIconType (MessageBoxIconType::InfoIcon)
  519. .withTitle ("Pending notification list")
  520. .withMessage (text),
  521. nullptr);
  522. }
  523. void deviceTokenRefreshed (const String& token) override
  524. {
  525. NativeMessageBox::showAsync (MessageBoxOptions()
  526. .withIconType (MessageBoxIconType::InfoIcon)
  527. .withTitle ("Device token refreshed")
  528. .withMessage (token),
  529. nullptr);
  530. }
  531. #if JUCE_ANDROID
  532. void remoteNotificationsDeleted() override
  533. {
  534. NativeMessageBox::showAsync (MessageBoxOptions()
  535. .withIconType (MessageBoxIconType::InfoIcon)
  536. .withTitle ("Remote notifications deleted")
  537. .withMessage ("Some of the pending messages were removed!"),
  538. nullptr);
  539. }
  540. void upstreamMessageSent (const String& messageId) override
  541. {
  542. NativeMessageBox::showAsync (MessageBoxOptions()
  543. .withIconType (MessageBoxIconType::InfoIcon)
  544. .withTitle ("Upstream message sent")
  545. .withMessage ("Message id: " + messageId),
  546. nullptr);
  547. }
  548. void upstreamMessageSendingError (const String& messageId, const String& error) override
  549. {
  550. NativeMessageBox::showAsync (MessageBoxOptions()
  551. .withIconType (MessageBoxIconType::InfoIcon)
  552. .withTitle ("Upstream message sending error")
  553. .withMessage ("Message id: " + messageId
  554. + "\nerror: " + error),
  555. nullptr);
  556. }
  557. static Array<PushNotifications::Channel> getAndroidChannels()
  558. {
  559. using Channel = PushNotifications::Channel;
  560. Channel ch1, ch2, ch3;
  561. ch1.identifier = "1";
  562. ch1.name = "HighImportance";
  563. ch1.importance = PushNotifications::Channel::max;
  564. ch1.lockScreenAppearance = PushNotifications::Notification::showCompletely;
  565. ch1.description = "High Priority Channel for important stuff";
  566. ch1.groupId = "demoGroup";
  567. ch1.ledColour = Colours::red;
  568. ch1.bypassDoNotDisturb = true;
  569. ch1.canShowBadge = true;
  570. ch1.enableLights = true;
  571. ch1.enableVibration = true;
  572. ch1.soundToPlay = URL ("demonstrative");
  573. ch1.vibrationPattern = { 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 };
  574. ch2.identifier = "2";
  575. ch2.name = "MediumImportance";
  576. ch2.importance = PushNotifications::Channel::normal;
  577. ch2.lockScreenAppearance = PushNotifications::Notification::showPartially;
  578. ch2.description = "Medium Priority Channel for standard stuff";
  579. ch2.groupId = "demoGroup";
  580. ch2.ledColour = Colours::yellow;
  581. ch2.canShowBadge = true;
  582. ch2.enableLights = true;
  583. ch2.enableVibration = true;
  584. ch2.soundToPlay = URL ("default_os_sound");
  585. ch2.vibrationPattern = { 1000, 1000 };
  586. ch3.identifier = "3";
  587. ch3.name = "LowImportance";
  588. ch3.importance = PushNotifications::Channel::min;
  589. ch3.lockScreenAppearance = PushNotifications::Notification::dontShow;
  590. ch3.description = "Low Priority Channel for silly stuff";
  591. ch3.groupId = "demoGroup";
  592. return { ch1, ch2, ch3 };
  593. }
  594. #elif JUCE_IOS || JUCE_MAC
  595. static PushNotifications::Settings getNotificationSettings()
  596. {
  597. PushNotifications::Settings settings;
  598. settings.allowAlert = true;
  599. settings.allowBadge = true;
  600. settings.allowSound = true;
  601. #if JUCE_IOS
  602. using Action = PushNotifications::Settings::Action;
  603. using Category = PushNotifications::Settings::Category;
  604. Action okAction;
  605. okAction.identifier = "okAction";
  606. okAction.title = "OK!";
  607. okAction.style = Action::button;
  608. okAction.triggerInBackground = true;
  609. Action cancelAction;
  610. cancelAction.identifier = "cancelAction";
  611. cancelAction.title = "Cancel";
  612. cancelAction.style = Action::button;
  613. cancelAction.triggerInBackground = true;
  614. cancelAction.destructive = true;
  615. Action textAction;
  616. textAction.identifier = "textAction";
  617. textAction.title = "Enter text";
  618. textAction.style = Action::text;
  619. textAction.triggerInBackground = true;
  620. textAction.destructive = false;
  621. textAction.textInputButtonText = "Ok";
  622. textAction.textInputPlaceholder = "Enter text...";
  623. Category okCategory;
  624. okCategory.identifier = "okCategory";
  625. okCategory.actions = { okAction };
  626. Category okCancelCategory;
  627. okCancelCategory.identifier = "okCancelCategory";
  628. okCancelCategory.actions = { okAction, cancelAction };
  629. Category textCategory;
  630. textCategory.identifier = "textCategory";
  631. textCategory.actions = { textAction };
  632. textCategory.sendDismissAction = true;
  633. settings.categories = { okCategory, okCancelCategory, textCategory };
  634. #endif
  635. return settings;
  636. }
  637. #endif
  638. struct RowComponent : public Component
  639. {
  640. RowComponent (Label& l, Component& c, int u = 1)
  641. : label (l),
  642. editor (c),
  643. rowUnits (u)
  644. {
  645. addAndMakeVisible (label);
  646. addAndMakeVisible (editor);
  647. }
  648. void resized() override
  649. {
  650. auto bounds = getLocalBounds();
  651. label .setBounds (bounds.removeFromLeft (getWidth() / 3));
  652. editor.setBounds (bounds);
  653. }
  654. Label& label;
  655. Component& editor;
  656. int rowUnits;
  657. };
  658. struct ParamControls
  659. {
  660. Label identifierLabel { "identifierLabel", "Identifier" };
  661. TextEditor identifierEditor;
  662. Label titleLabel { "titleLabel", "Title" };
  663. TextEditor titleEditor;
  664. Label bodyLabel { "bodyLabel", "Body" };
  665. TextEditor bodyEditor;
  666. Label categoryLabel { "categoryLabel", "Category" };
  667. ComboBox categoryComboBox;
  668. Label channelIdLabel { "channelIdLabel", "Channel ID" };
  669. ComboBox channelIdComboBox;
  670. Label iconLabel { "iconLabel", "Icon" };
  671. ComboBox iconComboBox;
  672. Label subtitleLabel { "subtitleLabel", "Subtitle" };
  673. TextEditor subtitleEditor;
  674. Label badgeNumberLabel { "badgeNumberLabel", "BadgeNumber" };
  675. ComboBox badgeNumberComboBox;
  676. Label soundToPlayLabel { "soundToPlayLabel", "SoundToPlay" };
  677. ComboBox soundToPlayComboBox;
  678. Label propertiesLabel { "propertiesLabel", "Properties" };
  679. TextEditor propertiesEditor;
  680. Label fireInLabel { "fireInLabel", "Fire in" };
  681. ComboBox fireInComboBox;
  682. Label repeatLabel { "repeatLabel", "Repeat" };
  683. ToggleButton repeatButton;
  684. Label largeIconLabel { "largeIconLabel", "Large Icon" };
  685. ComboBox largeIconComboBox;
  686. Label badgeIconLabel { "badgeIconLabel", "Badge Icon" };
  687. ComboBox badgeIconComboBox;
  688. Label tickerTextLabel { "tickerTextLabel", "Ticker Text" };
  689. TextEditor tickerTextEditor;
  690. Label autoCancelLabel { "autoCancelLabel", "AutoCancel" };
  691. ToggleButton autoCancelButton;
  692. Label alertOnlyOnceLabel { "alertOnlyOnceLabel", "AlertOnlyOnce" };
  693. ToggleButton alertOnlyOnceButton;
  694. Label actionsLabel { "actionsLabel", "Actions" };
  695. ComboBox actionsComboBox;
  696. Label progressMaxLabel { "progressMaxLabel", "ProgressMax" };
  697. ComboBox progressMaxComboBox;
  698. Label progressCurrentLabel { "progressCurrentLabel", "ProgressCurrent" };
  699. ComboBox progressCurrentComboBox;
  700. Label progressIndeterminateLabel { "progressIndeterminateLabel", "ProgressIndeterminate" };
  701. ToggleButton progressIndeterminateButton;
  702. Label notifCategoryLabel { "notifCategoryLabel", "Category" };
  703. ComboBox notifCategoryComboBox;
  704. Label priorityLabel { "priorityLabel", "Priority" };
  705. ComboBox priorityComboBox;
  706. Label personLabel { "personLabel", "Person" };
  707. TextEditor personEditor;
  708. Label lockScreenVisibilityLabel { "lockScreenVisibilityLabel", "LockScreenVisibility" };
  709. ComboBox lockScreenVisibilityComboBox;
  710. Label groupIdLabel { "groupIdLabel", "GroupID" };
  711. TextEditor groupIdEditor;
  712. Label sortKeyLabel { "sortKeyLabel", "SortKey" };
  713. TextEditor sortKeyEditor;
  714. Label groupSummaryLabel { "groupSummaryLabel", "GroupSummary" };
  715. ToggleButton groupSummaryButton;
  716. Label groupAlertBehaviourLabel { "groupAlertBehaviourLabel", "GroupAlertBehaviour" };
  717. ComboBox groupAlertBehaviourComboBox;
  718. Label accentColourLabel { "accentColourLabel", "AccentColour" };
  719. TextButton accentColourButton;
  720. Label ledColourLabel { "ledColourLabel", "LedColour" };
  721. TextButton ledColourButton;
  722. Label ledMsToBeOnLabel { "ledMsToBeOnLabel", "LedMsToBeOn" };
  723. ComboBox ledMsToBeOnComboBox;
  724. Label ledMsToBeOffLabel { "ledMsToBeOffLabel", "LedMsToBeOff" };
  725. ComboBox ledMsToBeOffComboBox;
  726. Label vibratorMsToBeOnLabel { "vibratorMsToBeOnLabel", "VibrationMsToBeOn" };
  727. ComboBox vibratorMsToBeOnComboBox;
  728. Label vibratorMsToBeOffLabel { "vibratorMsToBeOffLabel", "VibrationMsToBeOff" };
  729. ComboBox vibratorMsToBeOffComboBox;
  730. Label localOnlyLabel { "localOnlyLabel", "LocalOnly" };
  731. ToggleButton localOnlyButton;
  732. Label ongoingLabel { "ongoingLabel", "Ongoing" };
  733. ToggleButton ongoingButton;
  734. Label timestampVisibilityLabel { "timestampVisibilityLabel", "TimestampMode" };
  735. ComboBox timestampVisibilityComboBox;
  736. Label timeoutAfterLabel { "timeoutAfterLabel", "Timeout After Ms" };
  737. ComboBox timeoutAfterComboBox;
  738. ColourSelector* accentColourSelector = nullptr;
  739. ColourSelector* ledColourSelector = nullptr;
  740. };
  741. void setupControls()
  742. {
  743. auto& pc = paramControls;
  744. StringArray categories { "okCategory", "okCancelCategory", "textCategory" };
  745. for (auto& c : categories)
  746. pc.categoryComboBox.addItem (c, pc.categoryComboBox.getNumItems() + 1);
  747. pc.categoryComboBox.setSelectedItemIndex (0);
  748. for (auto i = 1; i <= 3; ++i)
  749. pc.channelIdComboBox.addItem (String (i), i);
  750. pc.channelIdComboBox.setSelectedItemIndex (0);
  751. for (auto i = 0; i < 5; ++i)
  752. pc.iconComboBox.addItem ("icon" + String (i + 1), i + 1);
  753. pc.iconComboBox.setSelectedItemIndex (0);
  754. #if JUCE_MAC
  755. pc.iconComboBox.addItem ("none", 100);
  756. #endif
  757. pc.fireInComboBox.addItem ("Now", 1);
  758. for (auto i = 1; i < 11; ++i)
  759. pc.fireInComboBox.addItem (String (10 * i) + "seconds", i + 1);
  760. pc.fireInComboBox.setSelectedItemIndex (0);
  761. pc.largeIconComboBox.addItem ("none", 1);
  762. for (auto i = 1; i < 5; ++i)
  763. pc.largeIconComboBox.addItem ("icon" + String (i), i + 1);
  764. pc.largeIconComboBox.setSelectedItemIndex (0);
  765. pc.badgeIconComboBox.addItem ("none", 1);
  766. pc.badgeIconComboBox.addItem ("small", 2);
  767. pc.badgeIconComboBox.addItem ("large", 3);
  768. pc.badgeIconComboBox.setSelectedItemIndex (2);
  769. pc.actionsComboBox.addItem ("none", 1);
  770. pc.actionsComboBox.addItem ("ok-cancel", 2);
  771. pc.actionsComboBox.addItem ("text-input", 3);
  772. #if JUCE_ANDROID
  773. pc.actionsComboBox.addItem ("ok-cancel-icons", 4);
  774. pc.actionsComboBox.addItem ("text-input-limited_responses", 5);
  775. #endif
  776. pc.actionsComboBox.setSelectedItemIndex (0);
  777. for (auto i = 0; i < 7; ++i)
  778. pc.badgeNumberComboBox.addItem (String (i), i + 1);
  779. pc.badgeNumberComboBox.setSelectedItemIndex (0);
  780. #if JUCE_IOS
  781. String prefix = "Notifications/sounds/";
  782. String extension = ".caf";
  783. #else
  784. String prefix;
  785. String extension;
  786. #endif
  787. pc.soundToPlayComboBox.addItem ("none", 1);
  788. pc.soundToPlayComboBox.addItem ("default_os_sound", 2);
  789. pc.soundToPlayComboBox.addItem (prefix + "demonstrative" + extension, 3);
  790. pc.soundToPlayComboBox.addItem (prefix + "isntit" + extension, 4);
  791. pc.soundToPlayComboBox.addItem (prefix + "jinglebellssms" + extension, 5);
  792. pc.soundToPlayComboBox.addItem (prefix + "served" + extension, 6);
  793. pc.soundToPlayComboBox.addItem (prefix + "solemn" + extension, 7);
  794. pc.soundToPlayComboBox.setSelectedItemIndex (1);
  795. for (auto i = 0; i < 11; ++i)
  796. {
  797. pc.progressMaxComboBox .addItem (String (i * 10) + "%", i + 1);
  798. pc.progressCurrentComboBox.addItem (String (i * 10) + "%", i + 1);
  799. }
  800. pc.progressMaxComboBox .setSelectedItemIndex (0);
  801. pc.progressCurrentComboBox.setSelectedItemIndex (0);
  802. pc.notifCategoryComboBox.addItem ("unspecified", 1);
  803. pc.notifCategoryComboBox.addItem ("alarm", 2);
  804. pc.notifCategoryComboBox.addItem ("call", 3);
  805. pc.notifCategoryComboBox.addItem ("email", 4);
  806. pc.notifCategoryComboBox.addItem ("error", 5);
  807. pc.notifCategoryComboBox.addItem ("event", 6);
  808. pc.notifCategoryComboBox.addItem ("message", 7);
  809. pc.notifCategoryComboBox.addItem ("progress", 8);
  810. pc.notifCategoryComboBox.addItem ("promo", 9);
  811. pc.notifCategoryComboBox.addItem ("recommendation", 10);
  812. pc.notifCategoryComboBox.addItem ("reminder", 11);
  813. pc.notifCategoryComboBox.addItem ("service", 12);
  814. pc.notifCategoryComboBox.addItem ("social", 13);
  815. pc.notifCategoryComboBox.addItem ("status", 14);
  816. pc.notifCategoryComboBox.addItem ("system", 15);
  817. pc.notifCategoryComboBox.addItem ("transport", 16);
  818. pc.notifCategoryComboBox.setSelectedItemIndex (0);
  819. for (auto i = -2; i < 3; ++i)
  820. pc.priorityComboBox.addItem (String (i), i + 3);
  821. pc.priorityComboBox.setSelectedItemIndex (2);
  822. pc.lockScreenVisibilityComboBox.addItem ("don't show", 1);
  823. pc.lockScreenVisibilityComboBox.addItem ("show partially", 2);
  824. pc.lockScreenVisibilityComboBox.addItem ("show completely", 3);
  825. pc.lockScreenVisibilityComboBox.setSelectedItemIndex (1);
  826. pc.groupAlertBehaviourComboBox.addItem ("alert all", 1);
  827. pc.groupAlertBehaviourComboBox.addItem ("alert summary", 2);
  828. pc.groupAlertBehaviourComboBox.addItem ("alert children", 3);
  829. pc.groupAlertBehaviourComboBox.setSelectedItemIndex (0);
  830. pc.timeoutAfterComboBox.addItem ("No timeout", 1);
  831. for (auto i = 0; i < 10; ++i)
  832. {
  833. pc.ledMsToBeOnComboBox .addItem (String (i * 200) + "ms", i + 1);
  834. pc.ledMsToBeOffComboBox .addItem (String (i * 200) + "ms", i + 1);
  835. pc.vibratorMsToBeOnComboBox .addItem (String (i * 500) + "ms", i + 1);
  836. pc.vibratorMsToBeOffComboBox.addItem (String (i * 500) + "ms", i + 1);
  837. pc.timeoutAfterComboBox .addItem (String (5000 + 1000 * i) + "ms", i + 2);
  838. }
  839. pc.ledMsToBeOnComboBox .setSelectedItemIndex (5);
  840. pc.ledMsToBeOffComboBox .setSelectedItemIndex (5);
  841. pc.vibratorMsToBeOnComboBox .setSelectedItemIndex (0);
  842. pc.vibratorMsToBeOffComboBox.setSelectedItemIndex (0);
  843. pc.timeoutAfterComboBox .setSelectedItemIndex (0);
  844. pc.timestampVisibilityComboBox.addItem ("off", 1);
  845. pc.timestampVisibilityComboBox.addItem ("on", 2);
  846. pc.timestampVisibilityComboBox.addItem ("chronometer", 3);
  847. pc.timestampVisibilityComboBox.addItem ("count down", 4);
  848. pc.timestampVisibilityComboBox.setSelectedItemIndex (1);
  849. }
  850. void distributeControls()
  851. {
  852. auto& pc = paramControls;
  853. paramsOneView .addRowComponent (new RowComponent (pc.identifierLabel, pc.identifierEditor));
  854. paramsOneView .addRowComponent (new RowComponent (pc.titleLabel, pc.titleEditor));
  855. paramsOneView .addRowComponent (new RowComponent (pc.bodyLabel, pc.bodyEditor, 4));
  856. #if JUCE_IOS
  857. paramsOneView .addRowComponent (new RowComponent (pc.categoryLabel, pc.categoryComboBox));
  858. #elif JUCE_ANDROID
  859. paramsOneView .addRowComponent (new RowComponent (pc.channelIdLabel, pc.channelIdComboBox));
  860. #endif
  861. #if JUCE_ANDROID || JUCE_MAC
  862. paramsOneView .addRowComponent (new RowComponent (pc.iconLabel, pc.iconComboBox));
  863. #endif
  864. paramsTwoView .addRowComponent (new RowComponent (pc.subtitleLabel, pc.subtitleEditor));
  865. #if ! JUCE_MAC
  866. paramsTwoView .addRowComponent (new RowComponent (pc.badgeNumberLabel, pc.badgeNumberComboBox));
  867. #endif
  868. paramsTwoView .addRowComponent (new RowComponent (pc.soundToPlayLabel, pc.soundToPlayComboBox));
  869. paramsTwoView .addRowComponent (new RowComponent (pc.propertiesLabel, pc.propertiesEditor, 3));
  870. #if JUCE_IOS || JUCE_MAC
  871. paramsTwoView .addRowComponent (new RowComponent (pc.fireInLabel, pc.fireInComboBox));
  872. paramsTwoView .addRowComponent (new RowComponent (pc.repeatLabel, pc.repeatButton));
  873. #elif JUCE_ANDROID
  874. paramsTwoView .addRowComponent (new RowComponent (pc.largeIconLabel, pc.largeIconComboBox));
  875. paramsTwoView .addRowComponent (new RowComponent (pc.badgeIconLabel, pc.badgeIconComboBox));
  876. paramsTwoView .addRowComponent (new RowComponent (pc.tickerTextLabel, pc.tickerTextEditor));
  877. paramsTwoView .addRowComponent (new RowComponent (pc.autoCancelLabel, pc.autoCancelButton));
  878. paramsTwoView .addRowComponent (new RowComponent (pc.alertOnlyOnceLabel, pc.alertOnlyOnceButton));
  879. #endif
  880. #if JUCE_ANDROID || JUCE_MAC
  881. paramsTwoView .addRowComponent (new RowComponent (pc.actionsLabel, pc.actionsComboBox));
  882. #endif
  883. #if JUCE_ANDROID
  884. paramsThreeView.addRowComponent (new RowComponent (pc.progressMaxLabel, pc.progressMaxComboBox));
  885. paramsThreeView.addRowComponent (new RowComponent (pc.progressCurrentLabel, pc.progressCurrentComboBox));
  886. paramsThreeView.addRowComponent (new RowComponent (pc.progressIndeterminateLabel, pc.progressIndeterminateButton));
  887. paramsThreeView.addRowComponent (new RowComponent (pc.categoryLabel, pc.categoryComboBox));
  888. paramsThreeView.addRowComponent (new RowComponent (pc.priorityLabel, pc.priorityComboBox));
  889. paramsThreeView.addRowComponent (new RowComponent (pc.personLabel, pc.personEditor));
  890. paramsThreeView.addRowComponent (new RowComponent (pc.lockScreenVisibilityLabel, pc.lockScreenVisibilityComboBox));
  891. paramsThreeView.addRowComponent (new RowComponent (pc.groupIdLabel, pc.groupIdEditor));
  892. paramsThreeView.addRowComponent (new RowComponent (pc.sortKeyLabel, pc.sortKeyEditor));
  893. paramsThreeView.addRowComponent (new RowComponent (pc.groupSummaryLabel, pc.groupSummaryButton));
  894. paramsThreeView.addRowComponent (new RowComponent (pc.groupAlertBehaviourLabel, pc.groupAlertBehaviourComboBox));
  895. paramsFourView .addRowComponent (new RowComponent (pc.accentColourLabel, pc.accentColourButton));
  896. paramsFourView .addRowComponent (new RowComponent (pc.ledColourLabel, pc.ledColourButton));
  897. paramsFourView .addRowComponent (new RowComponent (pc.ledMsToBeOffLabel, pc.ledMsToBeOffComboBox));
  898. paramsFourView .addRowComponent (new RowComponent (pc.ledMsToBeOnLabel, pc.ledMsToBeOnComboBox));
  899. paramsFourView .addRowComponent (new RowComponent (pc.vibratorMsToBeOffLabel, pc.vibratorMsToBeOffComboBox));
  900. paramsFourView .addRowComponent (new RowComponent (pc.vibratorMsToBeOnLabel, pc.vibratorMsToBeOnComboBox));
  901. paramsFourView .addRowComponent (new RowComponent (pc.localOnlyLabel, pc.localOnlyButton));
  902. paramsFourView .addRowComponent (new RowComponent (pc.ongoingLabel, pc.ongoingButton));
  903. paramsFourView .addRowComponent (new RowComponent (pc.timestampVisibilityLabel, pc.timestampVisibilityComboBox));
  904. paramsFourView .addRowComponent (new RowComponent (pc.timeoutAfterLabel, pc.timeoutAfterComboBox));
  905. #endif
  906. }
  907. struct ParamsView : public Component
  908. {
  909. ParamsView()
  910. {
  911. // For now, to be able to dismiss mobile keyboard.
  912. setWantsKeyboardFocus (true);
  913. }
  914. void addRowComponent (RowComponent* rc)
  915. {
  916. rowComponents.add (rc);
  917. addAndMakeVisible (rc);
  918. }
  919. void resized() override
  920. {
  921. auto totalRowUnits = 0;
  922. for (auto* rc : rowComponents)
  923. totalRowUnits += rc->rowUnits;
  924. auto rowHeight = getHeight() / totalRowUnits;
  925. auto bounds = getLocalBounds();
  926. for (auto* rc : rowComponents)
  927. rc->setBounds (bounds.removeFromTop (rc->rowUnits * rowHeight));
  928. auto* last = rowComponents[rowComponents.size() - 1];
  929. last->setBounds (last->getBounds().withHeight (getHeight() - last->getY()));
  930. }
  931. private:
  932. OwnedArray<RowComponent> rowComponents;
  933. };
  934. struct AuxActionsView : public Component
  935. {
  936. AuxActionsView()
  937. {
  938. addAndMakeVisible (getDeliveredNotificationsButton);
  939. addAndMakeVisible (removeDeliveredNotifWithIdButton);
  940. addAndMakeVisible (deliveredNotifIdentifier);
  941. addAndMakeVisible (removeAllDeliveredNotifsButton);
  942. #if JUCE_IOS || JUCE_MAC
  943. addAndMakeVisible (getPendingNotificationsButton);
  944. addAndMakeVisible (removePendingNotifWithIdButton);
  945. addAndMakeVisible (pendingNotifIdentifier);
  946. addAndMakeVisible (removeAllPendingNotifsButton);
  947. #endif
  948. // For now, to be able to dismiss mobile keyboard.
  949. setWantsKeyboardFocus (true);
  950. }
  951. void resized() override
  952. {
  953. auto columnWidth = getWidth();
  954. auto rowHeight = getHeight() / 6;
  955. auto bounds = getLocalBounds();
  956. getDeliveredNotificationsButton .setBounds (bounds.removeFromTop (rowHeight));
  957. auto rowBounds = bounds.removeFromTop (rowHeight);
  958. removeDeliveredNotifWithIdButton.setBounds (rowBounds.removeFromLeft (columnWidth / 2));
  959. deliveredNotifIdentifier .setBounds (rowBounds);
  960. removeAllDeliveredNotifsButton .setBounds (bounds.removeFromTop (rowHeight));
  961. #if JUCE_IOS || JUCE_MAC
  962. getPendingNotificationsButton .setBounds (bounds.removeFromTop (rowHeight));
  963. rowBounds = bounds.removeFromTop (rowHeight);
  964. removePendingNotifWithIdButton.setBounds (rowBounds.removeFromLeft (columnWidth / 2));
  965. pendingNotifIdentifier .setBounds (rowBounds);
  966. removeAllPendingNotifsButton .setBounds (bounds.removeFromTop (rowHeight));
  967. #endif
  968. }
  969. TextButton getDeliveredNotificationsButton { "Get Delivered Notifications" };
  970. TextButton removeDeliveredNotifWithIdButton { "Remove Delivered Notif With ID:" };
  971. TextEditor deliveredNotifIdentifier;
  972. TextButton removeAllDeliveredNotifsButton { "Remove All Delivered Notifs" };
  973. TextButton getPendingNotificationsButton { "Get Pending Notifications" };
  974. TextButton removePendingNotifWithIdButton { "Remove Pending Notif With ID:" };
  975. TextEditor pendingNotifIdentifier;
  976. TextButton removeAllPendingNotifsButton { "Remove All Pending Notifs" };
  977. };
  978. struct RemoteView : public Component
  979. {
  980. RemoteView()
  981. {
  982. addAndMakeVisible (getDeviceTokenButton);
  983. #if JUCE_ANDROID
  984. addAndMakeVisible (sendRemoteMessageButton);
  985. addAndMakeVisible (subscribeToSportsButton);
  986. addAndMakeVisible (unsubscribeFromSportsButton);
  987. #endif
  988. }
  989. void resized()
  990. {
  991. auto rowSize = getHeight () / 10;
  992. auto bounds = getLocalBounds().reduced (getWidth() / 10, getHeight() / 10);
  993. bounds.removeFromTop (2 * rowSize);
  994. getDeviceTokenButton .setBounds (bounds.removeFromTop (rowSize));
  995. sendRemoteMessageButton .setBounds (bounds.removeFromTop (rowSize));
  996. subscribeToSportsButton .setBounds (bounds.removeFromTop (rowSize));
  997. unsubscribeFromSportsButton.setBounds (bounds.removeFromTop (rowSize));
  998. }
  999. TextButton getDeviceTokenButton { "GetDeviceToken" };
  1000. TextButton sendRemoteMessageButton { "SendRemoteMessage" };
  1001. TextButton subscribeToSportsButton { "SubscribeToSports" };
  1002. TextButton unsubscribeFromSportsButton { "UnsubscribeFromSports" };
  1003. };
  1004. struct DemoTabbedComponent : public TabbedComponent
  1005. {
  1006. explicit DemoTabbedComponent (TabbedButtonBar::Orientation orientation)
  1007. : TabbedComponent (orientation)
  1008. {
  1009. }
  1010. void currentTabChanged (int, const String& newCurrentTabName) override
  1011. {
  1012. if (! showedRemoteInstructions && newCurrentTabName == "Remote")
  1013. {
  1014. PushNotificationsDemo::showRemoteInstructions();
  1015. showedRemoteInstructions = true;
  1016. }
  1017. }
  1018. private:
  1019. bool showedRemoteInstructions = false;
  1020. };
  1021. static void showRemoteInstructions()
  1022. {
  1023. #if JUCE_IOS || JUCE_MAC
  1024. NativeMessageBox::showAsync (MessageBoxOptions()
  1025. .withIconType (MessageBoxIconType::InfoIcon)
  1026. .withTitle ("Remote Notifications instructions")
  1027. .withMessage ("In order to be able to test remote notifications "
  1028. "ensure that the app is signed and that you register "
  1029. "the bundle ID for remote notifications in "
  1030. "Apple Developer Center."),
  1031. nullptr);
  1032. #endif
  1033. }
  1034. Label headerLabel { "headerLabel", "Push Notifications Demo" };
  1035. ParamControls paramControls;
  1036. ParamsView paramsOneView, paramsTwoView, paramsThreeView, paramsFourView;
  1037. AuxActionsView auxActionsView;
  1038. TabbedComponent localNotificationsTabs { TabbedButtonBar::TabsAtTop };
  1039. RemoteView remoteView;
  1040. DemoTabbedComponent mainTabs { TabbedButtonBar::TabsAtTop };
  1041. TextButton sendButton { "Send!" };
  1042. Label notAvailableYetLabel { "notAvailableYetLabel",
  1043. "Push Notifications feature is not available on this platform yet!" };
  1044. //==============================================================================
  1045. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PushNotificationsDemo)
  1046. };