Note:

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

Moodle Mobile Push Notifications: Difference between revisions

From MoodleDocs
Line 100: Line 100:
# When the Airnotifier provider tries to send a message to a deleted device, it stores the error and send a unique alert to the device user. The device is disabled. After a month, the cron processes delete the device.
# When the Airnotifier provider tries to send a message to a deleted device, it stores the error and send a unique alert to the device user. The device is disabled. After a month, the cron processes delete the device.


== Payload format ==
== Payload content ==
The current payload format would need a bit of brainstorming. We need to identify what to send as it is extremely short.  
The current payload content would need a bit of brainstorming. We need to identify what to send as it is extremely short. The app will behave following the content it receives.  
=== Apple ===
=== Apple ===
The Apple payload is limited to 256bytes and this include any params! Our current implementation is still quite a wip:
The Apple payload is limited to 256bytes and it includes everything sent. Our current implementation is still quite of a wip:
<code php>
<code php>
{type:'moodle_instantmessage',id:'8',urlparams:'{"user":"2","id":"8"}',userfrom:'Manager Moodle',date:'1367477362',alert:'This is a message text.',}
{type:'moodle_instantmessage',id:'8',urlparams:'{"user":"2","id":"8"}',userfrom:'Manager Moodle',date:'1367477362',alert:'This is a message text.',}

Revision as of 08:29, 2 May 2013

Goal

Moodle Mobile should receive Moodle messages as push notifications on iOS and Android.

How it works

The generic way

