Skip to content

Commit c67c682

Browse files
rlepinskiUlrico972
andauthored
Add notification options types (#397)
* Implementation * Types updates * Android impl * Fixes Co-authored-by: Ulrich Giberne <[email protected]>
1 parent 9f62c98 commit c67c682

File tree

8 files changed

+524
-233
lines changed

8 files changed

+524
-233
lines changed

urbanairship-react-native/android/src/main/java/com/urbanairship/reactnative/UrbanAirshipReactModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.urbanairship.messagecenter.Inbox;
5050
import com.urbanairship.messagecenter.Message;
5151
import com.urbanairship.messagecenter.MessageCenter;
52+
import com.urbanairship.push.PushManager;
5253
import com.urbanairship.push.PushMessage;
5354
import com.urbanairship.reactnative.events.NotificationOptInEvent;
5455
import com.urbanairship.reactnative.events.PushReceivedEvent;
@@ -310,6 +311,21 @@ public void isSystemNotificationsEnabledForApp(Promise promise) {
310311
promise.resolve(NotificationManagerCompat.from(getReactApplicationContext()).areNotificationsEnabled());
311312
}
312313

314+
/**
315+
* Gets the notification status.
316+
*
317+
* @param promise The JS promise.
318+
*/
319+
@ReactMethod
320+
public void getNotificationStatus(Promise promise) {
321+
WritableMap result = Arguments.createMap();
322+
PushManager push = UAirship.shared().getPushManager();
323+
result.putBoolean("airshipOptIn", push.isOptIn());
324+
result.putBoolean("airshipEnabled", push.getUserNotificationsEnabled());
325+
result.putBoolean("systemEnabled", NotificationManagerCompat.from(getReactApplicationContext()).areNotificationsEnabled());
326+
promise.resolve(result);
327+
}
328+
313329
@ReactMethod
314330
@RequiresApi(api = Build.VERSION_CODES.O)
315331
public void getNotificationChannelStatus(String channelId, Promise promise) {

urbanairship-react-native/ios/UARCTModule.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
6E42C7AF1EBA953100C85B3A /* UARCTAutopilot.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E42C7AC1EBA953100C85B3A /* UARCTAutopilot.m */; };
1212
6E42C7B01EBA953100C85B3A /* UrbanAirshipReactModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E42C7AE1EBA953100C85B3A /* UrbanAirshipReactModule.m */; };
1313
6E42C8551EC0DD0900C85B3A /* UARCTEventEmitter.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E42C8541EC0DD0900C85B3A /* UARCTEventEmitter.m */; };
14+
6E6C3FA427AB41C8007F55C7 /* UARCTUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E6C3FA327AB41C8007F55C7 /* UARCTUtils.m */; };
1415
6ED67C292318812700169F5C /* UARCTMessageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ED67C252318812700169F5C /* UARCTMessageView.m */; };
1516
6ED67C2A2318812700169F5C /* UARCTMessageViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ED67C282318812700169F5C /* UARCTMessageViewManager.m */; };
1617
6ED701FA1F6C91A400E67C37 /* UARCTDeepLinkAction.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ED701F91F6C91A400E67C37 /* UARCTDeepLinkAction.m */; };
@@ -39,6 +40,8 @@
3940
6E42C8531EC0DD0900C85B3A /* UARCTEventEmitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UARCTEventEmitter.h; path = UARCTModule/UARCTEventEmitter.h; sourceTree = "<group>"; };
4041
6E42C8541EC0DD0900C85B3A /* UARCTEventEmitter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UARCTEventEmitter.m; path = UARCTModule/UARCTEventEmitter.m; sourceTree = "<group>"; };
4142
6E4D17C21EBA7FAA0032DED3 /* libUARCTModule.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libUARCTModule.a; sourceTree = BUILT_PRODUCTS_DIR; };
43+
6E6C3FA227AB41C8007F55C7 /* UARCTUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UARCTUtils.h; path = UARCTModule/UARCTUtils.h; sourceTree = "<group>"; };
44+
6E6C3FA327AB41C8007F55C7 /* UARCTUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UARCTUtils.m; path = UARCTModule/UARCTUtils.m; sourceTree = "<group>"; };
4245
6ED67C252318812700169F5C /* UARCTMessageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UARCTMessageView.m; path = UARCTModule/UARCTMessageView.m; sourceTree = "<group>"; };
4346
6ED67C262318812700169F5C /* UARCTMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UARCTMessageView.h; path = UARCTModule/UARCTMessageView.h; sourceTree = "<group>"; };
4447
6ED67C272318812700169F5C /* UARCTMessageViewManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UARCTMessageViewManager.h; path = UARCTModule/UARCTMessageViewManager.h; sourceTree = "<group>"; };
@@ -63,6 +66,8 @@
6366
6E4D17341EBA7C100032DED3 /* UARCTModule */ = {
6467
isa = PBXGroup;
6568
children = (
69+
6E6C3FA227AB41C8007F55C7 /* UARCTUtils.h */,
70+
6E6C3FA327AB41C8007F55C7 /* UARCTUtils.m */,
6671
6ED67C262318812700169F5C /* UARCTMessageView.h */,
6772
6ED67C252318812700169F5C /* UARCTMessageView.m */,
6873
6ED67C272318812700169F5C /* UARCTMessageViewManager.h */,
@@ -167,6 +172,7 @@
167172
files = (
168173
457511BA23A41C7A00E4B1AD /* UARCTModuleVersion.m in Sources */,
169174
6E42C7AF1EBA953100C85B3A /* UARCTAutopilot.m in Sources */,
175+
6E6C3FA427AB41C8007F55C7 /* UARCTUtils.m in Sources */,
170176
6ED67C292318812700169F5C /* UARCTMessageView.m in Sources */,
171177
6E42C8551EC0DD0900C85B3A /* UARCTEventEmitter.m in Sources */,
172178
6E42C7B01EBA953100C85B3A /* UrbanAirshipReactModule.m in Sources */,

urbanairship-react-native/ios/UARCTModule/UARCTEventEmitter.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
@import AirshipKit;
1212
#endif
1313

14-
extern NSString *const UARCTNotificationPresentationAlertKey;
15-
extern NSString *const UARCTNotificationPresentationBadgeKey;
16-
extern NSString *const UARCTNotificationPresentationSoundKey;
17-
1814
/**
1915
* Listeners for Urban Airship events and emits them to the JS layer.
2016
*/

urbanairship-react-native/ios/UARCTModule/UARCTEventEmitter.m

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Copyright Urban Airship and Contributors */
22

33
#import "UARCTEventEmitter.h"
4+
#import "UARCTUtils.h"
45

56
@interface UARCTEventEmitter()
67
@property(nonatomic, strong) NSMutableArray *pendingEvents;
@@ -20,17 +21,6 @@ @interface UARCTEventEmitter()
2021
NSString *const UARCTOpenPreferenceCenterEventName = @"com.urbanairship.open_preference_center";
2122
NSString *const UAChannelUpdatedEventChannelKey = @"com.urbanairship.channel.identifier";
2223

23-
NSString *const UARCTNotificationPresentationAlertKey = @"alert";
24-
NSString *const UARCTNotificationPresentationBadgeKey = @"badge";
25-
NSString *const UARCTNotificationPresentationSoundKey = @"sound";
26-
27-
NSString *const UARCTAuthorizedNotificationSettingsAlertKey = UARCTNotificationPresentationAlertKey;
28-
NSString *const UARCTAuthorizedNotificationSettingsBadgeKey = UARCTNotificationPresentationBadgeKey;
29-
NSString *const UARCTAuthorizedNotificationSettingsSoundKey = UARCTNotificationPresentationSoundKey;
30-
NSString *const UARCTAuthorizedNotificationSettingsCarPlayKey = @"carPlay";
31-
NSString *const UARCTAuthorizedNotificationSettingsLockScreenKey = @"lockScreen";
32-
NSString *const UARCTAuthorizedNotificationSettingsNotificationCenterKey = @"notificationCenter";
33-
3424
NSString *const UARCTEventNameKey = @"name";
3525
NSString *const UARCTEventBodyKey = @"body";
3626

@@ -154,61 +144,22 @@ - (void)channelRegistrationSucceeded:(NSNotification *)notification {
154144
#pragma mark UARegistrationDelegate
155145

156146
- (void)notificationAuthorizedSettingsDidChange:(UAAuthorizedNotificationSettings)authorizedSettings {
157-
BOOL optedIn = NO;
158-
159-
BOOL alertBool = NO;
160-
BOOL badgeBool = NO;
161-
BOOL soundBool = NO;
162-
BOOL carPlayBool = NO;
163-
BOOL lockScreenBool = NO;
164-
BOOL notificationCenterBool = NO;
165-
166-
if (authorizedSettings & UAAuthorizedNotificationSettingsAlert) {
167-
alertBool = YES;
168-
}
169-
170-
if (authorizedSettings & UAAuthorizedNotificationSettingsBadge) {
171-
badgeBool = YES;
172-
}
173-
174-
if (authorizedSettings & UAAuthorizedNotificationSettingsSound) {
175-
soundBool = YES;
176-
}
177-
178-
if (authorizedSettings & UAAuthorizedNotificationSettingsCarPlay) {
179-
carPlayBool = YES;
180-
}
181-
182-
if (authorizedSettings & UAAuthorizedNotificationSettingsLockScreen) {
183-
lockScreenBool = YES;
184-
}
185-
186-
if (authorizedSettings & UAAuthorizedNotificationSettingsNotificationCenter) {
187-
notificationCenterBool = YES;
188-
}
189-
190-
optedIn = authorizedSettings != UAAuthorizedNotificationSettingsNone;
191-
192-
NSDictionary *body = @{ @"optIn": @(optedIn),
193-
@"authorizedNotificationSettings" : @{
194-
UARCTAuthorizedNotificationSettingsAlertKey : @(alertBool),
195-
UARCTAuthorizedNotificationSettingsBadgeKey : @(badgeBool),
196-
UARCTAuthorizedNotificationSettingsSoundKey : @(soundBool),
197-
UARCTAuthorizedNotificationSettingsCarPlayKey : @(carPlayBool),
198-
UARCTAuthorizedNotificationSettingsLockScreenKey : @(lockScreenBool),
199-
UARCTAuthorizedNotificationSettingsNotificationCenterKey : @(notificationCenterBool)
200-
}};
201-
147+
NSDictionary *body = @{
148+
@"optIn": @(authorizedSettings != UAAuthorizedNotificationSettingsNone),
149+
@"authorizedNotificationSettings": [UARCTUtils authorizedSettingsDictionary:authorizedSettings],
150+
@"authorizedSettings": [UARCTUtils authorizedSettingsArray:authorizedSettings]
151+
};
202152
[self sendEventWithName:UARCTOptInStatusChangedEventName body:body];
203153
}
204154

205155
#pragma mark -
206156
#pragma mark Message Center
207157

208158
- (void)inboxUpdated {
209-
NSDictionary *body = @{ @"messageUnreadCount": @([UAMessageCenter shared].messageList.unreadCount),
210-
@"messageCount": @([UAMessageCenter shared].messageList.messageCount)
211-
};
159+
NSDictionary *body = @{
160+
@"messageUnreadCount": @([UAMessageCenter shared].messageList.unreadCount),
161+
@"messageCount": @([UAMessageCenter shared].messageList.messageCount)
162+
};
212163

213164
[self sendEventWithName:UARCTInboxUpdatedEventName body:body];
214165
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#if __has_include("AirshipLib.h")
2+
#import "AirshipLib.h"
3+
#else
4+
@import AirshipKit;
5+
#endif
6+
7+
NS_ASSUME_NONNULL_BEGIN
8+
9+
@interface UARCTUtils : NSObject
10+
11+
+ (UANotificationOptions)optionsFromOptionsArray:(NSArray *)options;
12+
+ (NSArray<NSString *> *)authorizedSettingsArray:(UAAuthorizedNotificationSettings)settings;
13+
+ (NSDictionary *)authorizedSettingsDictionary:(UAAuthorizedNotificationSettings)settings;
14+
+ (NSString *)authorizedStatusString:(UAAuthorizationStatus)status;
15+
@end
16+
17+
NS_ASSUME_NONNULL_END
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#import "UARCTUtils.h"
2+
3+
@implementation UARCTUtils
4+
5+
+ (NSString *)authorizedStatusString:(UAAuthorizationStatus)status {
6+
switch (status) {
7+
case UAAuthorizationStatusDenied:
8+
return @"denied";
9+
case UAAuthorizationStatusEphemeral:
10+
return @"ephemeral";
11+
case UAAuthorizationStatusAuthorized:
12+
return @"authorized";
13+
case UAAuthorizationStatusProvisional:
14+
return @"provisional";
15+
case UAAuthorizationStatusNotDetermined:
16+
default:
17+
return @"notDetermined";
18+
}
19+
}
20+
21+
+ (UANotificationOptions)optionsFromOptionsArray:(NSArray *)options {
22+
UANotificationOptions notificationOptions = UANotificationOptionNone;
23+
24+
if ([options containsObject:@"alert"]) {
25+
notificationOptions = notificationOptions | UANotificationOptionAlert;
26+
}
27+
28+
if ([options containsObject:@"badge"]) {
29+
notificationOptions = notificationOptions | UANotificationOptionBadge;
30+
}
31+
32+
if ([options containsObject:@"sound"]) {
33+
notificationOptions = notificationOptions | UANotificationOptionSound;
34+
}
35+
36+
if ([options containsObject:@"carPlay"]) {
37+
notificationOptions = notificationOptions | UANotificationOptionCarPlay;
38+
}
39+
40+
if ([options containsObject:@"criticalAlert"]) {
41+
notificationOptions = notificationOptions | UANotificationOptionCriticalAlert;
42+
}
43+
44+
if ([options containsObject:@"providesAppNotificationSettings"]) {
45+
notificationOptions = notificationOptions | UANotificationOptionProvidesAppNotificationSettings;
46+
}
47+
48+
if ([options containsObject:@"provisional"]) {
49+
notificationOptions = notificationOptions | UANotificationOptionProvisional;
50+
}
51+
52+
return notificationOptions;
53+
}
54+
55+
+ (NSArray<NSString *> *)authorizedSettingsArray:(UAAuthorizedNotificationSettings)settings {
56+
NSMutableArray *settingsArray = [NSMutableArray array];
57+
if (settings & UAAuthorizedNotificationSettingsAlert) {
58+
[settingsArray addObject:@"alert"];
59+
}
60+
if (settings & UAAuthorizedNotificationSettingsBadge) {
61+
[settingsArray addObject:@"badge"];
62+
}
63+
if (settings & UAAuthorizedNotificationSettingsSound) {
64+
[settingsArray addObject:@"sound"];
65+
}
66+
if (settings & UAAuthorizedNotificationSettingsCarPlay) {
67+
[settingsArray addObject:@"carPlay"];
68+
}
69+
if (settings & UAAuthorizedNotificationSettingsLockScreen) {
70+
[settingsArray addObject:@"lockScreen"];
71+
}
72+
if (settings & UAAuthorizedNotificationSettingsNotificationCenter) {
73+
[settingsArray addObject:@"notificationCenter"];
74+
}
75+
if (settings & UAAuthorizedNotificationSettingsAnnouncement) {
76+
[settingsArray addObject:@"announcement"];
77+
}
78+
if (settings & UAAuthorizedNotificationSettingsScheduledDelivery) {
79+
[settingsArray addObject:@"scheduledDelivery"];
80+
}
81+
if (settings & UAAuthorizedNotificationSettingsTimeSensitive) {
82+
[settingsArray addObject:@"timeSensitive"];
83+
}
84+
85+
return settingsArray;
86+
}
87+
88+
+ (NSDictionary *)authorizedSettingsDictionary:(UAAuthorizedNotificationSettings)settings {
89+
return @{
90+
@"alert" : @(settings & UAAuthorizedNotificationSettingsAlert),
91+
@"badge" : @(settings & UAAuthorizedNotificationSettingsBadge),
92+
@"sound" : @(settings & UAAuthorizedNotificationSettingsSound),
93+
@"carPlay" : @(settings & UAAuthorizedNotificationSettingsCarPlay),
94+
@"lockScreen" : @(settings & UAAuthorizedNotificationSettingsLockScreen),
95+
@"notificationCenter" : @(settings & UAAuthorizedNotificationSettingsNotificationCenter),
96+
@"criticalAlert" : @(settings & UAAuthorizedNotificationSettingsNotificationCenter),
97+
@"announcement" : @(settings & UAAuthorizedNotificationSettingsNotificationCenter),
98+
@"scheduledDelivery" : @(settings & UAAuthorizedNotificationSettingsNotificationCenter),
99+
@"timeSensitive" : @(settings & UAAuthorizedNotificationSettingsNotificationCenter),
100+
};
101+
}
102+
103+
@end

urbanairship-react-native/ios/UARCTModule/UrbanAirshipReactModule.m

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#import "UARCTDeepLinkAction.h"
66
#import "UARCTAutopilot.h"
77
#import "UARCTMessageCenter.h"
8+
#import "UARCTUtils.h"
89

910
#if __has_include("AirshipLib.h")
1011
#import "UAInAppMessageHTMLAdapter.h"
@@ -424,34 +425,56 @@ + (BOOL)requiresMainQueueSetup {
424425

425426
}
426427

427-
RCT_EXPORT_METHOD(setForegroundPresentationOptions:(NSDictionary *)options) {
428+
RCT_EXPORT_METHOD(setNotificationOptions:(NSArray *)options) {
429+
UANotificationOptions notificationOptions = [UARCTUtils optionsFromOptionsArray:options];
430+
UA_LDEBUG(@"Notification options set: %lu from dictionary: %@", (unsigned long)notificationOptions, options);
431+
UAirship.push.notificationOptions = notificationOptions;
432+
[UAirship.push updateRegistration];
433+
}
434+
435+
RCT_EXPORT_METHOD(setForegroundPresentationOptions:(NSArray *)options) {
428436
UNNotificationPresentationOptions presentationOptions = UNNotificationPresentationOptionNone;
429437

430-
if (options[UARCTNotificationPresentationAlertKey] != nil) {
431-
if ([options[UARCTNotificationPresentationAlertKey] boolValue]) {
432-
presentationOptions = presentationOptions | UNNotificationPresentationOptionAlert;
433-
}
438+
if ([options containsObject:@"alert"]) {
439+
presentationOptions = presentationOptions | UNNotificationPresentationOptionAlert;
434440
}
435441

436-
if (options[UARCTNotificationPresentationBadgeKey] != nil) {
437-
if ([options[UARCTNotificationPresentationBadgeKey] boolValue]) {
438-
presentationOptions = presentationOptions | UNNotificationPresentationOptionBadge;
439-
}
442+
if ([options containsObject:@"badge"]) {
443+
presentationOptions = presentationOptions | UNNotificationPresentationOptionBadge;
440444
}
441445

442-
if (options[UARCTNotificationPresentationSoundKey] != nil) {
443-
if ([options[UARCTNotificationPresentationSoundKey] boolValue]) {
444-
presentationOptions = presentationOptions | UNNotificationPresentationOptionSound;
445-
}
446+
if ([options containsObject:@"sound"]) {
447+
presentationOptions = presentationOptions | UNNotificationPresentationOptionSound;
446448
}
447-
449+
448450
UA_LDEBUG(@"Foreground presentation options set: %lu from dictionary: %@", (unsigned long)presentationOptions, options);
449451

450452
[UAirship push].defaultPresentationOptions = presentationOptions;
451453
[[NSUserDefaults standardUserDefaults] setInteger:presentationOptions
452454
forKey:UARCTPresentationOptionsStorageKey];
453455
}
454456

457+
458+
RCT_REMAP_METHOD(getNotificationStatus,
459+
getNotificationStatus_resolver:(RCTPromiseResolveBlock)resolve
460+
rejecter:(RCTPromiseRejectBlock)reject) {
461+
462+
UAPush *push = UAirship.push;
463+
BOOL isSystemEnabled = push.authorizedNotificationSettings != 0;
464+
id result = @{
465+
@"airshipOptIn": @(push.isPushNotificationsOptedIn),
466+
@"airshipEnabled": @(push.userPushNotificationsEnabled),
467+
@"systemEnabled": @(isSystemEnabled),
468+
@"ios": @{
469+
@"authorizedSettings": [UARCTUtils authorizedSettingsArray:push.authorizedNotificationSettings],
470+
@"authorizedStatus": [UARCTUtils authorizedStatusString:push.authorizationStatus]
471+
}
472+
};
473+
474+
resolve(result);
475+
}
476+
477+
455478
RCT_EXPORT_METHOD(setAutobadgeEnabled:(BOOL)enabled) {
456479
[UAirship push].autobadgeEnabled = enabled;
457480
}

0 commit comments

Comments
 (0)