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
Initialize the badge functionality for use.
-
Creating a Badge
Create a badge for an application.
-
Managing the Badge
Get and set the count and display attributes for the badge.
-
Removing the Badge
Remove the badge from the application.
Initializing the Badge Functionality
To initialize badges:
-
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>
- The badge functionality requires the http://tizen.org/privilege/notification privilege.
- Declare the variables for the return value and application (package name):
static int ret = 0; #define TEST_PKG "org.tizen.badgeapp"
To determine if a Badge API function has been correctly executed, check its return value. If the value is different from BADGE_ERROR_NONE, an error has occurred.
Creating a Badge
To create a badge for an application, call the badge_new() function:
badge_new(const char *writable_app_id)
The application calling the badge_new() function has the ownership of the badge. And the writable_app_id parameter means the application which is to be authorized to change the badge.
ret = badge_new(TEST_PKG); if (ret != BADGE_ERROR_NONE) { // Error handling }
Managing the Badge
To manage the badge:
- Get the count attribute:
The count attribute is displayed in the right corner of the badge and its value must be an integer.
To get the count attribute, call the badge_get_count() function:
badge_get_count(const char *app_id, unsigned int *count)
The function takes the following parameters:
- [in] app_id: The name of the application that gets the count of the badge.
- [out] count: The value of the count attribute.
unsigned int count = 0; ret = badge_get_count(TEST_PKG, &count); if (ret != BADGE_ERROR_NONE) { // Error handling }
- Set the count attribute:
To set the count attribute, call the badge_set_count() function:
badge_set_count(const char *app_id, unsigned int count)
The function takes the following parameters:
- [in] app_id: The name of the application that sets the count of the badge.
- [in] count: The new value for the count attribute.
ret = badge_set_count(TEST_PKG, count+1); if (ret != BADGE_ERROR_NONE) { // Error handling }
- Getting the display attribute:
You can check whether the badge is visible by getting its display attribute.
The app_id parameter refers to the application which is the owner of the badge.
To get the display attribute, call the badge_get_display() function:
badge_get_display(const char *app_id, unsigned int *is_display)
The function takes the following parameters:
- [in] app_id: The name of the application that gets the display flag of the badge.
- [out] is_display: 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 }
- Setting the display attribute:
You can display or hide the badge by setting its display attribute accordingly.
To set the display attribute, call the badge_set_display() function:
badge_set_display(const char *app_id, unsigned int is_display)
The function takes the following parameters:
- [in] app_id: The name of the application that gets the display flag of the badge.
- [in] is_display: The new value for 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 }
Removing the Badge
To remove the badge from the application, call the badge_remove() function:
badge_remove(const char *app_id)
The function takes the following parameters:
- [in] app_id: The ID of the application whose badge to remove.
ret = badge_remove(TEST_PKG); if (ret != BADGE_ERROR_NONE) { // Error handling }