Skip to content

Commit 036e2da

Browse files
committed
switch: Add sample usage for mobile push notifications
Enabling CONFIG_EXAMPLE_ENABLE_TEST_NOTIFICATIONS for the switch example will enable the test mobile notifications on push button events. Turning the switch on would report a parameter notification like {"Switch":{"Power":true}} and turning off would report an alert "Switch was turned off". Note that push notifications should be used only when users need to be specifically alerted about some event even when the app is in background, not otherwise.
1 parent afcc67f commit 036e2da

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

components/esp_rainmaker/src/core/esp_rmaker_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ esp_err_t esp_rmaker_report_node_state(void);
101101
_esp_rmaker_device_t *esp_rmaker_node_get_first_device(const esp_rmaker_node_t *node);
102102
esp_rmaker_attr_t *esp_rmaker_node_get_first_attribute(const esp_rmaker_node_t *node);
103103
esp_err_t esp_rmaker_params_mqtt_init(void);
104-
esp_err_t esp_rmaker_report_param_internal(void);
105104
esp_err_t esp_rmaker_param_get_stored_value(_esp_rmaker_param_t *param, esp_rmaker_param_val_t *val);
106105
esp_err_t esp_rmaker_param_store_value(_esp_rmaker_param_t *param);
107106
esp_err_t esp_rmaker_node_delete(const esp_rmaker_node_t *node);

examples/switch/main/Kconfig.projbuild

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ menu "Example Configuration"
88
GPIO number on which the "Boot" button is connected. This is generally used
99
by the application for custom operations like toggling states, resetting to defaults, etc.
1010

11+
config EXAMPLE_ENABLE_TEST_NOTIFICATIONS
12+
bool "Test Notifications"
13+
default n
14+
help
15+
Enable this option to test mobile push notifications. When enabled, turning on the switch using
16+
push button will trigger a parameter notification {"Switch":{"Power":true}} and turning off will
17+
trigger an alert "Switch was turned off".
18+
1119
endmenu

examples/switch/main/app_driver.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,27 @@ static void push_btn_cb(void *arg)
5151
{
5252
bool new_state = !g_power_state;
5353
app_driver_set_state(new_state);
54+
#ifdef CONFIG_EXAMPLE_ENABLE_TEST_NOTIFICATIONS
55+
/* This snippet has been added just to demonstrate how the APIs esp_rmaker_param_update_and_notify()
56+
* and esp_rmaker_raise_alert() can be used to trigger push notifications on the phone apps.
57+
* Normally, there should not be a need to use these APIs for such simple operations. Please check
58+
* API documentation for details.
59+
*/
60+
if (new_state) {
61+
esp_rmaker_param_update_and_notify(
62+
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
63+
esp_rmaker_bool(new_state));
64+
} else {
65+
esp_rmaker_param_update_and_report(
66+
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
67+
esp_rmaker_bool(new_state));
68+
esp_rmaker_raise_alert("Switch was turned off");
69+
}
70+
#else
5471
esp_rmaker_param_update_and_report(
5572
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
5673
esp_rmaker_bool(new_state));
74+
#endif
5775
}
5876

5977
static void set_power_state(bool target)

0 commit comments

Comments
 (0)