capacitor_bindings 0.14.0

Capacitor bindings to help you build android and ios apps with rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use crate::extern_functions::*;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use typed_builder::TypedBuilder;

use crate::helpers::*;
use crate::{error::Error, plugin_listener_handle::PluginListenerHandle};

pub struct LocalNotifications;

impl LocalNotifications {
    /// Check if notifications are enabled or not.
    pub async fn are_enabled() -> Result<EnabledResult, Error> {
        run_unit_value(local_notifications_are_enabled).await
    }

    /// Check permission to display local notifications.
    pub async fn check_permissions() -> Result<PermissionStatus, Error> {
        run_unit_value(local_notifications_check_permissions).await
    }

    /// Request permission to display local notifications.
    pub async fn request_permissions() -> Result<PermissionStatus, Error> {
        run_unit_value(local_notifications_request_permissions).await
    }

    /// Get a list of notifications that are visible on the notifications screen.
    pub async fn get_delivered_notifications() -> Result<DeliveredNotifications, Error> {
        run_unit_value(local_notifications_get_delivered_notifications).await
    }

    /// Remove the specified notifications from the notifications screen.
    pub async fn remove_delivered_notifications(
        delivered: impl Into<DeliveredNotifications>,
    ) -> Result<(), Error> {
        run_value_unit(
            delivered,
            local_notifications_remove_delivered_notifications,
        )
        .await
    }

    /// Cancel pending notifications.
    pub async fn cancel(options: impl Into<CancelOptions>) -> Result<(), Error> {
        run_value_unit(options, local_notifications_cancel).await
    }

    /// Remove all the notifications from the notifications screen.
    pub async fn remove_all_delivered_notifications() -> Result<(), Error> {
        run_unit_unit(local_notifications_remove_all_delivered_notifications).await
    }

    /// Schedule one or more local notifications.
    pub async fn schedule(options: impl Into<ScheduleOptions>) -> Result<ScheduleResult, Error> {
        run_value_value(options, local_notifications_schedule).await
    }

    /// Register actions to take when notifications are displayed.
    /// Only available for iOS and Android.
    #[cfg(any(feature = "ios", feature = "android"))]
    pub async fn register_action_types(
        options: impl Into<RegisterActionTypesOptions>,
    ) -> Result<(), Error> {
        run_value_unit(options, local_notifications_register_action_types).await
    }

    pub async fn add_received_listener<F: Fn(LocalNotificationSchema) + 'static>(
        func: F,
    ) -> Result<PluginListenerHandle, Error> {
        listen_async(
            func,
            "localNotificationReceived",
            local_notifications_add_listener,
        )
        .await
    }

    pub async fn add_action_performed_listener<F: Fn(ActionPerformed) + 'static>(
        func: F,
    ) -> Result<PluginListenerHandle, Error> {
        listen_async(
            func,
            "localNotificationActionPerformed",
            local_notifications_add_listener,
        )
        .await
    }
}

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct ScheduleOptions {
    pub notifications: Vec<LocalNotificationSchema>,
}

impl From<LocalNotificationSchema> for ScheduleOptions {
    fn from(val: LocalNotificationSchema) -> Self {
        ScheduleOptions {
            notifications: vec![val],
        }
    }
}

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct RegisterActionTypesOptions {
    pub types: Vec<ActionType>,
}

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct Action {
    /// The action identifier. Referenced in the 'actionPerformed' event as actionId.
    pub id: String,

    /// The title text to display for this action.
    pub title: String,
}

