Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Sending custom Push Notifications to the Moodle App: Difference between revisions

From MoodleDocs
(English corrections)
Line 1: Line 1:
{{Moodle App (Ionic 5)}}
{{Moodle App (Ionic 5)}}
There are two ways to send custom Push Notifications to the Moodle app users:
There are two ways to send custom Push Notifications to the Moodle app users:
# By using Moodle’s Message API implementing a message provider  
# By using Moodle’s Message API implementing a message provider  
Line 6: Line 5:




'''Main differences are:'''
'''The main differences are:'''


By using Moodle’s Message API, users (students, teachers, etc..) can opt-out to not receive custom notifications, they can also opt-in to receive them not only on the app but also via email. Messages sent using this API can arrive to non app users.
By using Moodle’s Message API, users (students, teachers, etc..) can opt-out to not receive custom notifications. They can also opt-in to receive them not only on the app but also via email. Messages sent using this API can be delivered to non-app users.


Using the Airnotifier API will force the user to receive the notification unless they change their app settings to not receive any notification.
Using the Airnotifier API will force the user to receive the notification unless they change their app settings to not receive any notification.
Messages sent using this API will only arrive to active app users.
Messages sent using this API will only get to active app users.
 
In both cases please remember that the number of user devices that can receive Push Notifications is dictated by your app [https://moodle.com/app/ subscription].


In both cases, please remember that the number of user devices that can receive Push Notifications is dictated by your app [https://moodle.com/app/ subscription].
== Moodle’s Message API ==
== Moodle’s Message API ==
Please read the official Moodle documentation, [[Message API]], where it is explained how to create a new message provider within a plugin.
Please read the official Moodle documentation, [[Message API]], where it is explained how to create a new message provider within a plugin.


Apart from what it is explained in the [[Message API]] please notice that it is possible to add a customdata field (json-encoded) with additional parameters. Those parameters are the ones described in the Airnotifier API section bellow.
Apart from what is explained in the [[Message API]] please notice that it is possible to add a "customdata" field (json-encoded) with additional parameters. Those parameters are the ones described in the Airnotifier API section below.
== Airnotifier API ==
== Airnotifier API ==
Airnotifier is the name of the Push Notification server used by Moodle. It is possible to use its own API to send custom notifications from Moodle without having to use Moodle's Message API.
Airnotifier is the name of the Push Notification server used by Moodle. It is possible to use its own API to send custom notifications from Moodle without having to use Moodle's Message API.
=== Payload format ===
=== Payload format ===
'''Mandatory fields:'''
'''Mandatory fields:'''
* processor (string): Always set to moodle
* processor (string): Always set to Moodle
* notification (number): Whether it’s a notification (1) or a message (0).
* notification (number): Whether it is a notification (1) or a message (0).
* subject (string): Notification title.
* subject (string): Notification title.
* smallmessage (string): Notification body, short text.
* smallmessage (string): Notification body, short text.
* fullmessage (string): Notification body, ignored if smallmessage is set.
* fullmessage (string): Notification body, ignored if smallmessage is set.
'''Optional'''
'''Optional'''
* site (string): Site ID (md5 hash of site URL + username)
* site (string): Site ID (md5 hash of site URL + username)
Line 35: Line 32:
* component (string): Moodle’s component that generated the notification
* component (string): Moodle’s component that generated the notification
* contexturl (string): URL the notification is related to
* contexturl (string): URL the notification is related to
* customdata (json encoded object, all fields are optional)
* customdata (JSON encoded object, all fields are optional)
** notificationiconurl (string): Icon to display in the notification (Android only).
** notificationiconurl (string): Icon to display in the notification (Android only).
** notificationpictureurl (string). Large picture to display in the notification (Android only).
** notificationpictureurl (string). Large picture to display in the notification (Android only).
** tokenpluginfile (string): Token to view the icon if needed.
** tokenpluginfile (string): Token to view the icon if needed.
** extendedtext (string). An extended text (HTML), opened in popup when clicked.  
** extendedtext (string). An extended text (HTML), opened in popup when clicked.  
** appurl (string): When notification is clicked, open this URL. Has preference over contexturl but it will be ignored if extendedtext is set.
** appurl (string): When notification is clicked, open this URL. It has preference over contexturl but it will be ignored if extendedtext is set.
** appurlopenin (string): Where to open the previous URL ‘browser’, ‘inapp’ or any other value.
** appurlopenin (string): Where to open the previous URL ‘browser’, ‘in-app’ or any other value.
 
=== Sample CURL requests ===
=== Sample CURL requests ===
 
'''Simple notification, only subject and body:'''
'''Simple notification, only subject and body'''
  curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN",  "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test"}}'
  curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN",  "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test"}}'
'''Notification including an icon and picture (only displayed on Android devices) opening a popup with custom content on the app'''
'''Notification including an icon and picture (only displayed on Android devices) opening a popup with custom content on the app:'''
  curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN", "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test", "customdata": "{\"extendedtext\" : \"Extended tex that will open in a popupt\", \"notificationiconurl\" : \"https://picsum.photos/50\", \"notificationpictureurl\" : \"https://picsum.photos/200\"}"}}'
  curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN", "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test", "customdata": "{\"extendedtext\" : \"Extended tex that will open in a popupt\", \"notificationiconurl\" : \"https://picsum.photos/50\", \"notificationpictureurl\" : \"https://picsum.photos/200\"}"}}'