App

  1. User runs the mobile app.
  2. User accepts to receive push notification.
  3. User logins on a Moodle site.
  4. App registers the device on airnotifier (Apple device token + site url).
  5. App registers the device on the Moodle site (Apple device ID (or an Airnotifier device ID if it's more secured/generic) to be able to receive the notification and the device name to be able to disable the device)
  6. User enables airnotifier notification in her/his Moodle profile.
  7. Moodle sends a message to Airnotifier server throughout its Airnotifier messaging provider.
  8. Airnotifier dispatches the message to the correct notification system.
  9. The user device receives the push notification.
  10. The app opens on a specific page (following the parameters payload).

Site

  1. Moodle runs Airnotifier messaging provider installation process.
  2. Airnotifier messaging provider requests an access key from the Airnotifier server - Moodle site temporarily opens an access point to confirm its site url.
  3. Airnotifer server has successfully identified the site url, it return the access key (the site can send messages and broadcast).

Apple use case

We start from the mobile device, when user launch the app, app should register it's device token to AirNotifier and Moodle at the same time, to register with AirNotifier, we could be sure the token is valid, the invalid token would disturb TCP connection with apple push notification server, so it's very important we do this. To register with Moodle, so moodle site could associate this token with user ID, when something interesting happened to this user, Moodle site will be able to send notification to certain device

Registering token on AirNotifier: Send POST HTTP request to http://server.name/tokens with app name (it's moodle) and app access key in HTTP header, and token in HTTP body Registering token on Moodle: Sending user ID and token, this should be done by new web service, we probably need a new table to record user tokens, NOTE, user may has multi mobile devices, so it's one-many relationship AirNotifier will issue access key to moodle installations, they share one app name: moodle, we need an admin panel in moodle to set this access key, this access keys only allow sending notifications and broadcasts, they don't have permission to register tokens, we only allow tokens being registered from mobile devices

iPhone app use a separate access key, which will be used to register device token

So we got user's device tokens in Moodle database, when somethings happened, for instance, a new instant message sent to a user who has mobile device registered. We will look for messaging processor in Moodle, this message processor is in charge of sending push notification request to AirNotifier, it will send POST HTTP request to http://server.name/notification with token and message digest in HTTP body, app name (it's moodle) and access key in http header

The message processor should be a new output type message plugin besides email, jabber and and popup In user settings page, user should have options to choose whether or not to send notification to certain devices. Broadcast, moodle site may want to broadcast messages to all registered mobile devices, it's inefficient to send individual notification to AirNotifier, it can be done by using http://server.name/broadcast service, moodle site only send one POST request to this endpoint with access key and app name in HTTP header and md5($CFG->wwwroot) in HTTP body, the hashed wwwroot is the default channel subscribed by all devices registered in AirNotifier, it will take care of broadcasting

Google specificities

Deliverables

  • push notification support on the mobile app - the app receives the push notification and behaves correctly.
  • The Airnotifier server - it sends notification to APNS / GCM.
  • The Airnotifier provider - it sends Moodle messages to Airnotifier - MDL-36445

Airnotifier messaging provider

In your messaging setting page you can select which of your devices can receive push notifications. MDL-36445 The provider has usual messaging provider settings.

Airnotifier server

To install the server follow: https://github.com/dongsheng/airnotifier/wiki/Installation

Need to be done:

App push notification support in the mobile app

  1. Get the token from Apple (through phonegap PushPlugin)
  2. Send the token to Airnotifier server (+ site url). See https://github.com/dongsheng/airnotifier/blob/master/test/token_create.sh. Currently it's not possible to link the site url to the token.
  3. send the token by web service to the Moodle site.

The app is closed or is running in the background

It's a normal OS notification. The user can open the app from it. We need to define what happens when we open the app.

The app is opened

The app displays a notification popup. Or better, it's a small temporary notification message at the top that retarct by itself - less intrusive.

Risks

These risks have been raised by Dan in MDL-36445:

  • Have we load tested it with large numbers of users/notifications?
  • What is stopping someone downloading Moodle getting the keys to DOS our push notification service (surely blacklisting us with apple)
  • If someone gains access to device ids, could they then use our service to spam users?
  • Are we checking the 'feedback service' and removing devices which no longer exist, apple ominously writes "Note: APNs monitors providers for their diligence in checking the feedback service and refraining from sending push notifications to nonexistent applications on devices."

Improvement

See Apu's comment in https://tracker.moodle.org/browse/MDL-36445?focusedCommentId=200343&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-200343. Specially the mention about language.

Workflow brainstorming

current workflow

  1. the app contacts Apple push notification server to get its device token (it's done by the mobile app PushPlugin plugin that itself uses Cocoa API)
  2. after login on the site, the app gets an Airnotifier access key from the site. (this access key is currently the same for each site => meaningless). It's done calling the webservice message_airnotifier_get_access_key(permissions). create_token is the only possible permission.
  3. the app sends its token to Airnotifier server - it's done by a HTTP POST request.
  4. the app registers its device to the site - it's done by a ws call message_airnotifier_add_user_device(appname, devicetoken, devicename)
  5. Airnotifier provider (Moodle site) sends a payload message for a specific device token to airnotifier server - it's done by a HTTP POST

suggested workflow

  1. during Airnotifier provider installation, the site contacts Airnotifer server to request an access key for itself + an access key for the devices (to send their device token to Airnotifier server).
  2. the app contacts Apple push notification server to get its device token.
  3. after login on the site, the app get an access key from the site to add its token to airnotifier. It's done calling the webservice message_airnotifier_get_access_key(type). An admin could request the site access key, the others only get the device access key.
  4. the app sends its device token to Airnotifier server indicating the site url (so airnotifier knows the device/site couple, and so Airnotifier can allow site broadcast) - it's done by a HTTP POST request.
  5. the app registers its device to the site - it's done by a ws call message_airnotifier_add_user_device(appname, devicetoken, devicename)
  6. during this web service, the site confirms the device token to Airnotifier. It's done by a HTTP POST request. Airnotifier server deletes unconfirmed device tokens older than one minute every hours.
  7. Airnotifier provider (Moodle site) sends a message for a specific device token to airnotifier server - it's done by a HTTP POST
  8. Airnotifier checks Apple feedback service every hour and delete the unactive device tokens.
  9. When the Airnotifier provider tries to send a message to a deleted device, it stores the error and send a unique alert to the device user. The device is disabled. After a month, the cron processes delete the device.

Payload content

The current payload content would need a bit of brainstorming. We need to identify what to send as it is extremely short. The app will behave following the content it receives.

Apple

The Apple payload is limited to 256bytes and it includes everything sent. Our current implementation is still quite of a wip: {type:'moodle_instantmessage',id:'8',urlparams:'{"user":"2","id":"8"}',userfrom:'Manager Moodle',date:'1367477362',alert:'This is a message text.',}

GCM

The Apple payload is about 4Kb.