#[skip_serializing_none]
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct EnabledResult {
    /// Whether or not the device has local notifications enabled.
    pub value: bool,
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PermissionStatus {
    /// Whether or not the device has local notifications enabled.
    pub display: PermissionState,
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase", default)]
pub struct ActionPerformed {
    /// The identifier of the performed action. This might be "tap" if the user tapped the notification.
    pub action_id: String,
    /// The value entered by the user on the notification. Only available on iOS for notifications with input set to true.
    pub input_value: Option<String>,
    /// The original notification schema.
    pub notification: LocalNotificationSchema,
}

impl ActionPerformed {
    pub fn is_tap(&self) -> bool {
        self.action_id == "tap"
    }
}

/// A collection of actions.
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct ActionType {
    /// The ID of the action type. Referenced in notifications by the actionTypeId key.
    pub id: String,

    /// The list of actions associated with this action type.
    pub actions: Vec<Action>,
}

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct ScheduleResult {
    /// The list of scheduled notifications.
    pub notifications: Vec<LocalNotificationDescriptor>,
}

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct CancelOptions {
    /// The list of notifications to cancel.
    pub notifications: Vec<LocalNotificationDescriptor>,
}

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct LocalNotificationDescriptor {
    pub id: i32,
}

impl Default for LocalNotificationSchema {
    fn default() -> Self {
        Self {
            title: Default::default(),
            body: Default::default(),
            schedule: Schedule::On {
                on: ScheduleOn::default(),
                allow_while_idle: false,
            },
            large_body: Default::default(),
            summary_text: Default::default(),
            id: Default::default(),
            ongoing: Default::default(),
            auto_cancel: Default::default(),
            inbox_list: Default::default(),
            small_icon: Default::default(),
            large_icon: Default::default(),
            icon_color: Default::default(),
            action_type_id: Default::default(),
            group: Default::default(),
            group_summary: Default::default(),
            sound: Default::default(),
            thread_identifier: Default::default(),
            summary_argument: Default::default(),
            channel_id: Default::default(),
        }
    }
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TypedBuilder)]
#[serde(rename_all = "camelCase", default)]
pub struct LocalNotificationSchema {
    #[builder(setter(into))]
    /// The title of the notification.
    pub title: String,
    #[builder(setter(into))]
    /// The body of the notification, shown below the title.
    pub body: String,
    #[builder(setter(into))]
    /// Schedule this notification for a later time.
    pub schedule: Schedule,
    #[builder(setter(into, strip_option), default)]
    /// Sets a multiline text block for display in a big text notification style.
    pub large_body: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Used to set the summary text detail in inbox and big text notification styles. Only available for Android.
    pub summary_text: Option<String>,
    /// The notification identifier. On Android it's a 32-bit int. So the value should be between -2147483648 and 2147483647 inclusive.
    pub id: i32,
    #[builder(default)]
    /// If true, the notification can't be swiped away. Calls setOngoing() on NotificationCompat.Builder with the provided value. Only available for Android.
    pub ongoing: bool,
    /// If true, the notification is canceled when the user clicks on it. Calls setAutoCancel() on NotificationCompat.Builder with the provided value. Only available for Android.
    pub auto_cancel: bool,
    #[builder(setter(into, strip_option), default)]
    /// Sets a list of strings for display in an inbox style notification. Up to 5 strings are allowed. Only available for Android.
    pub inbox_list: Option<Vec<String>>,
    #[builder(setter(into, strip_option), default)]
    /// Set a custom status bar icon. If set, this overrides the smallIcon option from Capacitor configuration. Icons should be placed in your app's res/drawable folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android.
    pub small_icon: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Set a large icon for notifications. Icons should be placed in your app's res/drawable folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android.
    pub large_icon: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Set the color of the notification icon. Only available for Android.
    pub icon_color: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Associate an action type with this notification.
    pub action_type_id: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Used to group multiple notifications. Calls setGroup() on NotificationCompat.Builder with the provided value. Only available for Android.
    pub group: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// If true, this notification becomes the summary for a group of notifications. Calls setGroupSummary() on NotificationCompat.Builder with the provided value. Only available for Android when using group.
    pub group_summary: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Name of the audio file to play when this notification is displayed. Include the file extension with the filename. On iOS, the file should be in the app bundle. On Android, the file should be in res/raw folder. Recommended format is .wav because is supported by both iOS and Android. Only available for iOS and Android < 26. For Android 26+ use channelId of a channel configured with the desired sound. If the sound file is not found, (i.e. empty string or wrong name) the default system notification sound will be used. If not provided, it will produce the default sound on Android and no sound on iOS.
    pub sound: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Used to group multiple notifications. Sets threadIdentifier on the UNMutableNotificationContent. Only available for iOS.
    pub thread_identifier: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// The string this notification adds to the category's summary format string. Sets summaryArgument on the UNMutableNotificationContent. Only available for iOS.
    pub summary_argument: Option<String>,
    #[builder(setter(into, strip_option), default)]
    /// Specifies the channel the notification should be delivered on. If channel with the given name does not exist then the notification will not fire. If not provided, it will use the default channel. Calls setChannelId() on NotificationCompat.Builder with the provided value. Only available for Android 26+.
    pub channel_id: Option<String>,
}


#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum Schedule {
    On {
        /// Schedule a notification on particular interval(s). This is similar to scheduling cron jobs. Only available for iOS and Android.
        on: ScheduleOn,
        #[serde(rename = "allowWhileIdle")]
        /// Allow this notification to fire while in Doze Only available for Android 23+. Note that these notifications can only fire once per 9 minutes, per app.
        allow_while_idle: bool,
    },
    At {
        #[serde(
            skip_deserializing,
            default = "js_sys::Date::new_0",
            serialize_with = "serde_wasm_bindgen::preserve::serialize"
        )]


        /// WARNING: This will not be deserialized correctly
        ///
        /// Schedule a notification at a specific date and time.
        /// ```ignore
        /// let time: chrono::DateTime<chrono::Utc> = chrono::Utc::now() + Duration::seconds(5); //notify 5 seconds from now
        /// let time_string = time.to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
        /// let at_millis = JsValue::from_f64(js_sys::Date::parse(&time_string));
        /// let at = js_sys::Date::new(&at_millis);
        /// ```
        at: js_sys::Date,

        /// Repeat delivery of this notification at the date and time specified by at. Only available for iOS and Android.
        repeats: bool,
        /// Allow this notification to fire while in Doze Only available for Android 23+. Note that these notifications can only fire once per 9 minutes, per app.
        allow_while_idle: bool,
    },
    Every {
        /// Schedule a notification on a particular interval.
        every: ScheduleEvery,
        /// Limit the number times a notification is delivered by the interval specified by every.
        count: usize,
        #[serde(rename = "allowWhileIdle")]
        /// Allow this notification to fire while in Doze Only available for Android 23+. Note that these notifications can only fire once per 9 minutes, per app.
        allow_while_idle: bool,
    },
}