'''Notification opening URL that can be handled by the app (a course within the app)'''
'''Notification opening URL that can be handled by the app (a course within the app):'''
  curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN", "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test", "customdata": "{ \"appurl\" : \"https://mymoodlesite.net/course/view.php?id=18\", \"notificationiconurl\" : \"https://picsum.photos/50\", \"notificationpictureurl\" : \"https://picsum.photos/200\"}"}}'
  curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN", "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test", "customdata": "{ \"appurl\" : \"https://mymoodlesite.net/course/view.php?id=18\", \"notificationiconurl\" : \"https://picsum.photos/50\", \"notificationpictureurl\" : \"https://picsum.photos/200\"}"}}'
'''AIRNOTIFIER_URL''': Can be obtained from your Moodle site settings: Site administration > Messaging > Mobile.


'''AIRNOTIFIER_URL''': Can be obtained from your Moodle site settings: Site administration > Messaging > Mobile
'''APP_ID''': commoodlemoodlemobile for the standard Moodle app, it can also be obtained from Site administration > Messaging > Mobile.


'''APP_ID''': commoodlemoodlemobile for the standard Moodle app, it can also be obtained from Site administration > Messaging > Mobile
'''AIRNOTIFIER_ACCESS_KEY''': Your Airnotifier Access key can be obtained from Site administration > Messaging > Mobile.


'''AIRNOTIFIER_ACCESS_KEY''': Your Airnotifier Access key can be obtained from Site administration > Messaging > Mobile
'''DEVICE_TOKEN''': The user’s receiving the notification device token, for testing purposes you can get it via the app -> App settings -> About -> Click at app version -> Push notification ID.
 
'''DEVICE_TOKEN''': The user’s receiving the notification device token, for testing purposes you can get it via the app -> App settings -> About -> Click at app version -> Push notification ID


In Moodle the push ids are in the user_devices table (pushid field), the previous table has to be joined with message_airnotifier_devices to obtain active devices.  
In Moodle the push ids are in the user_devices table (pushid field), the previous table has to be joined with message_airnotifier_devices to obtain active devices.  


The device could be android-fcm or ios-fcm (in the example we used android-fcm only)
The device could be android-fcm or ios-fcm (in the example we used android-fcm only).
 
[[Category:Mobile]]
[[Category:Mobile]]

Revision as of 10:43, 31 March 2022


There are two ways to send custom Push Notifications to the Moodle app users:

  1. By using Moodle’s Message API implementing a message provider
  2. By directly using the Airnotifier (Moodle’s Push Notification server) APIs


