Sending and Receiving Push Notifications
Sending push notifications
After the application is installed on your mobile device, you can send push notifications to your mobile devices.
Android
To send push notifications to devices running your application, you need to define your app in the Google Firebase Developer Console and build a custom client app (Since version: 3.2a):
-
Open the Google Firebase Developer Console.
-
Click the Create New Project button.
-
Enter your application name in the Project name field, select your country, and then click the Create Project button to create your new project in Firebase.
-
Click Add Firebase to your Android app, and then enter your application package name in the Package name field.
-
Click the Add App button. A google-services.json file for your app is downloaded.
-
Click Continue, followed by Finish.
-
Place the google-services.json file in the RIAModules\Android\Source\app folder in place of the existing file.
-
Build the custom client.
Get the Server key required to send notifications:
-
Go to the Google Firebase Developer Console and open your project.
-
On the left side of the screen, click on the Settings icon next to your application’s name and select Project Settings.
-
Click the Cloud Messaging tab to get the Server key.
After the application is properly configured and installed on your device, you need to get the device ID. The device gets a unique ID from the FCM (Firebase Cloud Messaging) service when the device registers itself to the FCM service. When starting the application on the device, the registration process to the FCM will start and the device will get an ID. You can retrieve this ID from the application using the ClientOSEnvGet('device_udf|getpushid') function.
You should add logic in your application (usually after the logon stage) to save the device ID to a database, so then the ID can be used to send messages to the device later on.
You can find a sample program with this code in the Rich Client Samples project as program RNC06.
Note: There is a possibility that the device ID will be changed during execution. Usually this is not a concern since you will get the device ID each time the device is connected to the server (after the logon stage). If you do wish to handle this scenario, you can change the MyFirebaseInstanceIDService.Java file in the src folder by unmarking the end event line in the ChangeRegistrationToServer method so that when the device ID is changed, an event will be sent to the Magic app. You also need to add a handler in your app for this event and update the device ID in the device list.
Push notifications can be sent to your device from various third party software packages. You can also send them from a Magic xpa application by sending an HTTP Post request. To do this, evaluate the following expression:
HTTPCall('POST', 'https://fcm.googleapis.com/fcm/send', '{"registration_ids" : ["'&Trim(B)&'"],"data" : {"body" : "' & Trim(C) & '"}}' , 'Content-Type:application/json', 'Authorization: key='&Trim(D))
Where:
-
B is a comma-delimited string of device IDs (such as "AXDSFGFSDSFSGSF")
-
C is the text to be sent (such as 'Hello World ')
-
D is the API key retrieved in the first step (such as 'AfdsSyg5ENO6jxUPQvUpIduydn53DlYiiGPTxUM')
Note: Ports 5228, 5229 and 5230 must be opened on the firewall.
You cannot send a notification to your app’s users from the Firebase Notifications Console since the message sent needs to be sent with the data token.
You can find a sample program with this code in the Online and Rich Client Samples projects as programs CN20 and RCN20.
iOS
To send push notifications to devices running your application, you should first:
-
Define an application on the iOS developer portal.
-
Create an SSL certificate.
-
Enable the Apple Push Notification service for this application using the SSL certificate.
To do this, follow the steps in the Creating the SSL Certificate and Keys section on the Apple website.
Refer also to:
After the application is properly configured and installed on your device, you need to get the device ID. The device gets a unique ID from the APN service when the device registers itself to the APN service. When starting the application on the device, the registration process to the APN will start and the device will get an ID. You can retrieve this ID from the application using the ClientOSEnvGet('device_udf|getpushid') function.
You should add logic in your application (usually after the logon stage) to save the device ID to a database, so then the ID can be used to send messages to the device later on.
You can find a sample program with this code in the Rich Client Samples project as program RNC06.
Push notifications can be sent to your device from various third-party software packages.
For example:
Note:
You can find a sample program that uses the PushSharp .NET library in the Online and Rich Client Samples projects as programs CN20 and RCN20.
Handling push notifications in the application
When the push notification is received in the device and the user clicks on the notification:
-
If the application is closed, the application will be launched.
You can get the push notification message by using the following expression: ClientOSEnvGet('device_udf|getargs')
-
If the application is opened, the External Event will be raised.
The push notification message will be available in the event parameter.
The notification message syntax is "GCM-message:" + the notification message.
Note: GCM is deprecated by Android and it is better to use Firebase as explained above. However, if you still want to use GCM, see Using Android GCM Push Notifications with Magic xpa 3.2a and Above.
Since version: 2.4c