Page Example

Notification Service Sample Overview

Wearable native

The (Rectangle) NotificationManager sample demonstrates how you can present a notification to the user.

The notifications are displayed as list items, where each item is an Edje layout from the list_content.edc file. All items use the same Edje layout.

The following figure illustrates the main screens of the NotificationManager.

Figure: (Rectangle) NotificationManager screens

(Rectangle) NotificationManager screens

The application opens with the main screen, where you can select which notification type to access: notifications or ongoing notifications. Click the notification type to view the list of notifications and their various features.

Implementation

When the user clicks a list item on the main screen, the _add_noti_list() function adds each notification into a feature list.

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 _noti_list_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 parameters, which are used in the result layout function.

You can 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 notification.

    static notification_h
    create_notification(const char *icon_path, const char *title_text, 
                        const char *content_text, const char *sound_path)
    {
       notification_h notify = notification_create(NOTIFICATION_TYPE_NOTI);
       if (notify)
       {
          notification_set_image(notify, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
          notification_set_text(notify, NOTIFICATION_TEXT_TYPE_TITLE, title_text, 
                                NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
          notification_set_text(notify, NOTIFICATION_TEXT_TYPE_CONTENT, 
                                content_text, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
          notification_set_sound(notify, NOTIFICATION_SOUND_TYPE_USER_DATA, sound_path);
       }
    
       return notify;
    }
    
    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. The function sets extra data to the application control, 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)
       {
          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.

    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

    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)
       {
          // Set the 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.

    Before using this function, the badge_new() function must be called.

    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;
       bool is_success = decrease_badge(&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, 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, and resets the press count and progress value of this notification.

static int 
delete_ongoing_notification(notification_data *data)
{
   notification_data *data = ongoing_notification_data_get(type);
   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 == NOTIFICATION_ERROR_NONE);
}

When the user clicks the list item related to the creation of an on-going notification, the _noti_list_cb() smart callback is invoked. This smart callback calls an on-going notification function and creates the result layout with the request message information. The on-going notification function sets the result information which is used in the result layout.

You can manage notifications in different ways:

  • 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 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 the 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 message
    
       // If a notification of such type is already created, 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");
    
       bool is_success = true;
       is_success = delete_ongoing_notification(ONGOING_NOTIFICATION_PERCENT);
       is_success = delete_ongoing_notification(ONGOING_NOTIFICATION_BYTE) && is_success;
       is_success = delete_ongoing_notification(ONGOING_NOTIFICATION_TEXT) && is_success;
    
       // Set the information result message
    }
    
  • 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 the notification
    
       notification_content_text_set(notify_data->notification, percentage_content_text, 
                                     notify_data->press_count);
    
       // Increase the badge
    
       // Set the information result message
    
       // Set extra data to appcontrol
    
       // If a notification of such type is already created, just update it, if not, post it
    }
    
    void 
    launch_arguments_set(..., 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");
    
       bool result = delete_ongoing_notification(ONGOING_NOTIFICATION_BY_APP_CONTROL);
       // Remove the badge
    
       // Set the information result message
    }
    
    static bool 
    delete_ongoing_notification(enum ongoing_notification type)
    {
       notification_data *data = ongoing_notification_data_get(type);
       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 == NOTIFICATION_ERROR_NONE);
    }
    
  • Manage on-going notifications using the application ID.

    To notify a 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 the information result message
    
       // Set the application ID to appcontrol
    
       // If a notification of such type is already created, 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 the 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 the information result message
    }
    

Displaying Results

The _noti_list_cb() function calls the appropriate notification function where the result information is set. Then the result_view_add() function is invoked.

In the layout_view_add() function, the result information is used to create the result layout.

static void 
_noti_list_cb(void *data, Evas_Object *obj, void *event_info)
{
   notification_data *notification_info = (notification_data *)data;
   notification_info->callback(notification_info);
   result_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<br><br> %s%s<br><br> %s<br> %s<br><br> %s<br> %s<br>",
            notify_data->fullname, result_message, 
            (notify_data->notification) ? result_message_success_text : result_message_failure_text, 
            notify_message_text, _text, launch_argument_text, app_message_text);
}

void 
result_view_add(Evas_Object *navi, notification_data *notify_info)
{
   // Create the result layout
   data->navi_item = elm_naviframe_item_push(data->navi, data->name, NULL, NULL, data->layout, NULL);
}