The NotificationManager sample demonstrates how you can present a notification to the user.
The following figure illustrates the main view of the NotificationManager.
Figure: NotificationManager main view
The application opens with the Notification tab, which provides access to various notification features. Click the list items to view the features.
The Ongoing notification tab provides access to on-going notifications.
To move between tabs, click the tabs in the header.
This overview consists of the following parts:
- Managing Notifications
Manage notifications and use the application ID and application control with normal notifications.
- Managing On-going Notifications
Manage on-going notifications and use the application ID and application control with on-going notifications.
- Displaying Results
Display a notification result on the screen.
Managing Notifications
The notification management is implemented in the <notifications.c> file.
The create_notification() function sets the default image, sound, and text.
When the user clicks the list item related to the creation of a normal notification, the _tab_view_layout_fill_cb() function calls the appropriate notification to create the result layout function.
The function to create notifications sets the request message information, such as the alert text, title text, and launch arguments, which are used in the result layout function.
Manage notifications in the following ways:
- Manage normal notifications.
If the user clicks the list item related to the creation of the notification, the notify_cb() or notify_normal_cb() function is called to create the notification.
If the user clicks the list item related to the removal of a notification, the remove_notification_cb() function is called to remove the normal and simple notification.
static void create_notification(notification_data *notify_data) { // Remove all notifications before creating a new one notify_data->notification = create_notification(MAIN_MENU_PATH, title_text, alert_text, NULL); if (notify_data->notification) { notification_status_message_post(app_message_text); notification_post(notify_data->notification); } // Set the information result message } static void remove_notification_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); bool is_success = delete_notification_items(); // Set the information result message badge_remove(PACKAGE); }
- Manage notifications using the application control.
If the user clicks the list item related to the creation of the notification by the application control, the notify_by_app_control_cb() function is called to create the notification by the application control. The function sets extra data to the appcontrol, so the data can be used after passing the control to the application.
If the user clicks the list item related to the removal of a notification by the application control, the remove_notification_cb() function is called to remove the appcontrol notification.
static void notify_by_app_control_cb(notification_data *notify_data) { // Remove all notifications before creating a new one notify_data->notification = create_notification(ICON2_PATH, notification_app, app_control, NULL); if (notify_data->notification) { notification_status_message_post(app_message_text); const char *array_result[] = {result_1_text, result_2_text, NULL}; const char *array_key[] = {SERVICE_DATA_TEXT, SERVICE_DATA_TO, NULL}; // Set extra data to app control notification_post(notify_data->notification); } // Set the information result message } static void remove_notification_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); bool is_success = delete_notification_items(); // Set the information result message badge_remove(PACKAGE); } void launch_arguments_set(notification_h notification, const char *argument, ...) { app_control_h service = NULL; int err = app_control_create(&service); RETM_IF(err != SERVICE_ERROR_NONE, "service_create failure "); if (argument) { app_control_add_extra_data(service, MESSAGE_POST, argument); } }
- Manage notifications using the application ID.
If the user clicks the list item related to the creation of the notification by the application ID, the set_badge_number_by_app_id_cb() function is called to create the notification by the application ID.
If the user clicks the list item related to the removal of a notification by the application ID, the remove_badge_number_by_app_id_cb() function is called to remove the notification by the application ID.
static void notify_by_app_id_cb(notification_data *notify_data) { // Remove all notifications before creating a new one notify_data->notification = create_notification(ICON2_PATH, title_text, notify_with_request, NULL); if (notify_data->notification) { notification_status_message_post(app_message_text); // Set App package to app control notification_post(notify_data->notification); } // Set the information result message } static void remove_notification_by_app_id_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); bool is_success = delete_notification_by_app_id(); // Set the information result message } void launch_arguments_set(notification_h notification, const char * pkgname, ...) { app_control_h service = NULL; int err = app_control_create(&service); RETM_IF(err != SERVICE_ERROR_NONE, "service_create failure "); if (pkgname) { app_control_set_app_id(service, pkgname); } }
- Manage badge numbers using the application ID.
If the user clicks the list item related to increases in the badge number, the set_badge_number_by_app_id_cb() function is called to increase the badge number.
To remove the badge number using the application ID, decrease the badge number or remove it if the badge count equals to 0.
static void set_badge_number_by_app_id_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); unsigned int count = 0; bool is_success = increase_badge(&count); // Set the information result message } static void remove_badge_number_by_app_id_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); unsigned int count = 0; int err = BADGE_ERROR_NONE; err = badge_get_count(PACKAGE, &count); if (count == 0) { err = badge_remove(PACKAGE); } else { err = badge_set_count(PACKAGE, --count); } // Set the information result message }
Managing On-going Notifications
The on-going notification management is implemented in the <ongoing-notifications.c> file.
The create_ongoing_notification() function creates an on-going notification. This function sets the notification property and image and title text.
static notification_h create_ongoing_notification(const char *image_path, const char *title_text) { notification_h ongoing_notification = notification_create(NOTIFICATION_TYPE_ONGOING); RETVM_IF(!ongoing_notification, NULL, "notify is NULL"); notification_set_property(ongoing_notification, NOTIFICATION_PROP_DISABLE_TICKERNOTI); notification_set_image(ongoing_notification, NOTIFICATION_IMAGE_TYPE_ICON, image_path); notification_set_text(ongoing_notification, NOTIFICATION_TEXT_TYPE_TITLE, title_text, NULL, NOTIFICATION_VARIABLE_TYPE_NONE); return ongoing_notification; }
The delete_ongoing_notification() function removes the on-going notification, resets the press count and progress value of this notification.
static int delete_ongoing_notification(notification_data *data) { int err = NOTIFICATION_ERROR_NONE; if (data) { if (data->notification) { err = notification_delete(data->notification); data->notification = NULL; } data->press_count = 0; data->progress_value = 0; } return err; }
When the user clicks the list item related to the creation of an on-going notification, the _tab_view_layout_fill_cb() smart callback is invoked. This smart callback calls an on-going notification function and creates the result layout with request message information. The on-going notification function sets the result information which is used in the result layout.
You can:
- Manage different types of on-going notifications.
If the user clicks the list item related to the creation of an on-going notification, the proper function is called according to the on-going activity type, such as percentage, byte, or text.
If the user clicks the list item related to the removing of an on-going notification, the remove_ongoing_notification_cb() function is called to remove the percentage, byte, and text of on-going notifications.
static void ongoing_notification_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); notify_data->press_count++; if (!notify_data->notification) { notify_data->notification = create_ongoing_notification(ICON2_PATH, notify_data->name); } RETM_IF(!notify_data->notification, "ongoing_notification is NULL"); // Set progress to notification notify_data->progress_value += percentage_increment; if (notify_data->progress_value > percentage_max) { notify_data->progress_value = 0; } notification_set_progress(notify_data->notification, notify_data->progress_value); notification_content_text_set(notify_data->notification, percentage_content_text, notify_data->press_count); // Set the information result // If a notification of such type is already created then just update it, if not, post it } // The ongoing_notification_byte_cb and ongoing_notification_text_cb functions are // similar to the above ongoing_notification_cb function static void remove_ongoing_notification_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); notification_data *data = ongoing_notification_data_get(ONGOING_NOTIFICATION_PERCENT); int err = delete_ongoing_notification(data); data = ongoing_notification_data_get(ONGOING_NOTIFICATION_BYTE); err = delete_ongoing_notification(data); data = ongoing_notification_data_get(ONGOING_NOTIFICATION_TEXT); err = delete_ongoing_notification(data); // Set information result }
- Manage on-going notifications using the application control.
To manage on-going notifications that use implicit appcontrol resolution, use the ongoing_notification_by_app_control_cb() function. It sets the extra data to appcontrol, so the data can be used after passing the control to the application.
To remove on-going notifications that use implicit appcontrol resolution, use the remove_notification_by_app_control_cb() function.
static void ongoing_notification_by_app_control_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); notify_data->press_count++; if (!notify_data->notification) { notify_data->notification = create_ongoing_notification(ICON1_PATH, notify_data->name); } RETM_IF(!notify_data->notification, "ongoing_notification is NULL"); // Set progress to notification notification_content_text_set(notify_data->notification, percentage_content_text, notify_data->press_count); // Increase badge // Set the information result // Set extra data to appcontrol // If a notification of such type is already created then just update it, if not, post it } void launch_arguments_set(..., const char *argument, ...) { app_control_h service = NULL; int err = app_control_create(&service); RETM_IF(err != SERVICE_ERROR_NONE, "service_create failure "); // Set extra data to appcontrol if (argument) { app_control_add_extra_data(service, MESSAGE_POST, argument); } } static void remove_notification_by_app_control_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); notification_data *data = ongoing_notification_data_get(ONGOING_NOTIFICATION_BY_APP_CONTROL); int err = delete_ongoing_notification(data); // Remove badge // Set the information result }
- Manage on-going notifications using the application ID.
To notify a specified user using the application ID of the on-going activity, the ongoing_notification_by_app_id_cb() function is called. It sets the application ID to the appcontrol, so the proper application is called.
The remove_ongoing_notification_by_app_id_cb() function is used to remove the notification for the specified application ID.
static void ongoing_notification_by_app_id_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); notify_data->press_count++; if (!notify_data->notification) { notify_data->notification = create_ongoing_notification(ICON1_PATH, notify_data->name); } RETM_IF(!notify_data->notification, "ongoing_notification_byte is NULL"); // Set the initial size for the ongoing type notification_content_text_set(notify_data->notification, byte_content_text, notify_data->press_count); // Set information result // Set application ID to appcontrol // If a notification of such type is already created then just update it, if not - post it } void launch_arguments_set(..., const char *argument, ...) { app_control_h service = NULL; int err = app_control_create(&service); RETM_IF(err != SERVICE_ERROR_NONE, "service_create failure "); // Set application ID to appcontrol if (pkgname) { app_control_set_app_id(service, pkgname); } } static void remove_ongoing_notification_by_app_id_cb(notification_data *notify_data) { RETM_IF(!notify_data, "notify_data is NULL"); notification_data *data = ongoing_notification_data_get(ONGOING_NOTIFICATION_BY_APP_ID); int err = delete_ongoing_notification(data); // Set information result }
Displaying Results
The _tab_view_layout_fill_cb() function calls the appropriate notification function where the result information is set. Then the layout_view_add() function is invoked.
In the layout_view_add() function, the result information is used to create the result layout.
static void _tab_view_layout_fill_cb(void *data, Evas_Object *obj, void *event_info) { notification_data *notification_info = (notification_data *)data; notification_info->callback(notification_info); layout_view_add(navi, notification_info); } static void notify_cb(notification_data *notify_data) { // Set the result information snprintf(notify_data->result_text, TEXT_MAX_LEN, "%s%s<br><br>%s<br>%s<br><br>%s<br>%s<br>", result_message, (notify_data->notification) ? result_message_success_text : result_message_failure_text, notify_message_text, _text, launch_argument_text, app_message_text); } Evas_Object* layout_view_add(Evas_Object *parent, notification_data *notify_info) { // Create the result layout _layout_view_fill(data->layout, notify_info); data->navi_item = elm_naviframe_item_push(data->navi, data->name, NULL, NULL, data->layout, NULL); return data->layout; }