Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/platform/mac/objc/notifications/notifications.m
1#import "notifications.h"
2#import "../app.h"
3 
4#import <UserNotifications/UserNotifications.h>
5 
6void requestNotificationPermissionsWithCompletionHandler(
7 void (^completion_handler)(NSUInteger outcome_type, id outcome_msg)) {
8 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
9 
10 [center
11 requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound +
12 UNAuthorizationOptionBadge)
13 completionHandler:^(BOOL granted, NSError *_Nullable error) {
14 if (!granted) {
15 completion_handler(1, @"User denied request to receive notifications.");
16 } else if (error != nil) {
17 completion_handler(2, error.localizedDescription);
18 } else {
19 // Create and register the notification category.
20 UNNotificationCategory *CustomizedNotification = [UNNotificationCategory
21 categoryWithIdentifier:@"CUSTOMIZED_NOTIFICATION"
22 actions:@[]
23 intentIdentifiers:@[]
24 options:
25 UNNotificationCategoryOptionCustomDismissAction];
26 
27 [center
28 setNotificationCategories:[NSSet
29 setWithObjects:CustomizedNotification,
30 nil]];
31 completion_handler(0,
32 @"User accepted request to receive notifications.");
33 }
34 }];
35}
36 
37void requestNotificationPermissions(void *on_completion_callback) {
38 requestNotificationPermissionsWithCompletionHandler(^(NSUInteger outcome_type, id outcome_msg) {
39 dispatch_async(dispatch_get_main_queue(), ^{
40 warp_on_request_notification_permissions_completed(outcome_type, outcome_msg,
41 on_completion_callback);
42 });
43 });
44}
45 
46void sendNotificationWithErrorHandler(NSString *title, NSString *body, NSString *data,
47 void (^error_handler)(NSUInteger error_type, id error_msg),
48 BOOL playSound) {
49 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
50 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
51 if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
52 error_handler(0, @"User turned permissions off in system preferences.");
53 } else {
54 // Create the notification content.
55 // `autorelease` balances the +1 retain from `alloc`; the enclosing UserNotifications
56 // completion block runs on a GCD-dispatched queue that drains an ambient pool.
57 UNMutableNotificationContent *content =
58 [[[UNMutableNotificationContent alloc] init] autorelease];
59 content.title = [NSString localizedUserNotificationStringForKey:title arguments:nil];
60 content.body = [NSString localizedUserNotificationStringForKey:body arguments:nil];
61 
62 // Only play sound if the user setting allows it
63 if (playSound) {
64 content.sound = [UNNotificationSound defaultSound];
65 }
66 
67 content.userInfo = @{
68 @"DATA" : data,
69 };
70 
71 // Configure the trigger to send the notification after 1 second.
72 UNTimeIntervalNotificationTrigger *trigger =
73 [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
74 
75 // Create the request object.
76 UNNotificationRequest *request =
77 [UNNotificationRequest requestWithIdentifier:@"CUSTOMIZED_NOTIFICATION"
78 content:content
79 trigger:trigger];
80 
81 // Schedule the notification.
82 [center addNotificationRequest:request
83 withCompletionHandler:^(NSError *_Nullable err) {
84 if (err != nil) {
85 error_handler(1, err.localizedDescription);
86 }
87 }];
88 }
89 }];
90}
91 
92void sendNotification(id title, id body, id data, void *on_error_callback, BOOL playSound) {
93 sendNotificationWithErrorHandler(
94 title, body, data,
95 ^(NSUInteger error_type, id error_msg) {
96 dispatch_async(dispatch_get_main_queue(), ^{
97 warp_on_notification_send_error(error_type, error_msg, on_error_callback);
98 });
99 },
100 playSound);
101}
102