Singleton class responsible for push notifications functionality. More...
#include <juce_PushNotifications.h>
Classes | |
struct | Channel |
Android API level 26 or higher only: Represents notification channel through which notifications will be sent. More... | |
struct | ChannelGroup |
Android API level 26 or higher only: represents a channel group. More... | |
struct | Listener |
Register a listener (ideally on application startup) to receive information about notifications received and any callbacks to async functions called. More... | |
struct | Notification |
Represents a notification that can be sent or received. More... | |
struct | Settings |
Describes settings we want to use for current device. More... | |
Public Member Functions | |
void | requestPermissionsWithSettings (const Settings &settings) |
Initialises push notifications on current device with the settings provided. | |
void | requestSettingsUsed () |
Sends an asynchronous request to retrieve current settings that are currently in use. | |
void | setupChannels (const Array< ChannelGroup > &groups, const Array< Channel > &channels) |
Android API level 26 or higher only: configures notification channel groups and channels to be used in the app. | |
void | getPendingLocalNotifications () const |
iOS only: sends an asynchronous request to retrieve a list of notifications that were scheduled and not yet delivered. | |
void | removePendingLocalNotification (const String &identifier) |
Unschedules a pending local notification with a given identifier. | |
void | removeAllPendingLocalNotifications () |
Unschedules all pending local notifications. | |
bool | areNotificationsEnabled () const |
Checks whether notifications are enabled for given application. | |
void | sendLocalNotification (const Notification ¬ification) |
On iOS as well as on Android, sends a local notification. | |
void | getDeliveredNotifications () const |
Sends a request for a list of notifications delivered. | |
void | removeDeliveredNotification (const String &identifier) |
Removes a previously delivered notification. | |
void | removeAllDeliveredNotifications () |
Removes all notifications that were delivered. | |
String | getDeviceToken () const |
Retrieves current device token. | |
void | subscribeToTopic (const String &topic) |
Android only: allows to subscribe to messages from a specific topic. | |
void | unsubscribeFromTopic (const String &topic) |
Android only: allows to remove a topic subscription that was previously added with subscribeToTopic(). | |
void | sendUpstreamMessage (const String &serverSenderId, const String &collapseKey, const String &messageId, const String &messageType, int timeToLive, const StringPairArray &additionalData) |
Android only: sends an upstream message to your app server. | |
void | addListener (Listener *l) |
void | removeListener (Listener *l) |
Singleton class responsible for push notifications functionality.
Both remote and local notifications are supported. To get information about notifications, register a listener on your application startup. It is best to register the listener as soon as possible, because your application can be launched from a push notification too.
To send a local notification create an instance of Notification, fill the necessary fields and call PushNotifications::sendLocalNotification(). When receiving local or remote notifications, inspect the Notification's fields for notification details. Bear in mind that some fields will not be available when receiving a remote notification.
void PushNotifications::requestPermissionsWithSettings | ( | const Settings & | settings | ) |
Initialises push notifications on current device with the settings provided.
Call this on your application startup and on iOS the first time the application starts, a user will be presented with a permission request dialog to give push notifications permission. Once a user responds, Listener::notificationSettingsReceived() will be called so that you can check what permissions where actually granted. The listener callback will be called on each subsequent startup too (provided you called requestPermissionsWithSettings() on previous application run). This way you can check what are current push notifications permissions.
Note that settings are currently only used on iOS. When calling on other platforms, Settings with no categories and all allow* flags set to true will be received in Listener::notificationSettingsReceived().
You can also call requestSettingsUsed() to explicitly ask for current settings.
void PushNotifications::requestSettingsUsed | ( | ) |
Sends an asynchronous request to retrieve current settings that are currently in use.
These can be exactly the same as used in requestPermissionsWithSettings(), but depending on user's subsequent changes in OS settings, the actual current settings may be different (e.g. user might have later decided to disable sounds).
Note that settings are currently only used on iOS and partially on OSX.
On OSX, only allow* flags are used and they refer to remote notifications only. For local notifications, refer to System Preferences.
When calling this function on other platforms, Settings with no categories and all allow* flags set to true will be received in Listener::notificationSettingsReceived().
void PushNotifications::setupChannels | ( | const Array< ChannelGroup > & | groups, |
const Array< Channel > & | channels ) |
Android API level 26 or higher only: configures notification channel groups and channels to be used in the app.
These have to be setup before notifications can be sent on Android API level 26 or higher.
void PushNotifications::getPendingLocalNotifications | ( | ) | const |
iOS only: sends an asynchronous request to retrieve a list of notifications that were scheduled and not yet delivered.
When the list is retrieved, Listener::pendingLocalNotificationsListReceived() will be called.
void PushNotifications::removePendingLocalNotification | ( | const String & | identifier | ) |
Unschedules a pending local notification with a given identifier.
void PushNotifications::removeAllPendingLocalNotifications | ( | ) |
Unschedules all pending local notifications.
iOS only.
bool PushNotifications::areNotificationsEnabled | ( | ) | const |
Checks whether notifications are enabled for given application.
On iOS and OSX this will always return true, use requestSettingsUsed() instead.
void PushNotifications::sendLocalNotification | ( | const Notification & | notification | ) |
On iOS as well as on Android, sends a local notification.
This will refresh an existing notification if the same identifier is used as in a notification that was already sent and not yet responded by a user.
void PushNotifications::getDeliveredNotifications | ( | ) | const |
Sends a request for a list of notifications delivered.
Such notifications are visible in the notification area on the device and they are still waiting for user action/response. When the request is finished Listener::deliveredNotificationsListReceived() will be called.
On iOS, iOS version 10 or higher is required. On Android, API level 18 or higher is required. For unsupported platforms, Listener::deliveredNotificationsListReceived() will return an empty array.
void PushNotifications::removeDeliveredNotification | ( | const String & | identifier | ) |
Removes a previously delivered notification.
This can be useful for instance when the information in the notification becomes obsolete.
void PushNotifications::removeAllDeliveredNotifications | ( | ) |
Removes all notifications that were delivered.
String PushNotifications::getDeviceToken | ( | ) | const |
Retrieves current device token.
Note, it is not a good idea to cache this token because it may change in the meantime. Always call this method to get the current token value.
void PushNotifications::subscribeToTopic | ( | const String & | topic | ) |
Android only: allows to subscribe to messages from a specific topic.
So you could for instance subscribe this device to all "sports" topic messages to receive any remote notifications that have "sports" topic set. Refer to Firebase documentation for how to send topic messages.
void PushNotifications::unsubscribeFromTopic | ( | const String & | topic | ) |
Android only: allows to remove a topic subscription that was previously added with subscribeToTopic().
void PushNotifications::sendUpstreamMessage | ( | const String & | serverSenderId, |
const String & | collapseKey, | ||
const String & | messageId, | ||
const String & | messageType, | ||
int | timeToLive, | ||
const StringPairArray & | additionalData ) |
Android only: sends an upstream message to your app server.
The server must implement XMPP Connection Server protocol (refer to Firebase documentation).
serverSenderId | Represents the sender. Consult your Firebase project settings to retrieve the sender id. |
collapseKey | Remote messages with the same collapse key that were not yet delivered will be collapsed into one, with the newest message replacing all the previous ones. Note that there may be a limit of maximum collapse keys used at the same time and beyond the limit (refer to Firebase documentation) it is not guaranteed which keys will be in use by the server. |
messageId | A unique message ID. Used in error callbacks and debugging. |
messageType | Message type. |
timeToLive | TTL in seconds. If 0, the message sending will be attempted immediately and it will be dropped if the device is not connected. Otherwise, the message will be queued for the period specified. |
additionalData | Collection of key-value pairs to be used as an additional data for the message. |
void PushNotifications::addListener | ( | Listener * | l | ) |
void PushNotifications::removeListener | ( | Listener * | l | ) |