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.

1217 lines
55KB

  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::showMessageBoxAsync (AlertWindow::InfoIcon, "Device token", token);
  156. };
  157. #if JUCE_ANDROID
  158. remoteView.sendRemoteMessageButton.onClick = []
  159. {
  160. StringPairArray data;
  161. data.set ("key1", "value1");
  162. data.set ("key2", "value2");
  163. static int id = 100;
  164. PushNotifications::getInstance()->sendUpstreamMessage ("872047750958",
  165. "com.juce.pushnotificationsdemo",
  166. String (id++),
  167. "standardType",
  168. 3600,
  169. data);
  170. };
  171. remoteView.subscribeToSportsButton .onClick = []
  172. { PushNotifications::getInstance()->subscribeToTopic ("sports"); };
  173. remoteView.unsubscribeFromSportsButton.onClick = []
  174. { PushNotifications::getInstance()->unsubscribeFromTopic ("sports"); };
  175. #endif
  176. paramControls.accentColourButton.onClick = [this] { setupAccentColour(); };
  177. paramControls.ledColourButton .onClick = [this] { setupLedColour(); };
  178. jassert (PushNotifications::getInstance()->areNotificationsEnabled());
  179. PushNotifications::getInstance()->addListener (this);
  180. #if JUCE_IOS || JUCE_MAC
  181. paramControls.fireInComboBox.onChange = [this] { delayNotification(); };
  182. PushNotifications::getInstance()->requestPermissionsWithSettings (getNotificationSettings());
  183. #elif JUCE_ANDROID
  184. PushNotifications::ChannelGroup cg { "demoGroup", "demo group" };
  185. PushNotifications::getInstance()->setupChannels ({ { cg } }, getAndroidChannels());
  186. #endif
  187. #if JUCE_IOS || JUCE_ANDROID
  188. setPortraitOrientationEnabled (true);
  189. #endif
  190. }
  191. ~PushNotificationsDemo() override
  192. {
  193. PushNotifications::getInstance()->removeListener (this);
  194. #if JUCE_IOS || JUCE_ANDROID
  195. setPortraitOrientationEnabled (false);
  196. #endif
  197. }
  198. void setPortraitOrientationEnabled (bool shouldBeEnabled)
  199. {
  200. auto allowedOrientations = Desktop::getInstance().getOrientationsEnabled();
  201. if (shouldBeEnabled)
  202. allowedOrientations |= Desktop::upright;
  203. else
  204. allowedOrientations &= ~Desktop::upright;
  205. Desktop::getInstance().setOrientationsEnabled (allowedOrientations);
  206. }
  207. void paint (Graphics& g) override
  208. {
  209. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  210. }
  211. void resized() override
  212. {
  213. auto bounds = getLocalBounds().reduced (getWidth() / 20, getHeight() / 40);
  214. headerLabel.setBounds (bounds.removeFromTop (bounds.proportionOfHeight (0.1f)));
  215. mainTabs .setBounds (bounds.removeFromTop (bounds.proportionOfHeight (0.8f)));
  216. sendButton .setBounds (bounds);
  217. notAvailableYetLabel.setBounds (getLocalBounds());
  218. }
  219. private:
  220. void delayNotification()
  221. {
  222. auto repeatsAllowed = paramControls.fireInComboBox.getSelectedItemIndex() >= 6;
  223. paramControls.repeatButton.setEnabled (repeatsAllowed);
  224. if (! repeatsAllowed)
  225. paramControls.repeatButton.setToggleState (false, NotificationType::sendNotification);
  226. }
  227. void sendLocalNotification()
  228. {
  229. PushNotifications::Notification n;
  230. fillRequiredParams (n);
  231. fillOptionalParamsOne (n);
  232. #if JUCE_ANDROID
  233. fillOptionalParamsTwo (n);
  234. fillOptionalParamsThree (n);
  235. #endif
  236. if (! n.isValid())
  237. {
  238. #if JUCE_IOS
  239. String requiredFields = "identifier (from iOS 10), title, body and category";
  240. #elif JUCE_ANDROID
  241. String requiredFields = "channel ID (from Android O), title, body and icon";
  242. #else
  243. String requiredFields = "all required fields";
  244. #endif
  245. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  246. "Incorrect notifications setup",
  247. "Please make sure that "
  248. + requiredFields + " are set.");
  249. return;
  250. }
  251. PushNotifications::getInstance()->sendLocalNotification (n);
  252. }
  253. void fillRequiredParams (PushNotifications::Notification& n)
  254. {
  255. n.identifier = paramControls.identifierEditor.getText();
  256. n.title = paramControls.titleEditor .getText();
  257. n.body = paramControls.bodyEditor .getText();
  258. #if JUCE_IOS
  259. n.category = paramControls.categoryComboBox.getText();
  260. #elif JUCE_ANDROID || JUCE_MAC
  261. #if JUCE_MAC
  262. String prefix = "Notifications/images/";
  263. String extension = ".png";
  264. #else
  265. String prefix;
  266. String extension;
  267. #endif
  268. if (paramControls.iconComboBox.getSelectedItemIndex() == 0)
  269. n.icon = prefix + "ic_stat_name" + extension;
  270. else if (paramControls.iconComboBox.getSelectedItemIndex() == 1)
  271. n.icon = prefix + "ic_stat_name2" + extension;
  272. else if (paramControls.iconComboBox.getSelectedItemIndex() == 2)
  273. n.icon = prefix + "ic_stat_name3" + extension;
  274. else if (paramControls.iconComboBox.getSelectedItemIndex() == 3)
  275. n.icon = prefix + "ic_stat_name4" + extension;
  276. else if (paramControls.iconComboBox.getSelectedItemIndex() == 4)
  277. n.icon = prefix + "ic_stat_name5" + extension;
  278. #endif
  279. #if JUCE_ANDROID
  280. // Note: this is not strictly speaking required param, just doing it here because it is the fastest way!
  281. n.publicVersion.reset (new PushNotifications::Notification());
  282. n.publicVersion->identifier = "blahblahblah";
  283. n.publicVersion->title = "Public title!";
  284. n.publicVersion->body = "Public body!";
  285. n.publicVersion->icon = n.icon;
  286. n.channelId = String (paramControls.channelIdComboBox.getSelectedItemIndex() + 1);
  287. #endif
  288. }
  289. void fillOptionalParamsOne (PushNotifications::Notification& n)
  290. {
  291. n.subtitle = paramControls.subtitleEditor.getText();
  292. n.badgeNumber = paramControls.badgeNumberComboBox.getSelectedItemIndex();
  293. if (paramControls.soundToPlayComboBox.getSelectedItemIndex() > 0)
  294. n.soundToPlay = URL (paramControls.soundToPlayComboBox.getItemText (paramControls.soundToPlayComboBox.getSelectedItemIndex()));
  295. n.properties = JSON::parse (paramControls.propertiesEditor.getText());
  296. #if JUCE_IOS || JUCE_MAC
  297. n.triggerIntervalSec = double (paramControls.fireInComboBox.getSelectedItemIndex() * 10);
  298. n.repeat = paramControls.repeatButton.getToggleState();
  299. #elif JUCE_ANDROID
  300. if (paramControls.largeIconComboBox.getSelectedItemIndex() == 1)
  301. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name6.png");
  302. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 2)
  303. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name7.png");
  304. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 3)
  305. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name8.png");
  306. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 4)
  307. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name9.png");
  308. else if (paramControls.largeIconComboBox.getSelectedItemIndex() == 5)
  309. n.largeIcon = getImageFromAssets ("Notifications/images/ic_stat_name10.png");
  310. n.badgeIconType = (PushNotifications::Notification::BadgeIconType) paramControls.badgeIconComboBox.getSelectedItemIndex();
  311. n.tickerText = paramControls.tickerTextEditor.getText();
  312. n.shouldAutoCancel = paramControls.autoCancelButton .getToggleState();
  313. n.alertOnlyOnce = paramControls.alertOnlyOnceButton.getToggleState();
  314. #endif
  315. #if JUCE_ANDROID || JUCE_MAC
  316. if (paramControls.actionsComboBox.getSelectedItemIndex() == 1)
  317. {
  318. PushNotifications::Notification::Action a, a2;
  319. a .style = PushNotifications::Notification::Action::button;
  320. a2.style = PushNotifications::Notification::Action::button;
  321. a .title = a .identifier = "Ok";
  322. a2.title = a2.identifier = "Cancel";
  323. n.actions.add (a);
  324. n.actions.add (a2);
  325. }
  326. else if (paramControls.actionsComboBox.getSelectedItemIndex() == 2)
  327. {
  328. PushNotifications::Notification::Action a, a2;
  329. a .title = a .identifier = "Input Text Here";
  330. a2.title = a2.identifier = "No";
  331. a .style = PushNotifications::Notification::Action::text;
  332. a2.style = PushNotifications::Notification::Action::button;
  333. a .icon = "ic_stat_name4";
  334. a2.icon = "ic_stat_name5";
  335. a.textInputPlaceholder = "placeholder text ...";
  336. n.actions.add (a);
  337. n.actions.add (a2);
  338. }
  339. else if (paramControls.actionsComboBox.getSelectedItemIndex() == 3)
  340. {
  341. PushNotifications::Notification::Action a, a2;
  342. a .title = a .identifier = "Ok";
  343. a2.title = a2.identifier = "Cancel";
  344. a .style = PushNotifications::Notification::Action::button;
  345. a2.style = PushNotifications::Notification::Action::button;
  346. a .icon = "ic_stat_name4";
  347. a2.icon = "ic_stat_name5";
  348. n.actions.add (a);
  349. n.actions.add (a2);
  350. }
  351. else if (paramControls.actionsComboBox.getSelectedItemIndex() == 4)
  352. {
  353. PushNotifications::Notification::Action a, a2;
  354. a .title = a .identifier = "Input Text Here";
  355. a2.title = a2.identifier = "No";
  356. a .style = PushNotifications::Notification::Action::text;
  357. a2.style = PushNotifications::Notification::Action::button;
  358. a .icon = "ic_stat_name4";
  359. a2.icon = "ic_stat_name5";
  360. a.textInputPlaceholder = "placeholder text ...";
  361. a.allowedResponses.add ("Response 1");
  362. a.allowedResponses.add ("Response 2");
  363. a.allowedResponses.add ("Response 3");
  364. n.actions.add (a);
  365. n.actions.add (a2);
  366. }
  367. #endif
  368. }
  369. void fillOptionalParamsTwo (PushNotifications::Notification& n)
  370. {
  371. using Notification = PushNotifications::Notification;
  372. Notification::Progress progress;
  373. progress.max = paramControls.progressMaxComboBox .getSelectedItemIndex() * 10;
  374. progress.current = paramControls.progressCurrentComboBox.getSelectedItemIndex() * 10;
  375. progress.indeterminate = paramControls.progressIndeterminateButton.getToggleState();
  376. n.progress = progress;
  377. n.person = paramControls.personEditor.getText();
  378. n.type = Notification::Type (paramControls.categoryComboBox .getSelectedItemIndex());
  379. n.priority = Notification::Priority (paramControls.priorityComboBox .getSelectedItemIndex() - 2);
  380. n.lockScreenAppearance = Notification::LockScreenAppearance (paramControls.lockScreenVisibilityComboBox.getSelectedItemIndex() - 1);
  381. n.groupId = paramControls.groupIdEditor.getText();
  382. n.groupSortKey = paramControls.sortKeyEditor.getText();
  383. n.groupSummary = paramControls.groupSummaryButton.getToggleState();
  384. n.groupAlertBehaviour = Notification::GroupAlertBehaviour (paramControls.groupAlertBehaviourComboBox.getSelectedItemIndex());
  385. }
  386. void fillOptionalParamsThree (PushNotifications::Notification& n)
  387. {
  388. n.accentColour = paramControls.accentColourButton.findColour (TextButton::buttonColourId, false);
  389. n.ledColour = paramControls.ledColourButton .findColour (TextButton::buttonColourId, false);
  390. using Notification = PushNotifications::Notification;
  391. Notification::LedBlinkPattern ledBlinkPattern;
  392. ledBlinkPattern.msToBeOn = paramControls.ledMsToBeOnComboBox .getSelectedItemIndex() * 200;
  393. ledBlinkPattern.msToBeOff = paramControls.ledMsToBeOffComboBox.getSelectedItemIndex() * 200;
  394. n.ledBlinkPattern = ledBlinkPattern;
  395. Array<int> vibrationPattern;
  396. if (paramControls.vibratorMsToBeOnComboBox .getSelectedItemIndex() > 0 &&
  397. paramControls.vibratorMsToBeOffComboBox.getSelectedItemIndex() > 0)
  398. {
  399. vibrationPattern.add (paramControls.vibratorMsToBeOffComboBox.getSelectedItemIndex() * 500);
  400. vibrationPattern.add (paramControls.vibratorMsToBeOnComboBox .getSelectedItemIndex() * 500);
  401. vibrationPattern.add (2 * paramControls.vibratorMsToBeOffComboBox.getSelectedItemIndex() * 500);
  402. vibrationPattern.add (2 * paramControls.vibratorMsToBeOnComboBox .getSelectedItemIndex() * 500);
  403. }
  404. n.vibrationPattern = vibrationPattern;
  405. n.localOnly = paramControls.localOnlyButton.getToggleState();
  406. n.ongoing = paramControls.ongoingButton.getToggleState();
  407. n.timestampVisibility = Notification::TimestampVisibility (paramControls.timestampVisibilityComboBox.getSelectedItemIndex());
  408. if (paramControls.timeoutAfterComboBox.getSelectedItemIndex() > 0)
  409. {
  410. auto index = paramControls.timeoutAfterComboBox.getSelectedItemIndex();
  411. n.timeoutAfterMs = index * 1000 + 4000;
  412. }
  413. }
  414. void setupAccentColour()
  415. {
  416. auto accentColourSelector = std::make_unique<ColourSelector>();
  417. accentColourSelector->setName ("accent colour");
  418. accentColourSelector->setCurrentColour (paramControls.accentColourButton.findColour (TextButton::buttonColourId));
  419. accentColourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  420. accentColourSelector->setSize (200, 200);
  421. accentColourSelector->addComponentListener (this);
  422. accentColourSelector->addChangeListener (this);
  423. paramControls.accentColourSelector = accentColourSelector.get();
  424. CallOutBox::launchAsynchronously (std::move (accentColourSelector), paramControls.accentColourButton.getScreenBounds(), nullptr);
  425. }
  426. void setupLedColour()
  427. {
  428. auto ledColourSelector = std::make_unique<ColourSelector>();
  429. ledColourSelector->setName ("led colour");
  430. ledColourSelector->setCurrentColour (paramControls.ledColourButton.findColour (TextButton::buttonColourId));
  431. ledColourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  432. ledColourSelector->setSize (200, 200);
  433. ledColourSelector->addComponentListener (this);
  434. ledColourSelector->addChangeListener (this);
  435. paramControls.ledColourSelector = ledColourSelector.get();
  436. CallOutBox::launchAsynchronously (std::move (ledColourSelector), paramControls.accentColourButton.getScreenBounds(), nullptr);
  437. }
  438. void changeListenerCallback (ChangeBroadcaster* source) override
  439. {
  440. if (source == paramControls.accentColourSelector)
  441. {
  442. auto c = paramControls.accentColourSelector->getCurrentColour();
  443. paramControls.accentColourButton.setColour (TextButton::buttonColourId, c);
  444. }
  445. else if (source == paramControls.ledColourSelector)
  446. {
  447. auto c = paramControls.ledColourSelector->getCurrentColour();
  448. paramControls.ledColourButton.setColour (TextButton::buttonColourId, c);
  449. }
  450. }
  451. void componentBeingDeleted (Component& component) override
  452. {
  453. if (&component == paramControls.accentColourSelector)
  454. paramControls.accentColourSelector = nullptr;
  455. else if (&component == paramControls.ledColourSelector)
  456. paramControls.ledColourSelector = nullptr;
  457. }
  458. void handleNotification (bool isLocalNotification, const PushNotifications::Notification& n) override
  459. {
  460. ignoreUnused (isLocalNotification);
  461. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  462. "Received notification",
  463. "ID: " + n.identifier
  464. + ", title: " + n.title
  465. + ", body: " + n.body);
  466. }
  467. void handleNotificationAction (bool isLocalNotification,
  468. const PushNotifications::Notification& n,
  469. const juce::String& actionIdentifier,
  470. const juce::String& optionalResponse) override
  471. {
  472. ignoreUnused (isLocalNotification);
  473. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  474. "Received notification action",
  475. "ID: " + n.identifier
  476. + ", title: " + n.title
  477. + ", body: " + n.body
  478. + ", action: " + actionIdentifier
  479. + ", optionalResponse: " + optionalResponse);
  480. PushNotifications::getInstance()->removeDeliveredNotification (n.identifier);
  481. }
  482. void localNotificationDismissedByUser (const PushNotifications::Notification& n) override
  483. {
  484. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  485. "Notification dismissed by a user",
  486. "ID: " + n.identifier
  487. + ", title: " + n.title
  488. + ", body: " + n.body);
  489. }
  490. void deliveredNotificationsListReceived (const Array<PushNotifications::Notification>& notifs) override
  491. {
  492. String text = "Received notifications: ";
  493. for (auto& n : notifs)
  494. text << "(" << n.identifier << ", " << n.title << ", " << n.body << "), ";
  495. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon, "Received notification list", text);
  496. }
  497. void pendingLocalNotificationsListReceived (const Array<PushNotifications::Notification>& notifs) override
  498. {
  499. String text = "Pending notifications: ";
  500. for (auto& n : notifs)
  501. text << "(" << n.identifier << ", " << n.title << ", " << n.body << "), ";
  502. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon, "Pending notification list", text);
  503. }
  504. void deviceTokenRefreshed (const String& token) override
  505. {
  506. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  507. "Device token refreshed",
  508. token);
  509. }
  510. #if JUCE_ANDROID
  511. void remoteNotificationsDeleted() override
  512. {
  513. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  514. "Remote notifications deleted",
  515. "Some of the pending messages were removed!");
  516. }
  517. void upstreamMessageSent (const String& messageId) override
  518. {
  519. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  520. "Upstream message sent",
  521. "Message id: " + messageId);
  522. }
  523. void upstreamMessageSendingError (const String& messageId, const String& error) override
  524. {
  525. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  526. "Upstream message sending error",
  527. "Message id: " + messageId
  528. + "\nerror: " + error);
  529. }
  530. static Array<PushNotifications::Channel> getAndroidChannels()
  531. {
  532. using Channel = PushNotifications::Channel;
  533. Channel ch1, ch2, ch3;
  534. ch1.identifier = "1";
  535. ch1.name = "HighImportance";
  536. ch1.importance = PushNotifications::Channel::max;
  537. ch1.lockScreenAppearance = PushNotifications::Notification::showCompletely;
  538. ch1.description = "High Priority Channel for important stuff";
  539. ch1.groupId = "demoGroup";
  540. ch1.ledColour = Colours::red;
  541. ch1.bypassDoNotDisturb = true;
  542. ch1.canShowBadge = true;
  543. ch1.enableLights = true;
  544. ch1.enableVibration = true;
  545. ch1.soundToPlay = URL ("demonstrative");
  546. ch1.vibrationPattern = { 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 };
  547. ch2.identifier = "2";
  548. ch2.name = "MediumImportance";
  549. ch2.importance = PushNotifications::Channel::normal;
  550. ch2.lockScreenAppearance = PushNotifications::Notification::showPartially;
  551. ch2.description = "Medium Priority Channel for standard stuff";
  552. ch2.groupId = "demoGroup";
  553. ch2.ledColour = Colours::yellow;
  554. ch2.canShowBadge = true;
  555. ch2.enableLights = true;
  556. ch2.enableVibration = true;
  557. ch2.soundToPlay = URL ("default_os_sound");
  558. ch2.vibrationPattern = { 1000, 1000 };
  559. ch3.identifier = "3";
  560. ch3.name = "LowImportance";
  561. ch3.importance = PushNotifications::Channel::min;
  562. ch3.lockScreenAppearance = PushNotifications::Notification::dontShow;
  563. ch3.description = "Low Priority Channel for silly stuff";
  564. ch3.groupId = "demoGroup";
  565. return { ch1, ch2, ch3 };
  566. }
  567. #elif JUCE_IOS || JUCE_MAC
  568. static PushNotifications::Settings getNotificationSettings()
  569. {
  570. PushNotifications::Settings settings;
  571. settings.allowAlert = true;
  572. settings.allowBadge = true;
  573. settings.allowSound = true;
  574. #if JUCE_IOS
  575. using Action = PushNotifications::Settings::Action;
  576. using Category = PushNotifications::Settings::Category;
  577. Action okAction;
  578. okAction.identifier = "okAction";
  579. okAction.title = "OK!";
  580. okAction.style = Action::button;
  581. okAction.triggerInBackground = true;
  582. Action cancelAction;
  583. cancelAction.identifier = "cancelAction";
  584. cancelAction.title = "Cancel";
  585. cancelAction.style = Action::button;
  586. cancelAction.triggerInBackground = true;
  587. cancelAction.destructive = true;
  588. Action textAction;
  589. textAction.identifier = "textAction";
  590. textAction.title = "Enter text";
  591. textAction.style = Action::text;
  592. textAction.triggerInBackground = true;
  593. textAction.destructive = false;
  594. textAction.textInputButtonText = "Ok";
  595. textAction.textInputPlaceholder = "Enter text...";
  596. Category okCategory;
  597. okCategory.identifier = "okCategory";
  598. okCategory.actions = { okAction };
  599. Category okCancelCategory;
  600. okCancelCategory.identifier = "okCancelCategory";
  601. okCancelCategory.actions = { okAction, cancelAction };
  602. Category textCategory;
  603. textCategory.identifier = "textCategory";
  604. textCategory.actions = { textAction };
  605. textCategory.sendDismissAction = true;
  606. settings.categories = { okCategory, okCancelCategory, textCategory };
  607. #endif
  608. return settings;
  609. }
  610. #endif
  611. struct RowComponent : public Component
  612. {
  613. RowComponent (Label& l, Component& c, int u = 1)
  614. : label (l),
  615. editor (c),
  616. rowUnits (u)
  617. {
  618. addAndMakeVisible (label);
  619. addAndMakeVisible (editor);
  620. }
  621. void resized() override
  622. {
  623. auto bounds = getLocalBounds();
  624. label .setBounds (bounds.removeFromLeft (getWidth() / 3));
  625. editor.setBounds (bounds);
  626. }
  627. Label& label;
  628. Component& editor;
  629. int rowUnits;
  630. };
  631. struct ParamControls
  632. {
  633. Label identifierLabel { "identifierLabel", "Identifier" };
  634. TextEditor identifierEditor;
  635. Label titleLabel { "titleLabel", "Title" };
  636. TextEditor titleEditor;
  637. Label bodyLabel { "bodyLabel", "Body" };
  638. TextEditor bodyEditor;
  639. Label categoryLabel { "categoryLabel", "Category" };
  640. ComboBox categoryComboBox;
  641. Label channelIdLabel { "channelIdLabel", "Channel ID" };
  642. ComboBox channelIdComboBox;
  643. Label iconLabel { "iconLabel", "Icon" };
  644. ComboBox iconComboBox;
  645. Label subtitleLabel { "subtitleLabel", "Subtitle" };
  646. TextEditor subtitleEditor;
  647. Label badgeNumberLabel { "badgeNumberLabel", "BadgeNumber" };
  648. ComboBox badgeNumberComboBox;
  649. Label soundToPlayLabel { "soundToPlayLabel", "SoundToPlay" };
  650. ComboBox soundToPlayComboBox;
  651. Label propertiesLabel { "propertiesLabel", "Properties" };
  652. TextEditor propertiesEditor;
  653. Label fireInLabel { "fireInLabel", "Fire in" };
  654. ComboBox fireInComboBox;
  655. Label repeatLabel { "repeatLabel", "Repeat" };
  656. ToggleButton repeatButton;
  657. Label largeIconLabel { "largeIconLabel", "Large Icon" };
  658. ComboBox largeIconComboBox;
  659. Label badgeIconLabel { "badgeIconLabel", "Badge Icon" };
  660. ComboBox badgeIconComboBox;
  661. Label tickerTextLabel { "tickerTextLabel", "Ticker Text" };
  662. TextEditor tickerTextEditor;
  663. Label autoCancelLabel { "autoCancelLabel", "AutoCancel" };
  664. ToggleButton autoCancelButton;
  665. Label alertOnlyOnceLabel { "alertOnlyOnceLabel", "AlertOnlyOnce" };
  666. ToggleButton alertOnlyOnceButton;
  667. Label actionsLabel { "actionsLabel", "Actions" };
  668. ComboBox actionsComboBox;
  669. Label progressMaxLabel { "progressMaxLabel", "ProgressMax" };
  670. ComboBox progressMaxComboBox;
  671. Label progressCurrentLabel { "progressCurrentLabel", "ProgressCurrent" };
  672. ComboBox progressCurrentComboBox;
  673. Label progressIndeterminateLabel { "progressIndeterminateLabel", "ProgressIndeterminate" };
  674. ToggleButton progressIndeterminateButton;
  675. Label notifCategoryLabel { "notifCategoryLabel", "Category" };
  676. ComboBox notifCategoryComboBox;
  677. Label priorityLabel { "priorityLabel", "Priority" };
  678. ComboBox priorityComboBox;
  679. Label personLabel { "personLabel", "Person" };
  680. TextEditor personEditor;
  681. Label lockScreenVisibilityLabel { "lockScreenVisibilityLabel", "LockScreenVisibility" };
  682. ComboBox lockScreenVisibilityComboBox;
  683. Label groupIdLabel { "groupIdLabel", "GroupID" };
  684. TextEditor groupIdEditor;
  685. Label sortKeyLabel { "sortKeyLabel", "SortKey" };
  686. TextEditor sortKeyEditor;
  687. Label groupSummaryLabel { "groupSummaryLabel", "GroupSummary" };
  688. ToggleButton groupSummaryButton;
  689. Label groupAlertBehaviourLabel { "groupAlertBehaviourLabel", "GroupAlertBehaviour" };
  690. ComboBox groupAlertBehaviourComboBox;
  691. Label accentColourLabel { "accentColourLabel", "AccentColour" };
  692. TextButton accentColourButton;
  693. Label ledColourLabel { "ledColourLabel", "LedColour" };
  694. TextButton ledColourButton;
  695. Label ledMsToBeOnLabel { "ledMsToBeOnLabel", "LedMsToBeOn" };
  696. ComboBox ledMsToBeOnComboBox;
  697. Label ledMsToBeOffLabel { "ledMsToBeOffLabel", "LedMsToBeOff" };
  698. ComboBox ledMsToBeOffComboBox;
  699. Label vibratorMsToBeOnLabel { "vibratorMsToBeOnLabel", "VibrationMsToBeOn" };
  700. ComboBox vibratorMsToBeOnComboBox;
  701. Label vibratorMsToBeOffLabel { "vibratorMsToBeOffLabel", "VibrationMsToBeOff" };
  702. ComboBox vibratorMsToBeOffComboBox;
  703. Label localOnlyLabel { "localOnlyLabel", "LocalOnly" };
  704. ToggleButton localOnlyButton;
  705. Label ongoingLabel { "ongoingLabel", "Ongoing" };
  706. ToggleButton ongoingButton;
  707. Label timestampVisibilityLabel { "timestampVisibilityLabel", "TimestampMode" };
  708. ComboBox timestampVisibilityComboBox;
  709. Label timeoutAfterLabel { "timeoutAfterLabel", "Timeout After Ms" };
  710. ComboBox timeoutAfterComboBox;
  711. ColourSelector* accentColourSelector = nullptr;
  712. ColourSelector* ledColourSelector = nullptr;
  713. };
  714. void setupControls()
  715. {
  716. auto& pc = paramControls;
  717. StringArray categories { "okCategory", "okCancelCategory", "textCategory" };
  718. for (auto& c : categories)
  719. pc.categoryComboBox.addItem (c, pc.categoryComboBox.getNumItems() + 1);
  720. pc.categoryComboBox.setSelectedItemIndex (0);
  721. for (auto i = 1; i <= 3; ++i)
  722. pc.channelIdComboBox.addItem (String (i), i);
  723. pc.channelIdComboBox.setSelectedItemIndex (0);
  724. for (auto i = 0; i < 5; ++i)
  725. pc.iconComboBox.addItem ("icon" + String (i + 1), i + 1);
  726. pc.iconComboBox.setSelectedItemIndex (0);
  727. #if JUCE_MAC
  728. pc.iconComboBox.addItem ("none", 100);
  729. #endif
  730. pc.fireInComboBox.addItem ("Now", 1);
  731. for (auto i = 1; i < 11; ++i)
  732. pc.fireInComboBox.addItem (String (10 * i) + "seconds", i + 1);
  733. pc.fireInComboBox.setSelectedItemIndex (0);
  734. pc.largeIconComboBox.addItem ("none", 1);
  735. for (auto i = 1; i < 5; ++i)
  736. pc.largeIconComboBox.addItem ("icon" + String (i), i + 1);
  737. pc.largeIconComboBox.setSelectedItemIndex (0);
  738. pc.badgeIconComboBox.addItem ("none", 1);
  739. pc.badgeIconComboBox.addItem ("small", 2);
  740. pc.badgeIconComboBox.addItem ("large", 3);
  741. pc.badgeIconComboBox.setSelectedItemIndex (2);
  742. pc.actionsComboBox.addItem ("none", 1);
  743. pc.actionsComboBox.addItem ("ok-cancel", 2);
  744. pc.actionsComboBox.addItem ("text-input", 3);
  745. #if JUCE_ANDROID
  746. pc.actionsComboBox.addItem ("ok-cancel-icons", 4);
  747. pc.actionsComboBox.addItem ("text-input-limited_responses", 5);
  748. #endif
  749. pc.actionsComboBox.setSelectedItemIndex (0);
  750. for (auto i = 0; i < 7; ++i)
  751. pc.badgeNumberComboBox.addItem (String (i), i + 1);
  752. pc.badgeNumberComboBox.setSelectedItemIndex (0);
  753. #if JUCE_IOS
  754. String prefix = "Notifications/sounds/";
  755. String extension = ".caf";
  756. #else
  757. String prefix;
  758. String extension;
  759. #endif
  760. pc.soundToPlayComboBox.addItem ("none", 1);
  761. pc.soundToPlayComboBox.addItem ("default_os_sound", 2);
  762. pc.soundToPlayComboBox.addItem (prefix + "demonstrative" + extension, 3);
  763. pc.soundToPlayComboBox.addItem (prefix + "isntit" + extension, 4);
  764. pc.soundToPlayComboBox.addItem (prefix + "jinglebellssms" + extension, 5);
  765. pc.soundToPlayComboBox.addItem (prefix + "served" + extension, 6);
  766. pc.soundToPlayComboBox.addItem (prefix + "solemn" + extension, 7);
  767. pc.soundToPlayComboBox.setSelectedItemIndex (1);
  768. for (auto i = 0; i < 11; ++i)
  769. {
  770. pc.progressMaxComboBox .addItem (String (i * 10) + "%", i + 1);
  771. pc.progressCurrentComboBox.addItem (String (i * 10) + "%", i + 1);
  772. }
  773. pc.progressMaxComboBox .setSelectedItemIndex (0);
  774. pc.progressCurrentComboBox.setSelectedItemIndex (0);
  775. pc.notifCategoryComboBox.addItem ("unspecified", 1);
  776. pc.notifCategoryComboBox.addItem ("alarm", 2);
  777. pc.notifCategoryComboBox.addItem ("call", 3);
  778. pc.notifCategoryComboBox.addItem ("email", 4);
  779. pc.notifCategoryComboBox.addItem ("error", 5);
  780. pc.notifCategoryComboBox.addItem ("event", 6);
  781. pc.notifCategoryComboBox.addItem ("message", 7);
  782. pc.notifCategoryComboBox.addItem ("progress", 8);
  783. pc.notifCategoryComboBox.addItem ("promo", 9);
  784. pc.notifCategoryComboBox.addItem ("recommendation", 10);
  785. pc.notifCategoryComboBox.addItem ("reminder", 11);
  786. pc.notifCategoryComboBox.addItem ("service", 12);
  787. pc.notifCategoryComboBox.addItem ("social", 13);
  788. pc.notifCategoryComboBox.addItem ("status", 14);
  789. pc.notifCategoryComboBox.addItem ("system", 15);
  790. pc.notifCategoryComboBox.addItem ("transport", 16);
  791. pc.notifCategoryComboBox.setSelectedItemIndex (0);
  792. for (auto i = -2; i < 3; ++i)
  793. pc.priorityComboBox.addItem (String (i), i + 3);
  794. pc.priorityComboBox.setSelectedItemIndex (2);
  795. pc.lockScreenVisibilityComboBox.addItem ("don't show", 1);
  796. pc.lockScreenVisibilityComboBox.addItem ("show partially", 2);
  797. pc.lockScreenVisibilityComboBox.addItem ("show completely", 3);
  798. pc.lockScreenVisibilityComboBox.setSelectedItemIndex (1);
  799. pc.groupAlertBehaviourComboBox.addItem ("alert all", 1);
  800. pc.groupAlertBehaviourComboBox.addItem ("alert summary", 2);
  801. pc.groupAlertBehaviourComboBox.addItem ("alert children", 3);
  802. pc.groupAlertBehaviourComboBox.setSelectedItemIndex (0);
  803. pc.timeoutAfterComboBox.addItem ("No timeout", 1);
  804. for (auto i = 0; i < 10; ++i)
  805. {
  806. pc.ledMsToBeOnComboBox .addItem (String (i * 200) + "ms", i + 1);
  807. pc.ledMsToBeOffComboBox .addItem (String (i * 200) + "ms", i + 1);
  808. pc.vibratorMsToBeOnComboBox .addItem (String (i * 500) + "ms", i + 1);
  809. pc.vibratorMsToBeOffComboBox.addItem (String (i * 500) + "ms", i + 1);
  810. pc.timeoutAfterComboBox .addItem (String (5000 + 1000 * i) + "ms", i + 2);
  811. }
  812. pc.ledMsToBeOnComboBox .setSelectedItemIndex (5);
  813. pc.ledMsToBeOffComboBox .setSelectedItemIndex (5);
  814. pc.vibratorMsToBeOnComboBox .setSelectedItemIndex (0);
  815. pc.vibratorMsToBeOffComboBox.setSelectedItemIndex (0);
  816. pc.timeoutAfterComboBox .setSelectedItemIndex (0);
  817. pc.timestampVisibilityComboBox.addItem ("off", 1);
  818. pc.timestampVisibilityComboBox.addItem ("on", 2);
  819. pc.timestampVisibilityComboBox.addItem ("chronometer", 3);
  820. pc.timestampVisibilityComboBox.addItem ("count down", 4);
  821. pc.timestampVisibilityComboBox.setSelectedItemIndex (1);
  822. }
  823. void distributeControls()
  824. {
  825. auto& pc = paramControls;
  826. paramsOneView .addRowComponent (new RowComponent (pc.identifierLabel, pc.identifierEditor));
  827. paramsOneView .addRowComponent (new RowComponent (pc.titleLabel, pc.titleEditor));
  828. paramsOneView .addRowComponent (new RowComponent (pc.bodyLabel, pc.bodyEditor, 4));
  829. #if JUCE_IOS
  830. paramsOneView .addRowComponent (new RowComponent (pc.categoryLabel, pc.categoryComboBox));
  831. #elif JUCE_ANDROID
  832. paramsOneView .addRowComponent (new RowComponent (pc.channelIdLabel, pc.channelIdComboBox));
  833. #endif
  834. #if JUCE_ANDROID || JUCE_MAC
  835. paramsOneView .addRowComponent (new RowComponent (pc.iconLabel, pc.iconComboBox));
  836. #endif
  837. paramsTwoView .addRowComponent (new RowComponent (pc.subtitleLabel, pc.subtitleEditor));
  838. #if ! JUCE_MAC
  839. paramsTwoView .addRowComponent (new RowComponent (pc.badgeNumberLabel, pc.badgeNumberComboBox));
  840. #endif
  841. paramsTwoView .addRowComponent (new RowComponent (pc.soundToPlayLabel, pc.soundToPlayComboBox));
  842. paramsTwoView .addRowComponent (new RowComponent (pc.propertiesLabel, pc.propertiesEditor, 3));
  843. #if JUCE_IOS || JUCE_MAC
  844. paramsTwoView .addRowComponent (new RowComponent (pc.fireInLabel, pc.fireInComboBox));
  845. paramsTwoView .addRowComponent (new RowComponent (pc.repeatLabel, pc.repeatButton));
  846. #elif JUCE_ANDROID
  847. paramsTwoView .addRowComponent (new RowComponent (pc.largeIconLabel, pc.largeIconComboBox));
  848. paramsTwoView .addRowComponent (new RowComponent (pc.badgeIconLabel, pc.badgeIconComboBox));
  849. paramsTwoView .addRowComponent (new RowComponent (pc.tickerTextLabel, pc.tickerTextEditor));
  850. paramsTwoView .addRowComponent (new RowComponent (pc.autoCancelLabel, pc.autoCancelButton));
  851. paramsTwoView .addRowComponent (new RowComponent (pc.alertOnlyOnceLabel, pc.alertOnlyOnceButton));
  852. #endif
  853. #if JUCE_ANDROID || JUCE_MAC
  854. paramsTwoView .addRowComponent (new RowComponent (pc.actionsLabel, pc.actionsComboBox));
  855. #endif
  856. #if JUCE_ANDROID
  857. paramsThreeView.addRowComponent (new RowComponent (pc.progressMaxLabel, pc.progressMaxComboBox));
  858. paramsThreeView.addRowComponent (new RowComponent (pc.progressCurrentLabel, pc.progressCurrentComboBox));
  859. paramsThreeView.addRowComponent (new RowComponent (pc.progressIndeterminateLabel, pc.progressIndeterminateButton));
  860. paramsThreeView.addRowComponent (new RowComponent (pc.categoryLabel, pc.categoryComboBox));
  861. paramsThreeView.addRowComponent (new RowComponent (pc.priorityLabel, pc.priorityComboBox));
  862. paramsThreeView.addRowComponent (new RowComponent (pc.personLabel, pc.personEditor));
  863. paramsThreeView.addRowComponent (new RowComponent (pc.lockScreenVisibilityLabel, pc.lockScreenVisibilityComboBox));
  864. paramsThreeView.addRowComponent (new RowComponent (pc.groupIdLabel, pc.groupIdEditor));
  865. paramsThreeView.addRowComponent (new RowComponent (pc.sortKeyLabel, pc.sortKeyEditor));
  866. paramsThreeView.addRowComponent (new RowComponent (pc.groupSummaryLabel, pc.groupSummaryButton));
  867. paramsThreeView.addRowComponent (new RowComponent (pc.groupAlertBehaviourLabel, pc.groupAlertBehaviourComboBox));
  868. paramsFourView .addRowComponent (new RowComponent (pc.accentColourLabel, pc.accentColourButton));
  869. paramsFourView .addRowComponent (new RowComponent (pc.ledColourLabel, pc.ledColourButton));
  870. paramsFourView .addRowComponent (new RowComponent (pc.ledMsToBeOffLabel, pc.ledMsToBeOffComboBox));
  871. paramsFourView .addRowComponent (new RowComponent (pc.ledMsToBeOnLabel, pc.ledMsToBeOnComboBox));
  872. paramsFourView .addRowComponent (new RowComponent (pc.vibratorMsToBeOffLabel, pc.vibratorMsToBeOffComboBox));
  873. paramsFourView .addRowComponent (new RowComponent (pc.vibratorMsToBeOnLabel, pc.vibratorMsToBeOnComboBox));
  874. paramsFourView .addRowComponent (new RowComponent (pc.localOnlyLabel, pc.localOnlyButton));
  875. paramsFourView .addRowComponent (new RowComponent (pc.ongoingLabel, pc.ongoingButton));
  876. paramsFourView .addRowComponent (new RowComponent (pc.timestampVisibilityLabel, pc.timestampVisibilityComboBox));
  877. paramsFourView .addRowComponent (new RowComponent (pc.timeoutAfterLabel, pc.timeoutAfterComboBox));
  878. #endif
  879. }
  880. struct ParamsView : public Component
  881. {
  882. ParamsView()
  883. {
  884. // For now, to be able to dismiss mobile keyboard.
  885. setWantsKeyboardFocus (true);
  886. }
  887. void addRowComponent (RowComponent* rc)
  888. {
  889. rowComponents.add (rc);
  890. addAndMakeVisible (rc);
  891. }
  892. void resized() override
  893. {
  894. auto totalRowUnits = 0;
  895. for (auto* rc : rowComponents)
  896. totalRowUnits += rc->rowUnits;
  897. auto rowHeight = getHeight() / totalRowUnits;
  898. auto bounds = getLocalBounds();
  899. for (auto* rc : rowComponents)
  900. rc->setBounds (bounds.removeFromTop (rc->rowUnits * rowHeight));
  901. auto* last = rowComponents[rowComponents.size() - 1];
  902. last->setBounds (last->getBounds().withHeight (getHeight() - last->getY()));
  903. }
  904. private:
  905. OwnedArray<RowComponent> rowComponents;
  906. };
  907. struct AuxActionsView : public Component
  908. {
  909. AuxActionsView()
  910. {
  911. addAndMakeVisible (getDeliveredNotificationsButton);
  912. addAndMakeVisible (removeDeliveredNotifWithIdButton);
  913. addAndMakeVisible (deliveredNotifIdentifier);
  914. addAndMakeVisible (removeAllDeliveredNotifsButton);
  915. #if JUCE_IOS || JUCE_MAC
  916. addAndMakeVisible (getPendingNotificationsButton);
  917. addAndMakeVisible (removePendingNotifWithIdButton);
  918. addAndMakeVisible (pendingNotifIdentifier);
  919. addAndMakeVisible (removeAllPendingNotifsButton);
  920. #endif
  921. // For now, to be able to dismiss mobile keyboard.
  922. setWantsKeyboardFocus (true);
  923. }
  924. void resized() override
  925. {
  926. auto columnWidth = getWidth();
  927. auto rowHeight = getHeight() / 6;
  928. auto bounds = getLocalBounds();
  929. getDeliveredNotificationsButton .setBounds (bounds.removeFromTop (rowHeight));
  930. auto rowBounds = bounds.removeFromTop (rowHeight);
  931. removeDeliveredNotifWithIdButton.setBounds (rowBounds.removeFromLeft (columnWidth / 2));
  932. deliveredNotifIdentifier .setBounds (rowBounds);
  933. removeAllDeliveredNotifsButton .setBounds (bounds.removeFromTop (rowHeight));
  934. #if JUCE_IOS || JUCE_MAC
  935. getPendingNotificationsButton .setBounds (bounds.removeFromTop (rowHeight));
  936. rowBounds = bounds.removeFromTop (rowHeight);
  937. removePendingNotifWithIdButton.setBounds (rowBounds.removeFromLeft (columnWidth / 2));
  938. pendingNotifIdentifier .setBounds (rowBounds);
  939. removeAllPendingNotifsButton .setBounds (bounds.removeFromTop (rowHeight));
  940. #endif
  941. }
  942. TextButton getDeliveredNotificationsButton { "Get Delivered Notifications" };
  943. TextButton removeDeliveredNotifWithIdButton { "Remove Delivered Notif With ID:" };
  944. TextEditor deliveredNotifIdentifier;
  945. TextButton removeAllDeliveredNotifsButton { "Remove All Delivered Notifs" };
  946. TextButton getPendingNotificationsButton { "Get Pending Notifications" };
  947. TextButton removePendingNotifWithIdButton { "Remove Pending Notif With ID:" };
  948. TextEditor pendingNotifIdentifier;
  949. TextButton removeAllPendingNotifsButton { "Remove All Pending Notifs" };
  950. };
  951. struct RemoteView : public Component
  952. {
  953. RemoteView()
  954. {
  955. addAndMakeVisible (getDeviceTokenButton);
  956. #if JUCE_ANDROID
  957. addAndMakeVisible (sendRemoteMessageButton);
  958. addAndMakeVisible (subscribeToSportsButton);
  959. addAndMakeVisible (unsubscribeFromSportsButton);
  960. #endif
  961. }
  962. void resized()
  963. {
  964. auto rowSize = getHeight () / 10;
  965. auto bounds = getLocalBounds().reduced (getWidth() / 10, getHeight() / 10);
  966. bounds.removeFromTop (2 * rowSize);
  967. getDeviceTokenButton .setBounds (bounds.removeFromTop (rowSize));
  968. sendRemoteMessageButton .setBounds (bounds.removeFromTop (rowSize));
  969. subscribeToSportsButton .setBounds (bounds.removeFromTop (rowSize));
  970. unsubscribeFromSportsButton.setBounds (bounds.removeFromTop (rowSize));
  971. }
  972. TextButton getDeviceTokenButton { "GetDeviceToken" };
  973. TextButton sendRemoteMessageButton { "SendRemoteMessage" };
  974. TextButton subscribeToSportsButton { "SubscribeToSports" };
  975. TextButton unsubscribeFromSportsButton { "UnsubscribeFromSports" };
  976. };
  977. struct DemoTabbedComponent : public TabbedComponent
  978. {
  979. explicit DemoTabbedComponent (TabbedButtonBar::Orientation orientation)
  980. : TabbedComponent (orientation)
  981. {
  982. }
  983. void currentTabChanged (int, const String& newCurrentTabName) override
  984. {
  985. if (! showedRemoteInstructions && newCurrentTabName == "Remote")
  986. {
  987. PushNotificationsDemo::showRemoteInstructions();
  988. showedRemoteInstructions = true;
  989. }
  990. }
  991. private:
  992. bool showedRemoteInstructions = false;
  993. };
  994. static void showRemoteInstructions()
  995. {
  996. #if JUCE_IOS || JUCE_MAC
  997. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  998. "Remote Notifications instructions",
  999. "In order to be able to test remote notifications "
  1000. "ensure that the app is signed and that you register "
  1001. "the bundle ID for remote notifications in "
  1002. "Apple Developer Center.");
  1003. #endif
  1004. }
  1005. Label headerLabel { "headerLabel", "Push Notifications Demo" };
  1006. ParamControls paramControls;
  1007. ParamsView paramsOneView, paramsTwoView, paramsThreeView, paramsFourView;
  1008. AuxActionsView auxActionsView;
  1009. TabbedComponent localNotificationsTabs { TabbedButtonBar::TabsAtTop };
  1010. RemoteView remoteView;
  1011. DemoTabbedComponent mainTabs { TabbedButtonBar::TabsAtTop };
  1012. TextButton sendButton { "Send!" };
  1013. Label notAvailableYetLabel { "notAvailableYetLabel",
  1014. "Push Notifications feature is not available on this platform yet!" };
  1015. //==============================================================================
  1016. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PushNotificationsDemo)
  1017. };