Hi all, Nick here. I will be talking about our newest feature in our app: the regular background checking of IVLE announcements.
Previously, the user had to manually press the refresh button in the announcements page to retrieve any new announcements from the modules. This might cause the user to miss some important announcements because it is not intuitive to manually keep on checking the announcements. So we decided to ask the app to do the checking for the user instead!
The user can set the time interval between each checking. Timings range from 1 hour to 24 hours. In our current app, we have an option for a 15 seconds interval, but this is intended for debugging and testing purposes only. Nobody should be so crazy to always want 15-second updates from IVLE. On the other hand, if the user does not want regular updates from IVLE, the feature can be turned off in the settings page.
The regular checking is implemented using the AlarmManager class given in the Android API. We normally think of alarms as the ring-ring machine that wakes us up in the morning. Well, the Android AlarmManager is similar. It can set a specific time, interval and repetitions for certain activities to happen on the phone. We make use of this convenient functionality for our app.
As mentioned previously, the user can set the refresh interval in the app's settings page. Once set, the app will then call the AlarmManager and make it set it's own "alarm" for the refresh Service. A Service in Android is just a background process with no UI elements. We use this Service to call the IVLE API that gives us unread announcements. The phone's "alarm" works by simply activating the refresh Service when the time is reached (it does not actually ring the real alarm in your clock). The Service then parses the data received and then informs the user if there are any new and unread announcements.
How does the app inform the user then? It simply shows a notification at the notification bar. This is done easily with the class called NotificationCompat. The actual class is supposed to be simply Notification, but the Compat is appended because this is a special class that allows your notification to show on older versions of Android (Compatibility). The Notification class is updated specifically for the newest versions of Android only. We thus use the notification builder supplied with the NotificationCompat class and this allows us to easily show the notification on the phone.
We encountered problem while doing this feature. We sometimes were unable to cancel the checking of the notifications because the cancel function provided by the AlarmManager is not very intuitive. We managed to fix it but the result is pretty messy spaghetti code. Also, we still have no idea why the original way we did it does not work. This shall be a notable issue should any of us do a similar feature in other apps in the future. Hopefully we can further understand the problem next time.
Alright that's all, hope it has been an interesting read. If you would like more technical details, feel free to browse our source code in our Github. The link is in the sidebar. Alternatively, you may comment below or contact us directly :)
Thanks for reading!