Search code examples
tizen

Hourly alert (chime) in Gear S3


How to make a hourly alert in a native watchface app for Gear S3? Any code samples?


Solution

  • You may try using Alarm API in this purpose. Follow the steps below:

    1. At first create a Watch Application and a Service Application. Package both of the apps and build. Go through this link to do the packaging.
    2. Now use the below code snippet in the watchface app to trigger the alert after a specific time which is 1hr in your case.

      bool init_alert(){
      int ret;
      int DELAY = 3;
      int REMIND = 3600; //alert after every 1hr.
      int alarm_id;
      
      app_control_h app_control = NULL;
      ret = app_control_create(&app_control);
      ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
      ret = app_control_set_app_id(app_control, "Service_App_ID");
      ret = alarm_schedule_after_delay(app_control, DELAY, REMIND, &alarm_id);
      
      return true;}
      

      Use below privileges in watchface app manifest file:

      http://tizen.org/privilege/alarm.get

      http://tizen.org/privilege/alarm.set

    3. Now in service_app_control() function of the service app, create an alert using the below code:

      void service_app_control(app_control_h app_control, void *data){
      
      dlog_print(DLOG_INFO, LOG_TAG, "Hourly Alert Here");
      notification = notification_create(NOTIFICATION_TYPE_NOTI);
      int ret =0;
      ret = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, "Hourly Alert!!",
                                  NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
      ret = notification_post(notification);
      notification_free(notification);
      return;}
      

    Use below privilege in service app manifest file:

    http://tizen.org/privilege/notification
    

    To know in details go through this link.

    Edit: 1 You may find the reason of this problem into this link. Please go through remarks and see also section. I've posted the answer for version 2.3.2 previously. That's why I wasn't getting any error. Please follow the way below. It works for version 3.0 in my case.

    Step 1: Take a counter, at first set it to 0 and increment it every time the clock gives 0 for seconds like this:

    watch_time_get_second(watch_time, &second);
    if(second == 0) count++;
    

    Step 2: Now, check the counter value if it equals to your required value e.g. if you want an alert after every 3 minutes interval then you may check whether it equals to 3 and after that create a notification like this:

    if(count == 3){
                dlog_print(DLOG_INFO, LOG_TAG, "Hourly Alert");
                notification = notification_create(NOTIFICATION_TYPE_NOTI);
    
                int ret =0;
                ret = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, "Hourly Alert!!",
                        NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
                ret = notification_post(notification);
                notification_free(notification);
                count = 0;
            }
    

    At the end, set the counter to 0, to get ready to count for the next interval. Find the full code structure below for a good understanding:

    #include <notification.h>
    static notification_h notification = NULL;
    int count = 0;
    
    
    static void
    update_watch(appdata_s *ad, watch_time_h watch_time, int ambient)
    {
        char watch_text[TEXT_BUF_SIZE];
        int hour24, minute, second;
    
        if (watch_time == NULL)
            return;
    
        watch_time_get_hour24(watch_time, &hour24);
        watch_time_get_minute(watch_time, &minute);
        watch_time_get_second(watch_time, &second);
    
        if (!ambient) {
            if(second == 0) count++;
    
            if(count == 3){
                dlog_print(DLOG_INFO, LOG_TAG, "Hourly Alert");
                notification = notification_create(NOTIFICATION_TYPE_NOTI);
    
                int ret =0;
                ret = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, "Hourly Alert!!",
                        NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
                ret = notification_post(notification);
                notification_free(notification);
                count = 0;
            }
            dlog_print(DLOG_INFO, LOG_TAG, "cnt = %d", count);
            snprintf(watch_text, TEXT_BUF_SIZE, "<align=center>Hello<br/>%02d:%02d:%02d</align>",
                    hour24, minute, second);
        } else {
            snprintf(watch_text, TEXT_BUF_SIZE, "<align=center>Hello Watch<br/>%02d:%02d</align>",
                    hour24, minute);
        }
    
        elm_object_text_set(ad->label, watch_text);
    }
    

    N.B: Don't forget to add privilege for notification.