The main differences are:

By using Moodle’s Message API, users (students, teachers, etc..) can opt-out to not receive custom notifications. They can also opt-in to receive them not only on the app but also via email. Messages sent using this API can be delivered to non-app users.

Using the Airnotifier API will force the user to receive the notification unless they change their app settings to not receive any notification. Messages sent using this API will only get to active app users.

In both cases, please remember that the number of user devices that can receive Push Notifications is dictated by your app subscription.

Moodle’s Message API

Please read the official Moodle documentation, Message API, where it is explained how to create a new message provider within a plugin.

Apart from what is explained in the Message API please notice that it is possible to add a "customdata" field (json-encoded) with additional parameters. Those parameters are the ones described in the Airnotifier API section below.

Airnotifier API

Airnotifier is the name of the Push Notification server used by Moodle. It is possible to use its own API to send custom notifications from Moodle without having to use Moodle's Message API.

Payload format

Mandatory fields:

  • processor (string): Always set to Moodle
  • notification (number): Whether it is a notification (1) or a message (0).
  • subject (string): Notification title.
  • smallmessage (string): Notification body, short text.
  • fullmessage (string): Notification body, ignored if smallmessage is set.

Optional

  • site (string): Site ID (md5 hash of site URL + username)
  • Siteurl (string): Site URL (used to identify the site the notification is coming from)
  • wwwroot (string): Moodle’s $CFG->wwwroot
  • component (string): Moodle’s component that generated the notification
  • contexturl (string): URL the notification is related to
  • customdata (JSON encoded object, all fields are optional)
    • notificationiconurl (string): Icon to display in the notification (Android only).
    • notificationpictureurl (string). Large picture to display in the notification (Android only).
    • tokenpluginfile (string): Token to view the icon if needed.
    • extendedtext (string). An extended text (HTML), opened in popup when clicked.
    • appurl (string): When notification is clicked, open this URL. It has preference over contexturl but it will be ignored if extendedtext is set.
    • appurlopenin (string): Where to open the previous URL ‘browser’, ‘in-app’ or any other value.

Sample CURL requests

Simple notification, only subject and body:

curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN",  "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test"}}'

Notification including an icon and picture (only displayed on Android devices) opening a popup with custom content on the app:

curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN", "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test", "customdata": "{\"extendedtext\" : \"Extended tex that will open in a popupt\", \"notificationiconurl\" : \"https://picsum.photos/50\", \"notificationpictureurl\" : \"https://picsum.photos/200\"}"}}'

Notification opening URL that can be handled by the app (a course within the app):

curl AIRNOTIFIER_URL/api/v2/push -X POST -H "X-AN-APP-NAME: APP_ID" -H "X-AN-APP-KEY: AIRNOTIFIER_ACCESS_KEY" --data '{"device": "android-fcm", "token": "DEVICE_TOKEN", "extra": {"processor" : "moodle", "notification": 1, "subject": "Title test", "fullmessage": "Message test", "customdata": "{ \"appurl\" : \"https://mymoodlesite.net/course/view.php?id=18\", \"notificationiconurl\" : \"https://picsum.photos/50\", \"notificationpictureurl\" : \"https://picsum.photos/200\"}"}}'

AIRNOTIFIER_URL: Can be obtained from your Moodle site settings: Site administration > Messaging > Mobile.

APP_ID: commoodlemoodlemobile for the standard Moodle app, it can also be obtained from Site administration > Messaging > Mobile.

AIRNOTIFIER_ACCESS_KEY: Your Airnotifier Access key can be obtained from Site administration > Messaging > Mobile.

DEVICE_TOKEN: The user’s receiving the notification device token, for testing purposes you can get it via the app -> App settings -> About -> Click at app version -> Push notification ID.

In Moodle the push ids are in the user_devices table (pushid field), the previous table has to be joined with message_airnotifier_devices to obtain active devices.

The device could be android-fcm or ios-fcm (in the example we used android-fcm only).