impl From<ScheduleOn> for Schedule {
    fn from(val: ScheduleOn) -> Self {
        Schedule::On {
            on: val,
            allow_while_idle: true,
        }
    }
}

impl From<(ScheduleEvery, usize)> for Schedule {
    fn from(val: (ScheduleEvery, usize)) -> Self {
        Schedule::Every {
            every: val.0,
            count: val.1,
            allow_while_idle: true,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ScheduleEvery {
    Year,
    Month,
    TwoWeeks,
    Week,
    Day,
    Hour,
    Minute,
    Second,
}

#[skip_serializing_none]
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, TypedBuilder)]
#[serde(rename_all = "camelCase", default)]
pub struct ScheduleOn {
    #[builder(setter(strip_option), default)]
    pub year: Option<u32>,
    #[builder(setter(strip_option), default)]
    pub month: Option<u32>,
    #[builder(setter(strip_option), default)]
    pub day: Option<u32>,
    #[builder(setter(strip_option), default)]
    pub weekday: Option<Weekday>,
    #[builder(setter(strip_option), default)]
    pub hour: Option<u32>,
    #[builder(setter(strip_option), default)]
    pub minute: Option<u32>,
    #[builder(setter(strip_option), default)]
    pub second: Option<u32>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[repr(u8)]
pub enum PermissionState {
    Prompt,
    PromptWithRationale,
    Granted,
    Denied,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[repr(u8)]
pub enum Weekday {
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7,
}

#[skip_serializing_none]
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct DeliveredNotifications {
    /// List of notifications that are visible on the notifications screen.
    pub notifications: Vec<DeliveredNotificationSchema>,
}

#[skip_serializing_none]
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct DeliveredNotificationSchema {
    /// The notification identifier.
    pub id: i32,
    /// The notification tag. Only available on Android.
    pub tag: Option<String>,
    /// The title of the notification.
    pub title: String,
    /// The body of the notification, shown below the title.
    pub body: String,
    /// The configured group of the notification. Only available for Android.
    pub group: Option<String>,
    /// If this notification is the summary for a group of notifications. Only available for Android.
    pub group_summary: Option<bool>,
    //pub data: Any,
    //pub extra: Any,
    //TODO additional fields
}

#[cfg(test)]
mod tests {

