Mobile native Wearable native

Badge: Displaying the Notification (Badge) Count on the Home Screen

This tutorial demonstrates how you can create, manage, and remove a badge for an application. A badge is an image displayed on the application icon, which informs the user about notifications and events.

Warm-up

Become familiar with the Badge API basics by learning about:

Initializing the Badge Functionality

To initialize the badge functionality:

  1. To use the functions and data types of the Badge API (in mobile and wearable applications), include the <badge.h> header file in your application:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <badge.h>
    

    To ensure that a Badge function has been executed properly, make sure that the return is equal to BADGE_ERROR_NONE.

  2. Add the http://tizen.org/privilege/notification privilege to the manifest file of the application to manage badges.
  3. Declare the variables for the return value and application (package name):
    static int ret = 0;
    
    #define TEST_PKG "org.tizen.badgeapp"

Creating and Removing a Badge

To create and remove badges:

  • To create a badge for an application, call the badge_add() function.

    The parameter defines the application for which the badge is added. If the application is adding a badge for itself, the parameter can be null.

    ret = badge_add(TEST_PKG);
    if (ret != BADGE_ERROR_NONE)
    {
       // Error handling
    }
    
  • To remove the badge from the application, call the badge_remove() function. The only parameter is the ID of the application whose badge to remove.

    ret = badge_remove(TEST_PKG);
    if (ret != BADGE_ERROR_NONE)
    {
       // Error handling
    }
    

Managing the Badge

To manage the badge:

  • Get the count attribute with the badge_get_count() function. The count attribute is displayed in the right corner of the badge and its value must be an integer.

    The parameters are the name of the application whose badge count is retrieved, and the current badge count.

    unsigned int count = 0;
    ret = badge_get_count(TEST_PKG, &count);
    if (ret != BADGE_ERROR_NONE)
    {
       // Error handling
    }
    
  • Set the count attribute with the badge_set_count() function.

    The parameters are the name of the application whose badge count is updated, and the new badge count.

    ret = badge_set_count(TEST_PKG, count+1);
    if (ret != BADGE_ERROR_NONE)
    {
       // Error handling
    }
    
  • Get the display attribute and check whether the badge is visible with the badge_get_display() function.

    The parameters are the application which owns the badge, and the value of the display attribute (1 = badge is visible, 0 = badge is hidden).

    unsigned int is_displayed = false;
    ret = badge_get_display(TEST_PKG, &is_displayed);
    if (ret != BADGE_ERROR_NONE)
    {
       // Error handling
    }
    
  • Set the display attribute with the badge_set_display() function to hide or show the badge.

    The parameters are the application which owns the badge, and the display attribute (1 = badge is visible, 0 = badge is hidden).

    ret = badge_set_display(TEST_PKG, 1);
    if (ret != BADGE_ERROR_NONE)
    {
       // Error handling
    }
    
Go to top