Example Extended Notification Test |
The example creates this notification
with a wide range of functions. The app has a button for creating and deleting the notification:
The buttons Create Notification (cmdCreate) and Remove Notification (cmdRemove) are enabled according to the display state of the notification.
Four actions are added to the notification: Tapping the notification and three action buttons. Every action is defined by an Intent.
The following instances of the components are defined in the project.
Type | Name | Function | |
---|---|---|---|
UrsAI2NotificationChannel | Channel | Notification channel. | |
UrsNotification | Notification | The essential notification. | |
UrsNotification | NotificationDestroyed | Notification to be opened after closing the app. | |
UrsIntent | IntentNone | No action. | |
UrsIntent | IntentOpenUrs | Open Ullis Roboter Seite in the browser. Also works when the app is closed. | |
UrsIntent | IntentRaiseEvent | Triggers the Notification.OnClick event. Only works as long as the app is open (either foreground or background). | |
UrsIntent | IntentScreenNotification | Opens the Screen Notification of the app. Also works when the app is closed. |
The UrsNotification.OnClick event will not be triggered if the associated Screen is closed. After closing the Screen (here Screen1, i.e. the app), the associated action button should no longer be displayed. Unfortunately, the App Inventor does not allow the Activity.onDestroy event to be passed on to the app. This means that there no way of updating the notification accordingly by an event. Therefore, a suitable notification is registered via the UrsNotification.SetOnDestroyAction function, which is opened when the app is closed. It should be noted that the internally necessary objects are created when this function is called1). If changes to the UrsNotificationObject or UrsIntentObject are to be made, the function must be called again. Otherwise the change would have no effect.
1) If the app is closed from the app overview (history), only a limited time and a limited range of functions are available (tested with version Android Oreo, 8.1). This is not enough to create the objects at the time when closing the app.
When the screen is initialized, the action buttons are added to the notification objects. NotificationDestroyed gets one less button. The red color of the first button is achieved by a hypertext tag.
Notification.SetOnDestroyAction specifies that the notification defined by NotificationDestroyed is displayed when the app is closed. NotificationDestroyed and Notification have the same ID (NumberID field). So NotificationDestroyed will replace Notification. NotificationDestroyed would also be displayed if Notification was not displayed or has already been deleted. To prevent this, SetOnDestroyAction is only called when the Notificationis really displayed. When detecting that the notification has been deleted, NotificationDestroyed is removed too.
The buttons Create Notification (cmdCreate) and Remove Notification (cmdRemove) are enabled according to the display state of the notification.
Finally, the notification panel is opened so that you can view the created notification directly.
The cmdRemove button deletes both the notification that is displayed and the notification that should be displayed when the app is closed.
If the user cancels the notification, the notification that should be displayed when the app is closed must be removed too. The user interface shows that the user has deleted the notification.
The Raise Event action button triggers the OnClick event.