    use super::*;
    use serde_test::assert_tokens;

    fn get_options() -> ScheduleOptions {
        LocalNotificationSchema::builder()
            .title("Notification Title")
            .body("Notification Body")
            .auto_cancel(true)
            .schedule(ScheduleOn::builder().second(0).build())
            .id(123)
            .large_body("Notification Large Body")
            .summary_text("Notification Summary Text")
            .inbox_list(vec![
                "N One".into(),
                "N Two".into(),
                "N Three".into(),
                "N Four".into(),
                "N Five".into(),
            ])
            .build()
            .into()
    }

    #[test]
    fn test_ser_json() {
        let options = get_options();

        let str: String = serde_json::to_string(&options).unwrap();

        let expected = "{\"notifications\":[{\"title\":\"Notification Title\",\"body\":\"Notification Body\",\"schedule\":{\"on\":{\"second\":0},\"allowWhileIdle\":true},\"largeBody\":\"Notification Large Body\",\"summaryText\":\"Notification Summary Text\",\"id\":123,\"ongoing\":false,\"autoCancel\":true,\"inboxList\":[\"N One\",\"N Two\",\"N Three\",\"N Four\",\"N Five\"]}]}";

        assert_eq!(str.trim(), expected.trim())
    }

    #[test]
    fn test_ser_de() {
        let options = get_options();

        {
            use serde_test::Token::*;

            assert_tokens(
                &options,
                &[
                    Struct {
                        name: "ScheduleOptions",
                        len: 1,
                    },
                    Str("notifications"),
                    Seq {
                        len: Option::Some(1),
                    },
                    Struct {
                        name: "LocalNotificationSchema",
                        len: 9,
                    },
                    Str("title"),
                    Str("Notification Title"),
                    Str("body"),
                    Str("Notification Body"),
                    Str("schedule"),
                    Struct {
                        name: "Schedule",
                        len: 2,
                    },
                    Str("on"),
                    Struct {
                        name: "ScheduleOn",
                        len: 1,
                    },
                    Str("second"),
                    Some,
                    U32(0).to_owned(),
                    StructEnd,
                    Str("allowWhileIdle"),
                    Bool(true),
                    StructEnd,
                    Str("largeBody"),
                    Some,
                    Str("Notification Large Body"),
                    Str("summaryText"),
                    Some,
                    Str("Notification Summary Text"),
                    Str("id"),
                    I32(123),
                    Str("ongoing"),
                    Bool(false),
                    Str("autoCancel"),
                    Bool(true),
                    Str("inboxList"),
                    Some,
                    Seq {
                        len: Option::Some(5),
                    },
                    Str("N One"),
                    Str("N Two"),
                    Str("N Three"),
                    Str("N Four"),
                    Str("N Five"),
                    SeqEnd,
                    StructEnd,
                    SeqEnd,
                    StructEnd,
                ],
            )
        }
    }
}