java.lang.Object | |
↳ | com.neomades.notification.push.PushServiceManager |
PushNotification service.
This service is responsible for registering the application with the Operating System Push-Notification service.
Push notifications are a service you can use to broadcast information
(notifications) from a server to the user of your mobile applications. These
notifications may be simple messages or more complex data for the
application. Notifications can be received even if the application is not
currently running. They can appear in various forms, the most common is a
message in the phone's notification center that can be accessed by scrolling
down the status bar on both Android phones and iPhone.
Push notification involves three parts:
Platform portal:
How to broadcast:
How to receive:
The following two steps are required to receive push messages:
The Firebase Cloud Messaging service is used.
In order to use this service, you need to create a Firebase project as described in the official documentation.
Note that the configuration of the Android project is done by NeoMAD, the only things you need to provide is the google-services.json file which can be obtained in the Firebase portal of the application.
The Apple Push Notification Service is used.
In order to use this service, you should refer to the official documentation.
To sum up, the process is as follow :
NB. In order to test an iOS application using APNs, you cannot use the simulator but a physical device.
In order for an application to be able to receive push notifications, the URS
must declare the pushservice
tag. This will indicate
to NeoMAD that the application uses push notifications.
<pushservice />
<pushservice googleServices="path_to_google_services_json"/>
The pushservices
section of the URS
automatically handles the required permissions declarations.
To be able to receive push notifications through the application, the device
where the application is running must register to the push notification
service. This can be done in the application whenever you want using
PushServiceManager
:
// Get the default NeoMAD PushServiceManager instance PushServiceManager manager = PushServiceManager.getDefault(); // Register the device to the push notification service manager.register();
This will register the device to the push service whatever is its platform.
The push service will transmit a registration identifier to the device. This
identifier must be use by your third party server to send push notifications
to the device. Consequently, the application must transfer the identifier to
the server, via a https connection for example, in order to be able to receive
push notifications. The identifier can be accessed thanks to
getCurrentID()
.
if (manager.isRegistered()) { // Get the device push service registration identifier, if the device is registered String identifier = manager.getCurrentID(); }
NB. The modification of the service registration status can be followed by
overriding onPushRegistrationChanged(int, String)
.
Once the device is registered to the push service, it can receive push
messages from a third party server for example. To do so, simply override
onPushMessageReceived(PushMessage)
. The
PushMessage
will contain all the information sent by the server.
Up to you to present the information to the user.
Be aware that the application can receive the push message whereas it is in
background. In that case, calls to UI or life cycle methods may cause the
application to crash or to have unexpected behavior. To present data to the
user from push message, prefer the usage of Notification
. If the data
does not need to be presented to the user immediately, prefer to save them in
the preferences or in files and load them when the application restarts.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String |
getCurrentID()
Returns the registration device ID (provided by the OS Push-Notification
service server) if the device is registered.
| ||||||||||
static PushServiceManager |
getDefault()
Gets the default Push Notification Service manager.
| ||||||||||
boolean |
isRegistered()
Returns
true if the device is registered by the Operating System
Push Notification service server. | ||||||||||
void |
register()
Calls the Push Notification service server to register the current device.
| ||||||||||
void |
unregister()
Calls the OS Push-Notification service server to unregister the current
device.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Returns the registration device ID (provided by the OS Push-Notification service server) if the device is registered.
Gets the default Push Notification Service manager.
Returns true
if the device is registered by the Operating System
Push Notification service server.
Calls the Push Notification service server to register the current device.
If the service is already registered, this method will do nothing.
Calls the OS Push-Notification service server to unregister the current device.
If the service is already unregistered, this method will do nothing.