@amrshbib/react-native-pushbib 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +475 -0
- package/android/build.gradle +51 -0
- package/android/src/main/AndroidManifest.xml +15 -0
- package/android/src/main/java/com/amrshbib/pushbib/FirebaseBootstrap.kt +37 -0
- package/android/src/main/java/com/amrshbib/pushbib/LiveUpdatePayload.kt +102 -0
- package/android/src/main/java/com/amrshbib/pushbib/LiveUpdateRenderer.kt +294 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationPayload.kt +130 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationRenderer.kt +181 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibChannels.kt +281 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibEmitter.kt +133 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibMessagingService.kt +87 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibModule.kt +286 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibPackage.kt +14 -0
- package/index.d.ts +224 -0
- package/ios/PushbibCenterDelegate.h +16 -0
- package/ios/PushbibCenterDelegate.m +138 -0
- package/ios/PushbibEventBus.h +34 -0
- package/ios/PushbibEventBus.m +88 -0
- package/ios/PushbibLiveActivities.h +18 -0
- package/ios/PushbibLiveActivities.m +46 -0
- package/ios/PushbibModule.h +5 -0
- package/ios/PushbibModule.m +260 -0
- package/ios/PushbibSwizzler.m +135 -0
- package/package.json +40 -0
- package/react-native-pushbib.podspec +19 -0
- package/src/EventManager.js +55 -0
- package/src/Logger.js +18 -0
- package/src/NativePushbib.js +36 -0
- package/src/api.js +108 -0
- package/src/index.js +753 -0
- package/src/version.js +2 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
4
|
+
|
|
5
|
+
typedef void (^PushbibEventHandler)(NSString *name, NSDictionary *body);
|
|
6
|
+
|
|
7
|
+
extern NSString *const PushbibEventPushToken;
|
|
8
|
+
extern NSString *const PushbibEventClick;
|
|
9
|
+
extern NSString *const PushbibEventReceived;
|
|
10
|
+
extern NSString *const PushbibEventForegroundWillDisplay;
|
|
11
|
+
extern NSString *const PushbibEventLiveActivityToken;
|
|
12
|
+
|
|
13
|
+
extern NSString *const PushbibDeviceTokenNotification;
|
|
14
|
+
extern NSString *const PushbibDeviceTokenErrorNotification;
|
|
15
|
+
|
|
16
|
+
@interface PushbibEventBus : NSObject
|
|
17
|
+
|
|
18
|
+
+ (instancetype)shared;
|
|
19
|
+
|
|
20
|
+
@property (atomic, copy, nullable) NSString *deviceTokenHex;
|
|
21
|
+
@property (atomic, copy, nullable) NSString *deviceTokenErrorMessage;
|
|
22
|
+
@property (atomic, assign) BOOL hasForegroundHandler;
|
|
23
|
+
|
|
24
|
+
- (void)postEvent:(NSString *)name body:(NSDictionary *)body;
|
|
25
|
+
- (void)setHandler:(nullable PushbibEventHandler)handler;
|
|
26
|
+
|
|
27
|
+
- (void)storeDisplayDecision:(NSString *)completionId
|
|
28
|
+
block:(void (^)(BOOL display))block
|
|
29
|
+
timeout:(NSTimeInterval)timeout;
|
|
30
|
+
- (void)completeDisplay:(NSString *)completionId display:(BOOL)display;
|
|
31
|
+
|
|
32
|
+
@end
|
|
33
|
+
|
|
34
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#import "PushbibEventBus.h"
|
|
2
|
+
|
|
3
|
+
NSString *const PushbibEventPushToken = @"Pushbib#pushToken";
|
|
4
|
+
NSString *const PushbibEventClick = @"Pushbib#click";
|
|
5
|
+
NSString *const PushbibEventReceived = @"Pushbib#received";
|
|
6
|
+
NSString *const PushbibEventForegroundWillDisplay = @"Pushbib#foregroundWillDisplay";
|
|
7
|
+
NSString *const PushbibEventLiveActivityToken = @"Pushbib#liveActivityToken";
|
|
8
|
+
|
|
9
|
+
NSString *const PushbibDeviceTokenNotification = @"PushbibDeviceTokenNotification";
|
|
10
|
+
NSString *const PushbibDeviceTokenErrorNotification = @"PushbibDeviceTokenErrorNotification";
|
|
11
|
+
|
|
12
|
+
static const NSUInteger kQueueCap = 20;
|
|
13
|
+
|
|
14
|
+
@interface PushbibEventBus ()
|
|
15
|
+
@property (nonatomic, strong) NSMutableArray<NSArray *> *queue;
|
|
16
|
+
@property (nonatomic, copy, nullable) PushbibEventHandler handler;
|
|
17
|
+
@property (nonatomic, strong) NSMutableDictionary<NSString *, void (^)(BOOL)> *pendingDisplays;
|
|
18
|
+
@end
|
|
19
|
+
|
|
20
|
+
@implementation PushbibEventBus
|
|
21
|
+
|
|
22
|
+
+ (instancetype)shared {
|
|
23
|
+
static PushbibEventBus *instance;
|
|
24
|
+
static dispatch_once_t once;
|
|
25
|
+
dispatch_once(&once, ^{
|
|
26
|
+
instance = [PushbibEventBus new];
|
|
27
|
+
});
|
|
28
|
+
return instance;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
- (instancetype)init {
|
|
32
|
+
if (self = [super init]) {
|
|
33
|
+
_queue = [NSMutableArray new];
|
|
34
|
+
_pendingDisplays = [NSMutableDictionary new];
|
|
35
|
+
}
|
|
36
|
+
return self;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
- (void)postEvent:(NSString *)name body:(NSDictionary *)body {
|
|
40
|
+
PushbibEventHandler handler;
|
|
41
|
+
@synchronized (self) {
|
|
42
|
+
handler = self.handler;
|
|
43
|
+
if (!handler) {
|
|
44
|
+
if (self.queue.count >= kQueueCap) [self.queue removeObjectAtIndex:0];
|
|
45
|
+
[self.queue addObject:@[ name, body ]];
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
handler(name, body);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
- (void)setHandler:(PushbibEventHandler)handler {
|
|
53
|
+
NSArray<NSArray *> *drained = nil;
|
|
54
|
+
@synchronized (self) {
|
|
55
|
+
_handler = [handler copy];
|
|
56
|
+
if (handler && self.queue.count > 0) {
|
|
57
|
+
drained = [self.queue copy];
|
|
58
|
+
[self.queue removeAllObjects];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (handler) {
|
|
62
|
+
for (NSArray *entry in drained) handler(entry[0], entry[1]);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
- (void)storeDisplayDecision:(NSString *)completionId
|
|
67
|
+
block:(void (^)(BOOL))block
|
|
68
|
+
timeout:(NSTimeInterval)timeout {
|
|
69
|
+
@synchronized (self.pendingDisplays) {
|
|
70
|
+
self.pendingDisplays[completionId] = [block copy];
|
|
71
|
+
}
|
|
72
|
+
__weak typeof(self) weakSelf = self;
|
|
73
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC)),
|
|
74
|
+
dispatch_get_main_queue(), ^{
|
|
75
|
+
[weakSelf completeDisplay:completionId display:YES];
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
- (void)completeDisplay:(NSString *)completionId display:(BOOL)display {
|
|
80
|
+
void (^block)(BOOL);
|
|
81
|
+
@synchronized (self.pendingDisplays) {
|
|
82
|
+
block = self.pendingDisplays[completionId];
|
|
83
|
+
if (block) [self.pendingDisplays removeObjectForKey:completionId];
|
|
84
|
+
}
|
|
85
|
+
if (block) block(display);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
4
|
+
|
|
5
|
+
@interface PushbibLiveActivities : NSObject
|
|
6
|
+
|
|
7
|
+
+ (void)reportActivityToken:(NSString *)tokenHex
|
|
8
|
+
forActivity:(NSString *)activityId
|
|
9
|
+
attributesType:(nullable NSString *)attributesType;
|
|
10
|
+
|
|
11
|
+
+ (void)reportPushToStartToken:(NSString *)tokenHex
|
|
12
|
+
attributesType:(nullable NSString *)attributesType;
|
|
13
|
+
|
|
14
|
+
+ (NSString *)hexStringFromData:(NSData *)data;
|
|
15
|
+
|
|
16
|
+
@end
|
|
17
|
+
|
|
18
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#import "PushbibLiveActivities.h"
|
|
2
|
+
#import "PushbibEventBus.h"
|
|
3
|
+
|
|
4
|
+
@implementation PushbibLiveActivities
|
|
5
|
+
|
|
6
|
+
+ (void)reportActivityToken:(NSString *)tokenHex
|
|
7
|
+
forActivity:(NSString *)activityId
|
|
8
|
+
attributesType:(NSString *)attributesType {
|
|
9
|
+
if (tokenHex.length == 0 || activityId.length == 0) {
|
|
10
|
+
NSLog(@"[Pushbib] reportActivityToken ignored — token and activityId are both required");
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
NSMutableDictionary *body = [@{
|
|
14
|
+
@"kind": @"activity",
|
|
15
|
+
@"token": tokenHex,
|
|
16
|
+
@"activityId": activityId,
|
|
17
|
+
} mutableCopy];
|
|
18
|
+
if (attributesType.length > 0) body[@"attributesType"] = attributesType;
|
|
19
|
+
|
|
20
|
+
[[PushbibEventBus shared] postEvent:PushbibEventLiveActivityToken body:body];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
+ (void)reportPushToStartToken:(NSString *)tokenHex
|
|
24
|
+
attributesType:(NSString *)attributesType {
|
|
25
|
+
if (tokenHex.length == 0) {
|
|
26
|
+
NSLog(@"[Pushbib] reportPushToStartToken ignored — empty token");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
NSMutableDictionary *body = [@{
|
|
30
|
+
@"kind": @"pushToStart",
|
|
31
|
+
@"token": tokenHex,
|
|
32
|
+
} mutableCopy];
|
|
33
|
+
if (attributesType.length > 0) body[@"attributesType"] = attributesType;
|
|
34
|
+
|
|
35
|
+
[[PushbibEventBus shared] postEvent:PushbibEventLiveActivityToken body:body];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
+ (NSString *)hexStringFromData:(NSData *)data {
|
|
39
|
+
if (data.length == 0) return @"";
|
|
40
|
+
const unsigned char *bytes = data.bytes;
|
|
41
|
+
NSMutableString *hex = [NSMutableString stringWithCapacity:data.length * 2];
|
|
42
|
+
for (NSUInteger i = 0; i < data.length; i++) [hex appendFormat:@"%02x", bytes[i]];
|
|
43
|
+
return hex;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@end
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
#import "PushbibModule.h"
|
|
2
|
+
|
|
3
|
+
#import <UIKit/UIKit.h>
|
|
4
|
+
#import <UserNotifications/UserNotifications.h>
|
|
5
|
+
#import <Security/Security.h>
|
|
6
|
+
#import <sys/utsname.h>
|
|
7
|
+
|
|
8
|
+
#import "PushbibEventBus.h"
|
|
9
|
+
|
|
10
|
+
static NSString *const kPrefsPrefix = @"pushbib_";
|
|
11
|
+
static NSString *const kDeviceIdService = @"com.amrshbib.pushbib.deviceid";
|
|
12
|
+
static NSString *const kDeviceIdAccount = @"device_id";
|
|
13
|
+
static const NSTimeInterval kTokenTimeout = 15.0;
|
|
14
|
+
|
|
15
|
+
static NSString *PushbibStableDeviceId(void) {
|
|
16
|
+
NSDictionary *lookup = @{
|
|
17
|
+
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
|
|
18
|
+
(__bridge id)kSecAttrService: kDeviceIdService,
|
|
19
|
+
(__bridge id)kSecAttrAccount: kDeviceIdAccount,
|
|
20
|
+
(__bridge id)kSecReturnData: @YES,
|
|
21
|
+
(__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne,
|
|
22
|
+
};
|
|
23
|
+
CFTypeRef found = NULL;
|
|
24
|
+
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)lookup, &found);
|
|
25
|
+
if (status == errSecSuccess && found != NULL) {
|
|
26
|
+
NSData *data = (__bridge_transfer NSData *)found;
|
|
27
|
+
NSString *existing = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
28
|
+
if (existing.length > 0) return existing;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
NSString *idfv = UIDevice.currentDevice.identifierForVendor.UUIDString;
|
|
32
|
+
NSString *newId = (idfv.length > 0) ? idfv : NSUUID.UUID.UUIDString;
|
|
33
|
+
NSDictionary *add = @{
|
|
34
|
+
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
|
|
35
|
+
(__bridge id)kSecAttrService: kDeviceIdService,
|
|
36
|
+
(__bridge id)kSecAttrAccount: kDeviceIdAccount,
|
|
37
|
+
(__bridge id)kSecValueData: [newId dataUsingEncoding:NSUTF8StringEncoding],
|
|
38
|
+
(__bridge id)kSecAttrAccessible: (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
|
|
39
|
+
};
|
|
40
|
+
SecItemDelete((__bridge CFDictionaryRef)add);
|
|
41
|
+
SecItemAdd((__bridge CFDictionaryRef)add, NULL);
|
|
42
|
+
return newId;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@implementation PushbibModule
|
|
46
|
+
|
|
47
|
+
RCT_EXPORT_MODULE(Pushbib);
|
|
48
|
+
|
|
49
|
+
+ (BOOL)requiresMainQueueSetup {
|
|
50
|
+
return NO;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
- (NSArray<NSString *> *)supportedEvents {
|
|
54
|
+
return @[ PushbibEventPushToken, PushbibEventClick, PushbibEventReceived,
|
|
55
|
+
PushbibEventForegroundWillDisplay, PushbibEventLiveActivityToken ];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
- (void)startObserving {
|
|
59
|
+
__weak typeof(self) weakSelf = self;
|
|
60
|
+
[[PushbibEventBus shared] setHandler:^(NSString *name, NSDictionary *body) {
|
|
61
|
+
[weakSelf sendEventWithName:name body:body];
|
|
62
|
+
}];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
- (void)stopObserving {
|
|
66
|
+
[[PushbibEventBus shared] setHandler:nil];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
RCT_EXPORT_METHOD(getDeviceInfo:(RCTPromiseResolveBlock)resolve
|
|
70
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
71
|
+
struct utsname systemInfo;
|
|
72
|
+
uname(&systemInfo);
|
|
73
|
+
NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]
|
|
74
|
+
?: UIDevice.currentDevice.model;
|
|
75
|
+
|
|
76
|
+
NSBundle *bundle = NSBundle.mainBundle;
|
|
77
|
+
NSLocale *locale = NSLocale.currentLocale;
|
|
78
|
+
NSString *deviceId = UIDevice.currentDevice.identifierForVendor.UUIDString;
|
|
79
|
+
resolve(@{
|
|
80
|
+
@"platform" : @"ios",
|
|
81
|
+
@"deviceId" : deviceId ?: (id)kCFNull,
|
|
82
|
+
@"osName" : @"iOS",
|
|
83
|
+
@"osVersion" : UIDevice.currentDevice.systemVersion ?: @"",
|
|
84
|
+
@"model" : model ?: @"",
|
|
85
|
+
@"bundleId" : bundle.bundleIdentifier ?: @"",
|
|
86
|
+
@"appVersion" : [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"] ?: @"",
|
|
87
|
+
@"language" : NSLocale.preferredLanguages.firstObject ?: @"en",
|
|
88
|
+
@"country" : [locale objectForKey:NSLocaleCountryCode] ?: @"",
|
|
89
|
+
@"timezone" : NSTimeZone.localTimeZone.name ?: @"",
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
RCT_EXPORT_METHOD(getStableDeviceId:(RCTPromiseResolveBlock)resolve
|
|
94
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
95
|
+
@try {
|
|
96
|
+
NSString *deviceId = PushbibStableDeviceId();
|
|
97
|
+
resolve(deviceId.length > 0 ? deviceId : (id)kCFNull);
|
|
98
|
+
} @catch (__unused NSException *e) {
|
|
99
|
+
resolve((id)kCFNull);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
RCT_EXPORT_METHOD(prefsGet:(NSString *)key
|
|
104
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
105
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
106
|
+
NSString *value =
|
|
107
|
+
[[NSUserDefaults standardUserDefaults] stringForKey:[kPrefsPrefix stringByAppendingString:key]];
|
|
108
|
+
resolve(value ?: (id)kCFNull);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
RCT_EXPORT_METHOD(prefsSet:(NSString *)key
|
|
112
|
+
value:(NSString *)value
|
|
113
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
114
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
115
|
+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
116
|
+
NSString *fullKey = [kPrefsPrefix stringByAppendingString:key];
|
|
117
|
+
if (value == nil || [value isKindOfClass:NSNull.class]) {
|
|
118
|
+
[defaults removeObjectForKey:fullKey];
|
|
119
|
+
} else {
|
|
120
|
+
[defaults setObject:value forKey:fullKey];
|
|
121
|
+
}
|
|
122
|
+
resolve(nil);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
RCT_EXPORT_METHOD(getPushToken:(NSString *)configJson
|
|
126
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
127
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
128
|
+
PushbibEventBus *bus = [PushbibEventBus shared];
|
|
129
|
+
if (bus.deviceTokenHex.length > 0) {
|
|
130
|
+
resolve(bus.deviceTokenHex);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
|
135
|
+
__block BOOL settled = NO;
|
|
136
|
+
__block id successObserver = nil;
|
|
137
|
+
__block id failureObserver = nil;
|
|
138
|
+
|
|
139
|
+
void (^cleanup)(void) = ^{
|
|
140
|
+
if (successObserver) [nc removeObserver:successObserver];
|
|
141
|
+
if (failureObserver) [nc removeObserver:failureObserver];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
successObserver = [nc addObserverForName:PushbibDeviceTokenNotification
|
|
145
|
+
object:nil
|
|
146
|
+
queue:NSOperationQueue.mainQueue
|
|
147
|
+
usingBlock:^(NSNotification *note) {
|
|
148
|
+
if (settled) return;
|
|
149
|
+
settled = YES;
|
|
150
|
+
cleanup();
|
|
151
|
+
resolve(note.userInfo[@"token"]);
|
|
152
|
+
}];
|
|
153
|
+
failureObserver = [nc addObserverForName:PushbibDeviceTokenErrorNotification
|
|
154
|
+
object:nil
|
|
155
|
+
queue:NSOperationQueue.mainQueue
|
|
156
|
+
usingBlock:^(NSNotification *note) {
|
|
157
|
+
if (settled) return;
|
|
158
|
+
settled = YES;
|
|
159
|
+
cleanup();
|
|
160
|
+
reject(@"pushbib/apns-failed", note.userInfo[@"message"], nil);
|
|
161
|
+
}];
|
|
162
|
+
|
|
163
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kTokenTimeout * NSEC_PER_SEC)),
|
|
164
|
+
dispatch_get_main_queue(), ^{
|
|
165
|
+
if (settled) return;
|
|
166
|
+
settled = YES;
|
|
167
|
+
cleanup();
|
|
168
|
+
reject(@"pushbib/token-timeout",
|
|
169
|
+
@"APNs did not return a device token in time (note: simulators cannot get "
|
|
170
|
+
@"push tokens; check the Push Notifications capability on a real device)",
|
|
171
|
+
nil);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
175
|
+
[[UIApplication sharedApplication] registerForRemoteNotifications];
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
RCT_EXPORT_METHOD(requestPermission:(RCTPromiseResolveBlock)resolve
|
|
180
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
181
|
+
UNAuthorizationOptions options =
|
|
182
|
+
UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
|
|
183
|
+
[[UNUserNotificationCenter currentNotificationCenter]
|
|
184
|
+
requestAuthorizationWithOptions:options
|
|
185
|
+
completionHandler:^(BOOL granted, NSError *_Nullable error) {
|
|
186
|
+
if (granted) {
|
|
187
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
188
|
+
[[UIApplication sharedApplication] registerForRemoteNotifications];
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
resolve(@(granted));
|
|
192
|
+
}];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
RCT_EXPORT_METHOD(hasPermission:(RCTPromiseResolveBlock)resolve
|
|
196
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
197
|
+
[[UNUserNotificationCenter currentNotificationCenter]
|
|
198
|
+
getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
|
199
|
+
BOOL granted = settings.authorizationStatus == UNAuthorizationStatusAuthorized ||
|
|
200
|
+
settings.authorizationStatus == UNAuthorizationStatusProvisional;
|
|
201
|
+
if (@available(iOS 14.0, *)) {
|
|
202
|
+
granted = granted || settings.authorizationStatus == UNAuthorizationStatusEphemeral;
|
|
203
|
+
}
|
|
204
|
+
resolve(@(granted));
|
|
205
|
+
}];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
RCT_EXPORT_METHOD(syncChannels:(NSString *)channelsJson
|
|
209
|
+
version:(NSString *)version
|
|
210
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
211
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
212
|
+
resolve(@(0));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
RCT_EXPORT_METHOD(listChannels:(RCTPromiseResolveBlock)resolve
|
|
216
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
217
|
+
resolve(@"[]");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
RCT_EXPORT_METHOD(openChannelSettings:(NSString *)channelId
|
|
221
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
222
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
223
|
+
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
|
224
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
225
|
+
if (url && [[UIApplication sharedApplication] canOpenURL:url]) {
|
|
226
|
+
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
|
|
227
|
+
resolve(@(YES));
|
|
228
|
+
} else {
|
|
229
|
+
resolve(@(NO));
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
RCT_EXPORT_METHOD(setForegroundHandling:(BOOL)enabled) {
|
|
235
|
+
[PushbibEventBus shared].hasForegroundHandler = enabled;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
RCT_EXPORT_METHOD(completeNotificationDisplay:(NSString *)completionId display:(BOOL)display) {
|
|
239
|
+
[[PushbibEventBus shared] completeDisplay:completionId display:display];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
RCT_EXPORT_METHOD(clearAllNotifications) {
|
|
243
|
+
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
|
|
244
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
245
|
+
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
RCT_EXPORT_METHOD(setBadgeCount:(double)count) {
|
|
250
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
251
|
+
[UIApplication sharedApplication].applicationIconBadgeNumber = (NSInteger)count;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
RCT_EXPORT_METHOD(getInitialClickEvent:(RCTPromiseResolveBlock)resolve
|
|
256
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
257
|
+
resolve((id)kCFNull);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
@end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#import <UIKit/UIKit.h>
|
|
2
|
+
#import <UserNotifications/UserNotifications.h>
|
|
3
|
+
#import <objc/message.h>
|
|
4
|
+
#import <objc/runtime.h>
|
|
5
|
+
|
|
6
|
+
#import "PushbibCenterDelegate.h"
|
|
7
|
+
#import "PushbibEventBus.h"
|
|
8
|
+
|
|
9
|
+
static IMP gOriginalAppSetDelegate = NULL;
|
|
10
|
+
static IMP gOriginalCenterSetDelegate = NULL;
|
|
11
|
+
|
|
12
|
+
static NSString *const kOriginalPrefix = @"pushbib_original_";
|
|
13
|
+
|
|
14
|
+
static void CallOriginal2(id self, SEL sel, id arg1, id arg2) {
|
|
15
|
+
if ([self respondsToSelector:sel]) {
|
|
16
|
+
((void (*)(id, SEL, id, id))objc_msgSend)(self, sel, arg1, arg2);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static SEL OriginalSelector(SEL sel) {
|
|
21
|
+
return NSSelectorFromString([kOriginalPrefix stringByAppendingString:NSStringFromSelector(sel)]);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static void Pushbib_didRegisterForRemoteNotifications(id self, SEL _cmd, UIApplication *application, NSData *deviceToken) {
|
|
25
|
+
NSMutableString *hex = [NSMutableString stringWithCapacity:deviceToken.length * 2];
|
|
26
|
+
const unsigned char *bytes = deviceToken.bytes;
|
|
27
|
+
for (NSUInteger i = 0; i < deviceToken.length; i++) [hex appendFormat:@"%02x", bytes[i]];
|
|
28
|
+
|
|
29
|
+
PushbibEventBus *bus = [PushbibEventBus shared];
|
|
30
|
+
bus.deviceTokenHex = hex;
|
|
31
|
+
bus.deviceTokenErrorMessage = nil;
|
|
32
|
+
[[NSNotificationCenter defaultCenter] postNotificationName:PushbibDeviceTokenNotification
|
|
33
|
+
object:nil
|
|
34
|
+
userInfo:@{@"token" : hex}];
|
|
35
|
+
[bus postEvent:PushbibEventPushToken body:@{@"token" : hex}];
|
|
36
|
+
|
|
37
|
+
CallOriginal2(self, OriginalSelector(@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)),
|
|
38
|
+
application, deviceToken);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static void Pushbib_didFailToRegisterForRemoteNotifications(id self, SEL _cmd, UIApplication *application, NSError *error) {
|
|
42
|
+
NSString *message = error.localizedDescription ?: @"APNs registration failed";
|
|
43
|
+
[PushbibEventBus shared].deviceTokenErrorMessage = message;
|
|
44
|
+
[[NSNotificationCenter defaultCenter] postNotificationName:PushbibDeviceTokenErrorNotification
|
|
45
|
+
object:nil
|
|
46
|
+
userInfo:@{@"message" : message}];
|
|
47
|
+
|
|
48
|
+
CallOriginal2(self, OriginalSelector(@selector(application:didFailToRegisterForRemoteNotificationsWithError:)),
|
|
49
|
+
application, error);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@interface PushbibSwizzler : NSObject
|
|
53
|
+
@end
|
|
54
|
+
|
|
55
|
+
@implementation PushbibSwizzler
|
|
56
|
+
|
|
57
|
+
+ (void)load {
|
|
58
|
+
static dispatch_once_t once;
|
|
59
|
+
dispatch_once(&once, ^{
|
|
60
|
+
[self hookApplicationSetDelegate];
|
|
61
|
+
[self hookCenterSetDelegate];
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
+ (void)hookApplicationSetDelegate {
|
|
66
|
+
Method method = class_getInstanceMethod(UIApplication.class, @selector(setDelegate:));
|
|
67
|
+
gOriginalAppSetDelegate = method_setImplementation(
|
|
68
|
+
method, imp_implementationWithBlock(^(UIApplication *application, id<UIApplicationDelegate> delegate) {
|
|
69
|
+
if (delegate) [PushbibSwizzler injectIntoDelegateClass:object_getClass(delegate)];
|
|
70
|
+
[PushbibSwizzler installCenterDelegateProxy];
|
|
71
|
+
((void (*)(id, SEL, id))gOriginalAppSetDelegate)(application, @selector(setDelegate:), delegate);
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
+ (void)injectIntoDelegateClass:(Class)cls {
|
|
76
|
+
static NSMutableSet<NSString *> *injected;
|
|
77
|
+
static dispatch_once_t once;
|
|
78
|
+
dispatch_once(&once, ^{
|
|
79
|
+
injected = [NSMutableSet new];
|
|
80
|
+
});
|
|
81
|
+
NSString *name = NSStringFromClass(cls) ?: @"?";
|
|
82
|
+
@synchronized (injected) {
|
|
83
|
+
if ([injected containsObject:name]) return;
|
|
84
|
+
[injected addObject:name];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
[self injectSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)
|
|
88
|
+
intoClass:cls
|
|
89
|
+
implementation:(IMP)Pushbib_didRegisterForRemoteNotifications];
|
|
90
|
+
[self injectSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)
|
|
91
|
+
intoClass:cls
|
|
92
|
+
implementation:(IMP)Pushbib_didFailToRegisterForRemoteNotifications];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
+ (void)injectSelector:(SEL)selector intoClass:(Class)cls implementation:(IMP)imp {
|
|
96
|
+
const char *typeEncoding = "v@:@@";
|
|
97
|
+
Method existing = class_getInstanceMethod(cls, selector);
|
|
98
|
+
if (existing) {
|
|
99
|
+
class_addMethod(cls, OriginalSelector(selector), method_getImplementation(existing),
|
|
100
|
+
method_getTypeEncoding(existing));
|
|
101
|
+
if (!class_addMethod(cls, selector, imp, typeEncoding)) {
|
|
102
|
+
method_setImplementation(class_getInstanceMethod(cls, selector), imp);
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
class_addMethod(cls, selector, imp, typeEncoding);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
+ (void)installCenterDelegateProxy {
|
|
110
|
+
static dispatch_once_t once;
|
|
111
|
+
dispatch_once(&once, ^{
|
|
112
|
+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
113
|
+
PushbibCenterDelegate *proxy = [PushbibCenterDelegate shared];
|
|
114
|
+
id<UNUserNotificationCenterDelegate> existing = center.delegate;
|
|
115
|
+
if (existing && existing != (id)proxy) proxy.original = existing;
|
|
116
|
+
center.delegate = proxy;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
+ (void)hookCenterSetDelegate {
|
|
121
|
+
Method method = class_getInstanceMethod(UNUserNotificationCenter.class, @selector(setDelegate:));
|
|
122
|
+
gOriginalCenterSetDelegate = method_setImplementation(
|
|
123
|
+
method,
|
|
124
|
+
imp_implementationWithBlock(^(UNUserNotificationCenter *center, id<UNUserNotificationCenterDelegate> delegate) {
|
|
125
|
+
PushbibCenterDelegate *proxy = [PushbibCenterDelegate shared];
|
|
126
|
+
if (delegate == nil || delegate == (id)proxy) {
|
|
127
|
+
((void (*)(id, SEL, id))gOriginalCenterSetDelegate)(center, @selector(setDelegate:), delegate);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
proxy.original = delegate;
|
|
131
|
+
((void (*)(id, SEL, id))gOriginalCenterSetDelegate)(center, @selector(setDelegate:), proxy);
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@end
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amrshbib/react-native-pushbib",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Self-hosted push notification SDK for React Native. Pushbib-style API (login, tags, click events) with zero Firebase setup in the app — no google-services.json, no GoogleService-Info.plist.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"react-native": "src/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"android/build.gradle",
|
|
11
|
+
"android/src",
|
|
12
|
+
"ios",
|
|
13
|
+
"index.d.ts",
|
|
14
|
+
"react-native-pushbib.podspec",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "node --test test"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"react-native",
|
|
22
|
+
"push-notifications",
|
|
23
|
+
"notifications",
|
|
24
|
+
"fcm",
|
|
25
|
+
"apns",
|
|
26
|
+
"self-hosted",
|
|
27
|
+
"pushbib-alternative"
|
|
28
|
+
],
|
|
29
|
+
"author": "Amr Shbib <amr.shbib@mythings.app>",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"homepage": "https://github.com/amrshbib/react-native-pushbib#readme",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/amrshbib/react-native-pushbib.git"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react": "*",
|
|
38
|
+
"react-native": ">=0.71.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "react-native-pushbib"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = { "Amr Shbib" => "amr.shbib@mythings.app" }
|
|
12
|
+
s.platforms = { :ios => "13.0" }
|
|
13
|
+
s.source = { :git => package["repository"]["url"], :tag => "v#{s.version}" }
|
|
14
|
+
|
|
15
|
+
s.source_files = "ios/**/*.{h,m,mm}"
|
|
16
|
+
s.frameworks = "UserNotifications", "Security"
|
|
17
|
+
|
|
18
|
+
s.dependency "React-Core"
|
|
19
|
+
end
|