Skip to content

Commit 545994e

Browse files
committed
Refactored Azure Notification Hub modules for easily testing.
1 parent 5080783 commit 545994e

File tree

12 files changed

+447
-211
lines changed

12 files changed

+447
-211
lines changed

ios/RCTAzureNotificationHubManager.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
16D92203240700B100FB17C6 /* RCTAzureNotificationHub.m in Sources */ = {isa = PBXBuildFile; fileRef = 16D92202240700B100FB17C6 /* RCTAzureNotificationHub.m */; };
11+
4513A3AF24078CC300A0A996 /* RCTAzureNotificationHubUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 4513A3AE24078CC300A0A996 /* RCTAzureNotificationHubUtil.m */; };
12+
4519AACD240773E500A9CED7 /* RCTAzureNotificationHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 4519AACB240773E500A9CED7 /* RCTAzureNotificationHandler.m */; };
1113
458BD41D1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 458BD41C1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.h */; };
1214
458BD41F1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 458BD41E1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.m */; };
1315
/* End PBXBuildFile section */
@@ -28,6 +30,10 @@
2830
/* Begin PBXFileReference section */
2931
16D92201240700B100FB17C6 /* RCTAzureNotificationHub.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTAzureNotificationHub.h; sourceTree = "<group>"; };
3032
16D92202240700B100FB17C6 /* RCTAzureNotificationHub.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTAzureNotificationHub.m; sourceTree = "<group>"; };
33+
4513A3AD24078CC300A0A996 /* RCTAzureNotificationHubUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTAzureNotificationHubUtil.h; sourceTree = "<group>"; };
34+
4513A3AE24078CC300A0A996 /* RCTAzureNotificationHubUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTAzureNotificationHubUtil.m; sourceTree = "<group>"; };
35+
4519AACB240773E500A9CED7 /* RCTAzureNotificationHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTAzureNotificationHandler.m; sourceTree = "<group>"; };
36+
4519AACC240773E500A9CED7 /* RCTAzureNotificationHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTAzureNotificationHandler.h; sourceTree = "<group>"; };
3137
458BD4191D9EDE5E007E3F13 /* libRCTAzureNotificationHubManager.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTAzureNotificationHubManager.a; sourceTree = BUILT_PRODUCTS_DIR; };
3238
458BD41C1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTAzureNotificationHubManager.h; sourceTree = "<group>"; };
3339
458BD41E1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTAzureNotificationHubManager.m; sourceTree = "<group>"; };
@@ -63,6 +69,10 @@
6369
458BD41B1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager */ = {
6470
isa = PBXGroup;
6571
children = (
72+
4513A3AD24078CC300A0A996 /* RCTAzureNotificationHubUtil.h */,
73+
4513A3AE24078CC300A0A996 /* RCTAzureNotificationHubUtil.m */,
74+
4519AACC240773E500A9CED7 /* RCTAzureNotificationHandler.h */,
75+
4519AACB240773E500A9CED7 /* RCTAzureNotificationHandler.m */,
6676
16D92201240700B100FB17C6 /* RCTAzureNotificationHub.h */,
6777
16D92202240700B100FB17C6 /* RCTAzureNotificationHub.m */,
6878
458BD41C1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.h */,
@@ -128,6 +138,8 @@
128138
isa = PBXSourcesBuildPhase;
129139
buildActionMask = 2147483647;
130140
files = (
141+
4513A3AF24078CC300A0A996 /* RCTAzureNotificationHubUtil.m in Sources */,
142+
4519AACD240773E500A9CED7 /* RCTAzureNotificationHandler.m in Sources */,
131143
16D92203240700B100FB17C6 /* RCTAzureNotificationHub.m in Sources */,
132144
458BD41F1D9EDE5E007E3F13 /* RCTAzureNotificationHubManager.m in Sources */,
133145
);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#ifndef RCTAzureNotificationHandler_h
11+
#define RCTAzureNotificationHandler_h
12+
13+
#import "React/RCTEventEmitter.h"
14+
15+
@interface RCTAzureNotificationHandler : NSObject
16+
17+
// Class initializer
18+
- (nullable instancetype)initWithEventEmitter:(nonnull RCTEventEmitter *)eventEmitter;
19+
20+
// Handle local notifications
21+
- (void)localNotificationReceived:(nonnull NSNotification *)notification;
22+
23+
// Handle remote notifications
24+
- (void)remoteNotificationReceived:(nonnull NSNotification *)notification;
25+
26+
// Handle successful registration for remote notifications
27+
- (void)remoteNotificationRegistered:(nonnull NSNotification *)notification;
28+
29+
// Handle registration error for remote notifications
30+
- (void)remoteNotificationRegisteredError:(nonnull NSNotification *)notification;
31+
32+
// Handle successful registration for Azure Notification Hub
33+
- (void)azureNotificationHubRegistered:(nonnull NSNotification *)notification;
34+
35+
// Handle registration error for Azure Notification Hub
36+
- (void)azureNotificationHubRegisteredError:(nonnull NSNotification *)notification;
37+
38+
// Handle successful registration for UIUserNotificationSettings
39+
- (void)userNotificationSettingsRegistered:(nonnull NSNotification *)notification;
40+
41+
@end
42+
43+
#endif /* RCTAzureNotificationHandler_h */
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTAzureNotificationHub.h"
11+
#import "RCTAzureNotificationHandler.h"
12+
13+
@implementation RCTAzureNotificationHandler
14+
{
15+
@private
16+
RCTEventEmitter *_eventEmitter;
17+
}
18+
19+
// Class initializer
20+
- (nullable instancetype)initWithEventEmitter:(nonnull RCTEventEmitter *)eventEmitter
21+
{
22+
// Initialize superclass.
23+
self = [super init];
24+
25+
// Handle errors.
26+
if (!self)
27+
{
28+
return nil;
29+
}
30+
31+
_eventEmitter = eventEmitter;
32+
33+
return self;
34+
}
35+
36+
// Handle local notifications
37+
- (void)localNotificationReceived:(nonnull NSNotification *)notification
38+
{
39+
[_eventEmitter sendEventWithName:RCTLocalNotificationReceived
40+
body:notification.userInfo];
41+
}
42+
43+
// Handle remote notifications
44+
- (void)remoteNotificationReceived:(nonnull NSNotification *)notification
45+
{
46+
NSMutableDictionary *userInfo = [notification.userInfo mutableCopy];
47+
userInfo[RCTUserInfoRemote] = @YES;
48+
[_eventEmitter sendEventWithName:RCTRemoteNotificationReceived
49+
body:userInfo];
50+
}
51+
52+
// Handle successful registration for remote notifications
53+
- (void)remoteNotificationRegistered:(nonnull NSNotification *)notification
54+
{
55+
[_eventEmitter sendEventWithName:RCTRemoteNotificationRegistered
56+
body:notification.userInfo];
57+
}
58+
59+
// Handle registration error for remote notifications
60+
- (void)remoteNotificationRegisteredError:(nonnull NSNotification *)notification
61+
{
62+
NSError *error = notification.userInfo[RCTUserInfoError];
63+
NSDictionary *errorDetails = @{
64+
@"message": error.localizedDescription,
65+
@"code": @(error.code),
66+
@"details": error.userInfo,
67+
};
68+
[_eventEmitter sendEventWithName:RCTRemoteNotificationRegisteredError
69+
body:errorDetails];
70+
}
71+
72+
// Handle successful registration for Azure Notification Hub
73+
- (void)azureNotificationHubRegistered:(nonnull NSNotification *)notification
74+
{
75+
[_eventEmitter sendEventWithName:RCTAzureNotificationHubRegistered
76+
body:notification.userInfo];
77+
}
78+
79+
// Handle registration error for Azure Notification Hub
80+
- (void)azureNotificationHubRegisteredError:(nonnull NSNotification *)notification
81+
{
82+
NSError *error = notification.userInfo[RCTUserInfoError];
83+
NSDictionary *errorDetails = @{
84+
@"message": error.localizedDescription,
85+
@"code": @(error.code),
86+
@"details": error.userInfo,
87+
};
88+
89+
[_eventEmitter sendEventWithName:RCTAzureNotificationHubRegisteredError
90+
body:errorDetails];
91+
}
92+
93+
// Handle successful registration for UIUserNotificationSettings
94+
- (void)userNotificationSettingsRegistered:(nonnull NSNotification *)notification
95+
{
96+
RCTPromiseResolveBlock resolve = notification.userInfo[RCTUserInfoResolveBlock];
97+
if (resolve == nil)
98+
{
99+
return;
100+
}
101+
102+
UIUserNotificationSettings *notificationSettings = notification.userInfo[RCTUserInfoNotificationSettings];
103+
NSDictionary *notificationTypes = @{
104+
RCTNotificationTypeAlert: @((notificationSettings.types & UIUserNotificationTypeAlert) > 0),
105+
RCTNotificationTypeSound: @((notificationSettings.types & UIUserNotificationTypeSound) > 0),
106+
RCTNotificationTypeBadge: @((notificationSettings.types & UIUserNotificationTypeBadge) > 0),
107+
};
108+
109+
resolve(notificationTypes);
110+
}
111+
112+
@end
Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* Copyright (c) 2015-present, Facebook, Inc.
3-
* All rights reserved.
4-
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
99

1010
#ifndef RCTAzureNotificationHub_h
1111
#define RCTAzureNotificationHub_h
@@ -28,6 +28,8 @@ extern NSString *const RCTTagsKey;
2828
extern NSString *const RCTUserInfoNotificationSettings;
2929
extern NSString *const RCTUserInfoDeviceToken;
3030
extern NSString *const RCTUserInfoRemote;
31+
extern NSString *const RCTUserInfoResolveBlock;
32+
extern NSString *const RCTUserInfoRejectBlock;
3133
extern NSString *const RCTUserInfoSuccess;
3234
extern NSString *const RCTUserInfoError;
3335

@@ -44,26 +46,4 @@ extern NSString *const RCTErrorInvalidArguments;
4446
extern NSString *const RCTErrorMissingConnectionString;
4547
extern NSString *const RCTErrorMissingHubName;
4648

47-
// Format local notification
48-
static NSDictionary *RCTFormatLocalNotification(UILocalNotification *notification)
49-
{
50-
NSMutableDictionary *formattedLocalNotification = [NSMutableDictionary dictionary];
51-
if (notification.fireDate)
52-
{
53-
NSDateFormatter *formatter = [NSDateFormatter new];
54-
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
55-
NSString *fireDateString = [formatter stringFromDate:notification.fireDate];
56-
formattedLocalNotification[@"fireDate"] = fireDateString;
57-
}
58-
59-
formattedLocalNotification[@"alertAction"] = RCTNullIfNil(notification.alertAction);
60-
formattedLocalNotification[@"alertBody"] = RCTNullIfNil(notification.alertBody);
61-
formattedLocalNotification[@"applicationIconBadgeNumber"] = @(notification.applicationIconBadgeNumber);
62-
formattedLocalNotification[@"category"] = RCTNullIfNil(notification.category);
63-
formattedLocalNotification[@"soundName"] = RCTNullIfNil(notification.soundName);
64-
formattedLocalNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(notification.userInfo));
65-
formattedLocalNotification[@"remote"] = @NO;
66-
return formattedLocalNotification;
67-
}
68-
6949
#endif /* RCTAzureNotificationHub_h */

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHub.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* Copyright (c) 2015-present, Facebook, Inc.
3-
* All rights reserved.
4-
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
99

1010
// Notification Hub events
1111
NSString *const RCTLocalNotificationReceived = @"localNotificationReceived";
@@ -25,6 +25,8 @@
2525
NSString *const RCTUserInfoNotificationSettings = @"notificationSettings";
2626
NSString *const RCTUserInfoDeviceToken = @"deviceToken";
2727
NSString *const RCTUserInfoRemote = @"remote";
28+
NSString *const RCTUserInfoResolveBlock = @"resolveBlock";
29+
NSString *const RCTUserInfoRejectBlock = @"rejectBlock";
2830
NSString *const RCTUserInfoSuccess = @"success";
2931
NSString *const RCTUserInfoError = @"error";
3032

0 commit comments

Comments
 (0)