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.

1211 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, vs2019, 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().getMainDisplay().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. paramControls.accentColourSelector = new ColourSelector();
  417. paramControls.accentColourSelector->setName ("accent colour");
  418. paramControls.accentColourSelector->setCurrentColour (paramControls.accentColourButton.findColour (TextButton::buttonColourId));
  419. paramControls.accentColourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  420. paramControls.accentColourSelector->setSize (200, 200);
  421. paramControls.accentColourSelector->addComponentListener (this);
  422. paramControls.accentColourSelector->addChangeListener (this);
  423. CallOutBox::launchAsynchronously (paramControls.accentColourSelector, paramControls.accentColourButton.getScreenBounds(), nullptr);
  424. }
  425. void setupLedColour()
  426. {
  427. paramControls.ledColourSelector = new ColourSelector();
  428. paramControls.ledColourSelector->setName ("led colour");
  429. paramControls.ledColourSelector->setCurrentColour (paramControls.ledColourButton.findColour (TextButton::buttonColourId));
  430. paramControls.ledColourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  431. paramControls.ledColourSelector->setSize (200, 200);
  432. paramControls.ledColourSelector->addComponentListener (this);
  433. paramControls.ledColourSelector->addChangeListener (this);
  434. CallOutBox::launchAsynchronously (paramControls.ledColourSelector, paramControls.accentColourButton.getScreenBounds(), nullptr);
  435. }
  436. void changeListenerCallback (ChangeBroadcaster* source) override
  437. {
  438. if (source == paramControls.accentColourSelector)
  439. {
  440. auto c = paramControls.accentColourSelector->getCurrentColour();
  441. paramControls.accentColourButton.setColour (TextButton::buttonColourId, c);
  442. }
  443. else if (source == paramControls.ledColourSelector)
  444. {
  445. auto c = paramControls.ledColourSelector->getCurrentColour();
  446. paramControls.ledColourButton.setColour (TextButton::buttonColourId, c);
  447. }
  448. }
  449. void componentBeingDeleted (Component& component) override
  450. {
  451. if (&component == paramControls.accentColourSelector)
  452. paramControls.accentColourSelector = nullptr;
  453. else if (&component == paramControls.ledColourSelector)
  454. paramControls.ledColourSelector = nullptr;
  455. }
  456. void handleNotification (bool isLocalNotification, const PushNotifications::Notification& n) override
  457. {
  458. ignoreUnused (isLocalNotification);
  459. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  460. "Received notification",
  461. "ID: " + n.identifier
  462. + ", title: " + n.title
  463. + ", body: " + n.body);
  464. }
  465. void handleNotificationAction (bool isLocalNotification,
  466. const PushNotifications::Notification& n,
  467. const juce::String& actionIdentifier,
  468. const juce::String& optionalResponse) override
  469. {
  470. ignoreUnused (isLocalNotification);
  471. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  472. "Received notification action",
  473. "ID: " + n.identifier
  474. + ", title: " + n.title
  475. + ", body: " + n.body
  476. + ", action: " + actionIdentifier
  477. + ", optionalResponse: " + optionalResponse);
  478. PushNotifications::getInstance()->removeDeliveredNotification (n.identifier);
  479. }
  480. void localNotificationDismissedByUser (const PushNotifications::Notification& n) override
  481. {
  482. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  483. "Notification dismissed by a user",
  484. "ID: " + n.identifier
  485. + ", title: " + n.title
  486. + ", body: " + n.body);
  487. }
  488. void deliveredNotificationsListReceived (const Array<PushNotifications::Notification>& notifs) override
  489. {
  490. String text = "Received notifications: ";
  491. for (auto& n : notifs)
  492. text << "(" << n.identifier << ", " << n.title << ", " << n.body << "), ";
  493. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon, "Received notification list", text);
  494. }
  495. void pendingLocalNotificationsListReceived (const Array<PushNotifications::Notification>& notifs) override
  496. {
  497. String text = "Pending notifications: ";
  498. for (auto& n : notifs)
  499. text << "(" << n.identifier << ", " << n.title << ", " << n.body << "), ";
  500. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon, "Pending notification list", text);
  501. }
  502. void deviceTokenRefreshed (const String& token) override
  503. {
  504. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  505. "Device token refreshed",
  506. token);
  507. }
  508. #if JUCE_ANDROID
  509. void remoteNotificationsDeleted() override
  510. {
  511. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  512. "Remote notifications deleted",
  513. "Some of the pending messages were removed!");
  514. }
  515. void upstreamMessageSent (const String& messageId) override
  516. {
  517. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  518. "Upstream message sent",
  519. "Message id: " + messageId);
  520. }
  521. void upstreamMessageSendingError (const String& messageId, const String& error) override
  522. {
  523. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  524. "Upstream message sending error",
  525. "Message id: " + messageId
  526. + "\nerror: " + error);
  527. }
  528. static Array<PushNotifications::Channel> getAndroidChannels()
  529. {
  530. using Channel = PushNotifications::Channel;
  531. Channel ch1, ch2, ch3;
  532. ch1.identifier = "1";
  533. ch1.name = "HighImportance";
  534. ch1.importance = PushNotifications::Channel::max;
  535. ch1.lockScreenAppearance = PushNotifications::Notification::showCompletely;
  536. ch1.description = "High Priority Channel for important stuff";
  537. ch1.groupId = "demoGroup";
  538. ch1.ledColour = Colours::red;
  539. ch1.bypassDoNotDisturb = true;
  540. ch1.canShowBadge = true;
  541. ch1.enableLights = true;
  542. ch1.enableVibration = true;
  543. ch1.soundToPlay = URL ("demonstrative");
  544. ch1.vibrationPattern = { 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 };
  545. ch2.identifier = "2";
  546. ch2.name = "MediumImportance";
  547. ch2.importance = PushNotifications::Channel::normal;
  548. ch2.lockScreenAppearance = PushNotifications::Notification::showPartially;
  549. ch2.description = "Medium Priority Channel for standard stuff";
  550. ch2.groupId = "demoGroup";
  551. ch2.ledColour = Colours::yellow;
  552. ch2.canShowBadge = true;
  553. ch2.enableLights = true;
  554. ch2.enableVibration = true;
  555. ch2.soundToPlay = URL ("default_os_sound");
  556. ch2.vibrationPattern = { 1000, 1000 };
  557. ch3.identifier = "3";
  558. ch3.name = "LowImportance";
  559. ch3.importance = PushNotifications::Channel::min;
  560. ch3.lockScreenAppearance = PushNotifications::Notification::dontShow;
  561. ch3.description = "Low Priority Channel for silly stuff";
  562. ch3.groupId = "demoGroup";
  563. return { ch1, ch2, ch3 };
  564. }
  565. #elif JUCE_IOS || JUCE_MAC
  566. static PushNotifications::Settings getNotificationSettings()
  567. {
  568. PushNotifications::Settings settings;
  569. settings.allowAlert = true;
  570. settings.allowBadge = true;
  571. settings.allowSound = true;
  572. #if JUCE_IOS
  573. using Action = PushNotifications::Settings::Action;
  574. using Category = PushNotifications::Settings::Category;
  575. Action okAction;
  576. okAction.identifier = "okAction";
  577. okAction.title = "OK!";
  578. okAction.style = Action::button;
  579. okAction.triggerInBackground = true;
  580. Action cancelAction;
  581. cancelAction.identifier = "cancelAction";
  582. cancelAction.title = "Cancel";
  583. cancelAction.style = Action::button;
  584. cancelAction.triggerInBackground = true;
  585. cancelAction.destructive = true;
  586. Action textAction;
  587. textAction.identifier = "textAction";
  588. textAction.title = "Enter text";
  589. textAction.style = Action::text;
  590. textAction.triggerInBackground = true;
  591. textAction.destructive = false;
  592. textAction.textInputButtonText = "Ok";
  593. textAction.textInputPlaceholder = "Enter text...";
  594. Category okCategory;
  595. okCategory.identifier = "okCategory";
  596. okCategory.actions = { okAction };
  597. Category okCancelCategory;
  598. okCancelCategory.identifier = "okCancelCategory";
  599. okCancelCategory.actions = { okAction, cancelAction };
  600. Category textCategory;
  601. textCategory.identifier = "textCategory";
  602. textCategory.actions = { textAction };
  603. textCategory.sendDismissAction = true;
  604. settings.categories = { okCategory, okCancelCategory, textCategory };
  605. #endif
  606. return settings;
  607. }
  608. #endif
  609. struct RowComponent : public Component
  610. {
  611. RowComponent (Label& l, Component& c, int u = 1)
  612. : label (l),
  613. editor (c),
  614. rowUnits (u)
  615. {
  616. addAndMakeVisible (label);
  617. addAndMakeVisible (editor);
  618. }
  619. void resized() override
  620. {
  621. auto bounds = getLocalBounds();
  622. label .setBounds (bounds.removeFromLeft (getWidth() / 3));
  623. editor.setBounds (bounds);
  624. }
  625. Label& label;
  626. Component& editor;
  627. int rowUnits;
  628. };
  629. struct ParamControls
  630. {
  631. Label identifierLabel { "identifierLabel", "Identifier" };
  632. TextEditor identifierEditor;
  633. Label titleLabel { "titleLabel", "Title" };
  634. TextEditor titleEditor;
  635. Label bodyLabel { "bodyLabel", "Body" };
  636. TextEditor bodyEditor;
  637. Label categoryLabel { "categoryLabel", "Category" };
  638. ComboBox categoryComboBox;
  639. Label channelIdLabel { "channelIdLabel", "Channel ID" };
  640. ComboBox channelIdComboBox;
  641. Label iconLabel { "iconLabel", "Icon" };
  642. ComboBox iconComboBox;
  643. Label subtitleLabel { "subtitleLabel", "Subtitle" };
  644. TextEditor subtitleEditor;
  645. Label badgeNumberLabel { "badgeNumberLabel", "BadgeNumber" };
  646. ComboBox badgeNumberComboBox;
  647. Label soundToPlayLabel { "soundToPlayLabel", "SoundToPlay" };
  648. ComboBox soundToPlayComboBox;
  649. Label propertiesLabel { "propertiesLabel", "Properties" };
  650. TextEditor propertiesEditor;
  651. Label fireInLabel { "fireInLabel", "Fire in" };
  652. ComboBox fireInComboBox;
  653. Label repeatLabel { "repeatLabel", "Repeat" };
  654. ToggleButton repeatButton;
  655. Label largeIconLabel { "largeIconLabel", "Large Icon" };
  656. ComboBox largeIconComboBox;
  657. Label badgeIconLabel { "badgeIconLabel", "Badge Icon" };
  658. ComboBox badgeIconComboBox;
  659. Label tickerTextLabel { "tickerTextLabel", "Ticker Text" };
  660. TextEditor tickerTextEditor;
  661. Label autoCancelLabel { "autoCancelLabel", "AutoCancel" };
  662. ToggleButton autoCancelButton;
  663. Label alertOnlyOnceLabel { "alertOnlyOnceLabel", "AlertOnlyOnce" };
  664. ToggleButton alertOnlyOnceButton;
  665. Label actionsLabel { "actionsLabel", "Actions" };
  666. ComboBox actionsComboBox;
  667. Label progressMaxLabel { "progressMaxLabel", "ProgressMax" };
  668. ComboBox progressMaxComboBox;
  669. Label progressCurrentLabel { "progressCurrentLabel", "ProgressCurrent" };
  670. ComboBox progressCurrentComboBox;
  671. Label progressIndeterminateLabel { "progressIndeterminateLabel", "ProgressIndeterminate" };
  672. ToggleButton progressIndeterminateButton;
  673. Label notifCategoryLabel { "notifCategoryLabel", "Category" };
  674. ComboBox notifCategoryComboBox;
  675. Label priorityLabel { "priorityLabel", "Priority" };
  676. ComboBox priorityComboBox;
  677. Label personLabel { "personLabel", "Person" };
  678. TextEditor personEditor;
  679. Label lockScreenVisibilityLabel { "lockScreenVisibilityLabel", "LockScreenVisibility" };
  680. ComboBox lockScreenVisibilityComboBox;
  681. Label groupIdLabel { "groupIdLabel", "GroupID" };
  682. TextEditor groupIdEditor;
  683. Label sortKeyLabel { "sortKeyLabel", "SortKey" };
  684. TextEditor sortKeyEditor;
  685. Label groupSummaryLabel { "groupSummaryLabel", "GroupSummary" };
  686. ToggleButton groupSummaryButton;
  687. Label groupAlertBehaviourLabel { "groupAlertBehaviourLabel", "GroupAlertBehaviour" };
  688. ComboBox groupAlertBehaviourComboBox;
  689. Label accentColourLabel { "accentColourLabel", "AccentColour" };
  690. TextButton accentColourButton;
  691. Label ledColourLabel { "ledColourLabel", "LedColour" };
  692. TextButton ledColourButton;
  693. Label ledMsToBeOnLabel { "ledMsToBeOnLabel", "LedMsToBeOn" };
  694. ComboBox ledMsToBeOnComboBox;
  695. Label ledMsToBeOffLabel { "ledMsToBeOffLabel", "LedMsToBeOff" };
  696. ComboBox ledMsToBeOffComboBox;
  697. Label vibratorMsToBeOnLabel { "vibratorMsToBeOnLabel", "VibrationMsToBeOn" };
  698. ComboBox vibratorMsToBeOnComboBox;
  699. Label vibratorMsToBeOffLabel { "vibratorMsToBeOffLabel", "VibrationMsToBeOff" };
  700. ComboBox vibratorMsToBeOffComboBox;
  701. Label localOnlyLabel { "localOnlyLabel", "LocalOnly" };
  702. ToggleButton localOnlyButton;
  703. Label ongoingLabel { "ongoingLabel", "Ongoing" };
  704. ToggleButton ongoingButton;
  705. Label timestampVisibilityLabel { "timestampVisibilityLabel", "TimestampMode" };
  706. ComboBox timestampVisibilityComboBox;
  707. Label timeoutAfterLabel { "timeoutAfterLabel", "Timeout After Ms" };
  708. ComboBox timeoutAfterComboBox;
  709. ColourSelector* accentColourSelector = nullptr;
  710. ColourSelector* ledColourSelector = nullptr;
  711. };
  712. void setupControls()
  713. {
  714. auto& pc = paramControls;
  715. StringArray categories { "okCategory", "okCancelCategory", "textCategory" };
  716. for (auto& c : categories)
  717. pc.categoryComboBox.addItem (c, pc.categoryComboBox.getNumItems() + 1);
  718. pc.categoryComboBox.setSelectedItemIndex (0);
  719. for (auto i = 1; i <= 3; ++i)
  720. pc.channelIdComboBox.addItem (String (i), i);
  721. pc.channelIdComboBox.setSelectedItemIndex (0);
  722. for (auto i = 0; i < 5; ++i)
  723. pc.iconComboBox.addItem ("icon" + String (i + 1), i + 1);
  724. pc.iconComboBox.setSelectedItemIndex (0);
  725. #if JUCE_MAC
  726. pc.iconComboBox.addItem ("none", 100);
  727. #endif
  728. pc.fireInComboBox.addItem ("Now", 1);
  729. for (auto i = 1; i < 11; ++i)
  730. pc.fireInComboBox.addItem (String (10 * i) + "seconds", i + 1);
  731. pc.fireInComboBox.setSelectedItemIndex (0);
  732. pc.largeIconComboBox.addItem ("none", 1);
  733. for (auto i = 1; i < 5; ++i)
  734. pc.largeIconComboBox.addItem ("icon" + String (i), i + 1);
  735. pc.largeIconComboBox.setSelectedItemIndex (0);
  736. pc.badgeIconComboBox.addItem ("none", 1);
  737. pc.badgeIconComboBox.addItem ("small", 2);
  738. pc.badgeIconComboBox.addItem ("large", 3);
  739. pc.badgeIconComboBox.setSelectedItemIndex (2);
  740. pc.actionsComboBox.addItem ("none", 1);
  741. pc.actionsComboBox.addItem ("ok-cancel", 2);
  742. pc.actionsComboBox.addItem ("text-input", 3);
  743. #if JUCE_ANDROID
  744. pc.actionsComboBox.addItem ("ok-cancel-icons", 4);
  745. pc.actionsComboBox.addItem ("text-input-limited_responses", 5);
  746. #endif
  747. pc.actionsComboBox.setSelectedItemIndex (0);
  748. for (auto i = 0; i < 7; ++i)
  749. pc.badgeNumberComboBox.addItem (String (i), i + 1);
  750. pc.badgeNumberComboBox.setSelectedItemIndex (0);
  751. #if JUCE_IOS
  752. String prefix = "Notifications/sounds/";
  753. String extension = ".caf";
  754. #else
  755. String prefix;
  756. String extension;
  757. #endif
  758. pc.soundToPlayComboBox.addItem ("none", 1);
  759. pc.soundToPlayComboBox.addItem ("default_os_sound", 2);
  760. pc.soundToPlayComboBox.addItem (prefix + "demonstrative" + extension, 3);
  761. pc.soundToPlayComboBox.addItem (prefix + "isntit" + extension, 4);
  762. pc.soundToPlayComboBox.addItem (prefix + "jinglebellssms" + extension, 5);
  763. pc.soundToPlayComboBox.addItem (prefix + "served" + extension, 6);
  764. pc.soundToPlayComboBox.addItem (prefix + "solemn" + extension, 7);
  765. pc.soundToPlayComboBox.setSelectedItemIndex (1);
  766. for (auto i = 0; i < 11; ++i)
  767. {
  768. pc.progressMaxComboBox .addItem (String (i * 10) + "%", i + 1);
  769. pc.progressCurrentComboBox.addItem (String (i * 10) + "%", i + 1);
  770. }
  771. pc.progressMaxComboBox .setSelectedItemIndex (0);
  772. pc.progressCurrentComboBox.setSelectedItemIndex (0);
  773. pc.notifCategoryComboBox.addItem ("unspecified", 1);
  774. pc.notifCategoryComboBox.addItem ("alarm", 2);
  775. pc.notifCategoryComboBox.addItem ("call", 3);
  776. pc.notifCategoryComboBox.addItem ("email", 4);
  777. pc.notifCategoryComboBox.addItem ("error", 5);
  778. pc.notifCategoryComboBox.addItem ("event", 6);
  779. pc.notifCategoryComboBox.addItem ("message", 7);
  780. pc.notifCategoryComboBox.addItem ("progress", 8);
  781. pc.notifCategoryComboBox.addItem ("promo", 9);
  782. pc.notifCategoryComboBox.addItem ("recommendation", 10);
  783. pc.notifCategoryComboBox.addItem ("reminder", 11);
  784. pc.notifCategoryComboBox.addItem ("service", 12);
  785. pc.notifCategoryComboBox.addItem ("social", 13);
  786. pc.notifCategoryComboBox.addItem ("status", 14);
  787. pc.notifCategoryComboBox.addItem ("system", 15);
  788. pc.notifCategoryComboBox.addItem ("transport", 16);
  789. pc.notifCategoryComboBox.setSelectedItemIndex (0);
  790. for (auto i = -2; i < 3; ++i)
  791. pc.priorityComboBox.addItem (String (i), i + 3);
  792. pc.priorityComboBox.setSelectedItemIndex (2);
  793. pc.lockScreenVisibilityComboBox.addItem ("don't show", 1);
  794. pc.lockScreenVisibilityComboBox.addItem ("show partially", 2);
  795. pc.lockScreenVisibilityComboBox.addItem ("show completely", 3);
  796. pc.lockScreenVisibilityComboBox.setSelectedItemIndex (1);
  797. pc.groupAlertBehaviourComboBox.addItem ("alert all", 1);
  798. pc.groupAlertBehaviourComboBox.addItem ("alert summary", 2);
  799. pc.groupAlertBehaviourComboBox.addItem ("alert children", 3);
  800. pc.groupAlertBehaviourComboBox.setSelectedItemIndex (0);
  801. pc.timeoutAfterComboBox.addItem ("No timeout", 1);
  802. for (auto i = 0; i < 10; ++i)
  803. {
  804. pc.ledMsToBeOnComboBox .addItem (String (i * 200) + "ms", i + 1);
  805. pc.ledMsToBeOffComboBox .addItem (String (i * 200) + "ms", i + 1);
  806. pc.vibratorMsToBeOnComboBox .addItem (String (i * 500) + "ms", i + 1);
  807. pc.vibratorMsToBeOffComboBox.addItem (String (i * 500) + "ms", i + 1);
  808. pc.timeoutAfterComboBox .addItem (String (5000 + 1000 * i) + "ms", i + 2);
  809. }
  810. pc.ledMsToBeOnComboBox .setSelectedItemIndex (5);
  811. pc.ledMsToBeOffComboBox .setSelectedItemIndex (5);
  812. pc.vibratorMsToBeOnComboBox .setSelectedItemIndex (0);
  813. pc.vibratorMsToBeOffComboBox.setSelectedItemIndex (0);
  814. pc.timeoutAfterComboBox .setSelectedItemIndex (0);
  815. pc.timestampVisibilityComboBox.addItem ("off", 1);
  816. pc.timestampVisibilityComboBox.addItem ("on", 2);
  817. pc.timestampVisibilityComboBox.addItem ("chronometer", 3);
  818. pc.timestampVisibilityComboBox.addItem ("count down", 4);
  819. pc.timestampVisibilityComboBox.setSelectedItemIndex (1);
  820. }
  821. void distributeControls()
  822. {
  823. auto& pc = paramControls;
  824. paramsOneView .addRowComponent (new RowComponent (pc.identifierLabel, pc.identifierEditor));
  825. paramsOneView .addRowComponent (new RowComponent (pc.titleLabel, pc.titleEditor));
  826. paramsOneView .addRowComponent (new RowComponent (pc.bodyLabel, pc.bodyEditor, 4));
  827. #if JUCE_IOS
  828. paramsOneView .addRowComponent (new RowComponent (pc.categoryLabel, pc.categoryComboBox));
  829. #elif JUCE_ANDROID
  830. paramsOneView .addRowComponent (new RowComponent (pc.channelIdLabel, pc.channelIdComboBox));
  831. #endif
  832. #if JUCE_ANDROID || JUCE_MAC
  833. paramsOneView .addRowComponent (new RowComponent (pc.iconLabel, pc.iconComboBox));
  834. #endif
  835. paramsTwoView .addRowComponent (new RowComponent (pc.subtitleLabel, pc.subtitleEditor));
  836. #if ! JUCE_MAC
  837. paramsTwoView .addRowComponent (new RowComponent (pc.badgeNumberLabel, pc.badgeNumberComboBox));
  838. #endif
  839. paramsTwoView .addRowComponent (new RowComponent (pc.soundToPlayLabel, pc.soundToPlayComboBox));
  840. paramsTwoView .addRowComponent (new RowComponent (pc.propertiesLabel, pc.propertiesEditor, 3));
  841. #if JUCE_IOS || JUCE_MAC
  842. paramsTwoView .addRowComponent (new RowComponent (pc.fireInLabel, pc.fireInComboBox));
  843. paramsTwoView .addRowComponent (new RowComponent (pc.repeatLabel, pc.repeatButton));
  844. #elif JUCE_ANDROID
  845. paramsTwoView .addRowComponent (new RowComponent (pc.largeIconLabel, pc.largeIconComboBox));
  846. paramsTwoView .addRowComponent (new RowComponent (pc.badgeIconLabel, pc.badgeIconComboBox));
  847. paramsTwoView .addRowComponent (new RowComponent (pc.tickerTextLabel, pc.tickerTextEditor));
  848. paramsTwoView .addRowComponent (new RowComponent (pc.autoCancelLabel, pc.autoCancelButton));
  849. paramsTwoView .addRowComponent (new RowComponent (pc.alertOnlyOnceLabel, pc.alertOnlyOnceButton));
  850. #endif
  851. #if JUCE_ANDROID || JUCE_MAC
  852. paramsTwoView .addRowComponent (new RowComponent (pc.actionsLabel, pc.actionsComboBox));
  853. #endif
  854. #if JUCE_ANDROID
  855. paramsThreeView.addRowComponent (new RowComponent (pc.progressMaxLabel, pc.progressMaxComboBox));
  856. paramsThreeView.addRowComponent (new RowComponent (pc.progressCurrentLabel, pc.progressCurrentComboBox));
  857. paramsThreeView.addRowComponent (new RowComponent (pc.progressIndeterminateLabel, pc.progressIndeterminateButton));
  858. paramsThreeView.addRowComponent (new RowComponent (pc.categoryLabel, pc.categoryComboBox));
  859. paramsThreeView.addRowComponent (new RowComponent (pc.priorityLabel, pc.priorityComboBox));
  860. paramsThreeView.addRowComponent (new RowComponent (pc.personLabel, pc.personEditor));
  861. paramsThreeView.addRowComponent (new RowComponent (pc.lockScreenVisibilityLabel, pc.lockScreenVisibilityComboBox));
  862. paramsThreeView.addRowComponent (new RowComponent (pc.groupIdLabel, pc.groupIdEditor));
  863. paramsThreeView.addRowComponent (new RowComponent (pc.sortKeyLabel, pc.sortKeyEditor));
  864. paramsThreeView.addRowComponent (new RowComponent (pc.groupSummaryLabel, pc.groupSummaryButton));
  865. paramsThreeView.addRowComponent (new RowComponent (pc.groupAlertBehaviourLabel, pc.groupAlertBehaviourComboBox));
  866. paramsFourView .addRowComponent (new RowComponent (pc.accentColourLabel, pc.accentColourButton));
  867. paramsFourView .addRowComponent (new RowComponent (pc.ledColourLabel, pc.ledColourButton));
  868. paramsFourView .addRowComponent (new RowComponent (pc.ledMsToBeOffLabel, pc.ledMsToBeOffComboBox));
  869. paramsFourView .addRowComponent (new RowComponent (pc.ledMsToBeOnLabel, pc.ledMsToBeOnComboBox));
  870. paramsFourView .addRowComponent (new RowComponent (pc.vibratorMsToBeOffLabel, pc.vibratorMsToBeOffComboBox));
  871. paramsFourView .addRowComponent (new RowComponent (pc.vibratorMsToBeOnLabel, pc.vibratorMsToBeOnComboBox));
  872. paramsFourView .addRowComponent (new RowComponent (pc.localOnlyLabel, pc.localOnlyButton));
  873. paramsFourView .addRowComponent (new RowComponent (pc.ongoingLabel, pc.ongoingButton));
  874. paramsFourView .addRowComponent (new RowComponent (pc.timestampVisibilityLabel, pc.timestampVisibilityComboBox));
  875. paramsFourView .addRowComponent (new RowComponent (pc.timeoutAfterLabel, pc.timeoutAfterComboBox));
  876. #endif
  877. }
  878. struct ParamsView : public Component
  879. {
  880. ParamsView()
  881. {
  882. // For now, to be able to dismiss mobile keyboard.
  883. setWantsKeyboardFocus (true);
  884. }
  885. void addRowComponent (RowComponent* rc)
  886. {
  887. rowComponents.add (rc);
  888. addAndMakeVisible (rc);
  889. }
  890. void resized() override
  891. {
  892. auto totalRowUnits = 0;
  893. for (auto* rc : rowComponents)
  894. totalRowUnits += rc->rowUnits;
  895. auto rowHeight = getHeight() / totalRowUnits;
  896. auto bounds = getLocalBounds();
  897. for (auto* rc : rowComponents)
  898. rc->setBounds (bounds.removeFromTop (rc->rowUnits * rowHeight));
  899. auto* last = rowComponents[rowComponents.size() - 1];
  900. last->setBounds (last->getBounds().withHeight (getHeight() - last->getY()));
  901. }
  902. private:
  903. OwnedArray<RowComponent> rowComponents;
  904. };
  905. struct AuxActionsView : public Component
  906. {
  907. AuxActionsView()
  908. {
  909. addAndMakeVisible (getDeliveredNotificationsButton);
  910. addAndMakeVisible (removeDeliveredNotifWithIdButton);
  911. addAndMakeVisible (deliveredNotifIdentifier);
  912. addAndMakeVisible (removeAllDeliveredNotifsButton);
  913. #if JUCE_IOS || JUCE_MAC
  914. addAndMakeVisible (getPendingNotificationsButton);
  915. addAndMakeVisible (removePendingNotifWithIdButton);
  916. addAndMakeVisible (pendingNotifIdentifier);
  917. addAndMakeVisible (removeAllPendingNotifsButton);
  918. #endif
  919. // For now, to be able to dismiss mobile keyboard.
  920. setWantsKeyboardFocus (true);
  921. }
  922. void resized() override
  923. {
  924. auto columnWidth = getWidth();
  925. auto rowHeight = getHeight() / 6;
  926. auto bounds = getLocalBounds();
  927. getDeliveredNotificationsButton .setBounds (bounds.removeFromTop (rowHeight));
  928. auto rowBounds = bounds.removeFromTop (rowHeight);
  929. removeDeliveredNotifWithIdButton.setBounds (rowBounds.removeFromLeft (columnWidth / 2));
  930. deliveredNotifIdentifier .setBounds (rowBounds);
  931. removeAllDeliveredNotifsButton .setBounds (bounds.removeFromTop (rowHeight));
  932. #if JUCE_IOS || JUCE_MAC
  933. getPendingNotificationsButton .setBounds (bounds.removeFromTop (rowHeight));
  934. rowBounds = bounds.removeFromTop (rowHeight);
  935. removePendingNotifWithIdButton.setBounds (rowBounds.removeFromLeft (columnWidth / 2));
  936. pendingNotifIdentifier .setBounds (rowBounds);
  937. removeAllPendingNotifsButton .setBounds (bounds.removeFromTop (rowHeight));
  938. #endif
  939. }
  940. TextButton getDeliveredNotificationsButton { "Get Delivered Notifications" };
  941. TextButton removeDeliveredNotifWithIdButton { "Remove Delivered Notif With ID:" };
  942. TextEditor deliveredNotifIdentifier;
  943. TextButton removeAllDeliveredNotifsButton { "Remove All Delivered Notifs" };
  944. TextButton getPendingNotificationsButton { "Get Pending Notifications" };
  945. TextButton removePendingNotifWithIdButton { "Remove Pending Notif With ID:" };
  946. TextEditor pendingNotifIdentifier;
  947. TextButton removeAllPendingNotifsButton { "Remove All Pending Notifs" };
  948. };
  949. struct RemoteView : public Component
  950. {
  951. RemoteView()
  952. {
  953. addAndMakeVisible (getDeviceTokenButton);
  954. #if JUCE_ANDROID
  955. addAndMakeVisible (sendRemoteMessageButton);
  956. addAndMakeVisible (subscribeToSportsButton);
  957. addAndMakeVisible (unsubscribeFromSportsButton);
  958. #endif
  959. }
  960. void resized()
  961. {
  962. auto rowSize = getHeight () / 10;
  963. auto bounds = getLocalBounds().reduced (getWidth() / 10, getHeight() / 10);
  964. bounds.removeFromTop (2 * rowSize);
  965. getDeviceTokenButton .setBounds (bounds.removeFromTop (rowSize));
  966. sendRemoteMessageButton .setBounds (bounds.removeFromTop (rowSize));
  967. subscribeToSportsButton .setBounds (bounds.removeFromTop (rowSize));
  968. unsubscribeFromSportsButton.setBounds (bounds.removeFromTop (rowSize));
  969. }
  970. TextButton getDeviceTokenButton { "GetDeviceToken" };
  971. TextButton sendRemoteMessageButton { "SendRemoteMessage" };
  972. TextButton subscribeToSportsButton { "SubscribeToSports" };
  973. TextButton unsubscribeFromSportsButton { "UnsubscribeFromSports" };
  974. };
  975. struct DemoTabbedComponent : public TabbedComponent
  976. {
  977. explicit DemoTabbedComponent (TabbedButtonBar::Orientation orientation)
  978. : TabbedComponent (orientation)
  979. {
  980. }
  981. void currentTabChanged (int, const String& newCurrentTabName) override
  982. {
  983. if (! showedRemoteInstructions && newCurrentTabName == "Remote")
  984. {
  985. PushNotificationsDemo::showRemoteInstructions();
  986. showedRemoteInstructions = true;
  987. }
  988. }
  989. private:
  990. bool showedRemoteInstructions = false;
  991. };
  992. static void showRemoteInstructions()
  993. {
  994. #if JUCE_IOS || JUCE_MAC
  995. NativeMessageBox::showMessageBoxAsync (AlertWindow::InfoIcon,
  996. "Remote Notifications instructions",
  997. "In order to be able to test remote notifications "
  998. "ensure that the app is signed and that you register "
  999. "the bundle ID for remote notifications in "
  1000. "Apple Developer Center.");
  1001. #endif
  1002. }
  1003. Label headerLabel { "headerLabel", "Push Notifications Demo" };
  1004. ParamControls paramControls;
  1005. ParamsView paramsOneView, paramsTwoView, paramsThreeView, paramsFourView;
  1006. AuxActionsView auxActionsView;
  1007. TabbedComponent localNotificationsTabs { TabbedButtonBar::TabsAtTop };
  1008. RemoteView remoteView;
  1009. DemoTabbedComponent mainTabs { TabbedButtonBar::TabsAtTop };
  1010. TextButton sendButton { "Send!" };
  1011. Label notAvailableYetLabel { "notAvailableYetLabel",
  1012. "Push Notifications feature is not available on this platform yet!" };
  1013. //==============================================================================
  1014. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PushNotificationsDemo)
  1015. };