@amrshbib/react-native-pushbib 0.0.2 → 0.1.0
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 +122 -13
- package/android/src/main/java/com/amrshbib/pushbib/NotificationRenderer.kt +74 -6
- package/android/src/main/java/com/amrshbib/pushbib/PushbibModule.kt +39 -0
- package/index.d.ts +95 -5
- package/ios/NotificationService/Info.plist +40 -0
- package/ios/NotificationService/PushbibNotificationService.h +36 -0
- package/ios/NotificationService/PushbibNotificationService.m +192 -0
- package/ios/PushbibModule.m +37 -0
- package/package.json +1 -1
- package/react-native-pushbib.podspec +4 -1
- package/src/Logger.js +13 -0
- package/src/api.js +6 -0
- package/src/index.js +336 -38
- package/src/version.js +1 -1
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#import "PushbibNotificationService.h"
|
|
2
|
+
|
|
3
|
+
// iOS gives a service extension about 30 seconds and then shows the notification without
|
|
4
|
+
// whatever it was still doing. Give up well before that so a slow image never costs the
|
|
5
|
+
// user the notification itself.
|
|
6
|
+
static NSTimeInterval const kPushbibImageTimeout = 12.0;
|
|
7
|
+
|
|
8
|
+
// Anything larger is not worth the wait on a phone network — the notification arrives with
|
|
9
|
+
// its text instead of hanging on the download.
|
|
10
|
+
static long long const kPushbibMaxImageBytes = 10LL * 1024 * 1024;
|
|
11
|
+
|
|
12
|
+
@interface PushbibNotificationService ()
|
|
13
|
+
@property (nonatomic, copy, nullable) void (^contentHandler)(UNNotificationContent *);
|
|
14
|
+
@property (nonatomic, strong, nullable) UNMutableNotificationContent *content;
|
|
15
|
+
@property (nonatomic, strong, nullable) NSURLSessionDownloadTask *task;
|
|
16
|
+
@property (nonatomic, strong, nullable) NSURL *attachedFile;
|
|
17
|
+
@end
|
|
18
|
+
|
|
19
|
+
@implementation PushbibNotificationService
|
|
20
|
+
|
|
21
|
+
+ (NSArray<NSString *> *)imageURLKeys {
|
|
22
|
+
// A direct APNs push carries `imageUrl`; one routed through FCM carries the same URL under
|
|
23
|
+
// `fcm_options.image`. Reading both means the extension works either way.
|
|
24
|
+
return @[ @"imageUrl", @"fcm_options.image" ];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
- (void)willDeliverContent:(UNMutableNotificationContent *)content {
|
|
28
|
+
// Nothing by default.
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#pragma mark - UNNotificationServiceExtension
|
|
32
|
+
|
|
33
|
+
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
|
|
34
|
+
withContentHandler:(void (^)(UNNotificationContent *))contentHandler {
|
|
35
|
+
self.contentHandler = contentHandler;
|
|
36
|
+
self.content = [request.content mutableCopy];
|
|
37
|
+
|
|
38
|
+
NSURL *url = [self imageURLInUserInfo:request.content.userInfo];
|
|
39
|
+
if (url == nil) {
|
|
40
|
+
[self deliver];
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
NSURLSessionConfiguration *config = NSURLSessionConfiguration.ephemeralSessionConfiguration;
|
|
45
|
+
config.timeoutIntervalForResource = kPushbibImageTimeout;
|
|
46
|
+
config.timeoutIntervalForRequest = kPushbibImageTimeout;
|
|
47
|
+
|
|
48
|
+
__weak typeof(self) weakSelf = self;
|
|
49
|
+
self.task = [[NSURLSession sessionWithConfiguration:config]
|
|
50
|
+
downloadTaskWithURL:url
|
|
51
|
+
completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
|
|
52
|
+
[weakSelf attachDownloadAt:location response:response error:error sourceURL:url];
|
|
53
|
+
[weakSelf deliver];
|
|
54
|
+
}];
|
|
55
|
+
[self.task resume];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
- (void)serviceExtensionTimeWillExpire {
|
|
59
|
+
// Out of time: show the text rather than letting iOS drop our changes entirely.
|
|
60
|
+
[self.task cancel];
|
|
61
|
+
[self deliver];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#pragma mark - Reading the URL
|
|
65
|
+
|
|
66
|
+
- (nullable NSURL *)imageURLInUserInfo:(NSDictionary *)userInfo {
|
|
67
|
+
for (NSString *key in [self.class imageURLKeys]) {
|
|
68
|
+
id raw = [self valueAtKeyPath:key inDictionary:userInfo];
|
|
69
|
+
if (![raw isKindOfClass:NSString.class]) continue;
|
|
70
|
+
|
|
71
|
+
NSString *text = [(NSString *)raw stringByTrimmingCharactersInSet:
|
|
72
|
+
NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
73
|
+
if (text.length == 0) continue;
|
|
74
|
+
|
|
75
|
+
NSURL *url = [NSURL URLWithString:text];
|
|
76
|
+
// App Transport Security blocks plain http with no way to report it, so a non-https URL
|
|
77
|
+
// would fail silently. Skipping it here at least leaves a readable reason in the log.
|
|
78
|
+
if (url.scheme.length == 0 || url.host.length == 0) continue;
|
|
79
|
+
if (![url.scheme.lowercaseString isEqualToString:@"https"]) {
|
|
80
|
+
NSLog(@"[Pushbib] notification image skipped, iOS only loads https: %@", text);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
return url;
|
|
84
|
+
}
|
|
85
|
+
return nil;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
- (nullable id)valueAtKeyPath:(NSString *)path inDictionary:(NSDictionary *)dictionary {
|
|
89
|
+
id node = dictionary;
|
|
90
|
+
for (NSString *part in [path componentsSeparatedByString:@"."]) {
|
|
91
|
+
if (![node isKindOfClass:NSDictionary.class]) return nil;
|
|
92
|
+
node = ((NSDictionary *)node)[part];
|
|
93
|
+
if (node == nil || node == NSNull.null) return nil;
|
|
94
|
+
}
|
|
95
|
+
return node;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
#pragma mark - Attaching
|
|
99
|
+
|
|
100
|
+
- (void)attachDownloadAt:(nullable NSURL *)location
|
|
101
|
+
response:(nullable NSURLResponse *)response
|
|
102
|
+
error:(nullable NSError *)error
|
|
103
|
+
sourceURL:(NSURL *)sourceURL {
|
|
104
|
+
if (error != nil || location == nil) {
|
|
105
|
+
NSLog(@"[Pushbib] notification image download failed (%@): %@", sourceURL, error.localizedDescription);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if ([response isKindOfClass:NSHTTPURLResponse.class]) {
|
|
110
|
+
NSInteger status = ((NSHTTPURLResponse *)response).statusCode;
|
|
111
|
+
if (status < 200 || status > 299) {
|
|
112
|
+
NSLog(@"[Pushbib] notification image got HTTP %ld (%@)", (long)status, sourceURL);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (response.expectedContentLength > kPushbibMaxImageBytes) {
|
|
117
|
+
NSLog(@"[Pushbib] notification image is too large (%lld bytes)", response.expectedContentLength);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// UNNotificationAttachment decides the type from the file extension and rejects a file
|
|
122
|
+
// without a usable one, so the temp copy has to be named for what the server actually sent.
|
|
123
|
+
NSString *extension = [self extensionForResponse:response sourceURL:sourceURL];
|
|
124
|
+
if (extension == nil) {
|
|
125
|
+
NSLog(@"[Pushbib] notification image is not a type iOS can attach (%@)", response.MIMEType);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
NSURL *destination = [[NSURL fileURLWithPath:NSTemporaryDirectory()]
|
|
130
|
+
URLByAppendingPathComponent:[NSUUID.UUID.UUIDString stringByAppendingPathExtension:extension]];
|
|
131
|
+
|
|
132
|
+
NSError *moveError = nil;
|
|
133
|
+
if (![NSFileManager.defaultManager moveItemAtURL:location toURL:destination error:&moveError]) {
|
|
134
|
+
NSLog(@"[Pushbib] notification image could not be staged: %@", moveError.localizedDescription);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
self.attachedFile = destination;
|
|
138
|
+
|
|
139
|
+
NSError *attachError = nil;
|
|
140
|
+
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"pushbib-image"
|
|
141
|
+
URL:destination
|
|
142
|
+
options:nil
|
|
143
|
+
error:&attachError];
|
|
144
|
+
if (attachment == nil) {
|
|
145
|
+
NSLog(@"[Pushbib] notification image could not be attached: %@", attachError.localizedDescription);
|
|
146
|
+
[NSFileManager.defaultManager removeItemAtURL:destination error:nil];
|
|
147
|
+
self.attachedFile = nil;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
self.content.attachments = @[ attachment ];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
- (nullable NSString *)extensionForResponse:(nullable NSURLResponse *)response sourceURL:(NSURL *)sourceURL {
|
|
155
|
+
NSDictionary<NSString *, NSString *> *byMimeType = @{
|
|
156
|
+
@"image/jpeg" : @"jpg",
|
|
157
|
+
@"image/jpg" : @"jpg",
|
|
158
|
+
@"image/png" : @"png",
|
|
159
|
+
@"image/gif" : @"gif",
|
|
160
|
+
@"image/webp" : @"webp",
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
NSString *mimeType = response.MIMEType.lowercaseString;
|
|
164
|
+
NSString *fromMimeType = mimeType != nil ? byMimeType[mimeType] : nil;
|
|
165
|
+
if (fromMimeType != nil) return fromMimeType;
|
|
166
|
+
|
|
167
|
+
// No usable Content-Type: fall back to the extension the URL carries, which is how the
|
|
168
|
+
// Pushbib backend names its image URLs.
|
|
169
|
+
NSString *fromPath = sourceURL.pathExtension.lowercaseString;
|
|
170
|
+
if (fromPath.length > 0 && [byMimeType.allValues containsObject:fromPath]) return fromPath;
|
|
171
|
+
if ([fromPath isEqualToString:@"jpeg"]) return @"jpg";
|
|
172
|
+
|
|
173
|
+
return nil;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#pragma mark - Delivering
|
|
177
|
+
|
|
178
|
+
- (void)deliver {
|
|
179
|
+
// Both the download callback and the expiry handler can land here; whichever is second
|
|
180
|
+
// must not call iOS back a second time.
|
|
181
|
+
void (^handler)(UNNotificationContent *) = self.contentHandler;
|
|
182
|
+
if (handler == nil) return;
|
|
183
|
+
self.contentHandler = nil;
|
|
184
|
+
|
|
185
|
+
UNMutableNotificationContent *content = self.content;
|
|
186
|
+
if (content == nil) return;
|
|
187
|
+
|
|
188
|
+
[self willDeliverContent:content];
|
|
189
|
+
handler(content);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
@end
|
package/ios/PushbibModule.m
CHANGED
|
@@ -239,6 +239,43 @@ RCT_EXPORT_METHOD(completeNotificationDisplay:(NSString *)completionId display:(
|
|
|
239
239
|
[[PushbibEventBus shared] completeDisplay:completionId display:display];
|
|
240
240
|
}
|
|
241
241
|
|
|
242
|
+
// OSNotificationPermission: 0 not determined, 1 denied, 2 authorized, 3 provisional, 4 ephemeral
|
|
243
|
+
RCT_EXPORT_METHOD(permissionNative:(RCTPromiseResolveBlock)resolve
|
|
244
|
+
reject:(RCTPromiseRejectBlock)reject) {
|
|
245
|
+
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
|
246
|
+
NSInteger value;
|
|
247
|
+
switch (settings.authorizationStatus) {
|
|
248
|
+
case UNAuthorizationStatusNotDetermined: value = 0; break;
|
|
249
|
+
case UNAuthorizationStatusDenied: value = 1; break;
|
|
250
|
+
case UNAuthorizationStatusAuthorized: value = 2; break;
|
|
251
|
+
case UNAuthorizationStatusProvisional: value = 3; break;
|
|
252
|
+
default: value = 4; break;
|
|
253
|
+
}
|
|
254
|
+
resolve(@(value));
|
|
255
|
+
}];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Android-only in OneSignal; here they clear delivered notifications so the API matches.
|
|
259
|
+
RCT_EXPORT_METHOD(removeNotification:(double)identifier) {
|
|
260
|
+
NSString *key = [NSString stringWithFormat:@"%lld", (long long)identifier];
|
|
261
|
+
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[key]];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
RCT_EXPORT_METHOD(removeGroupedNotifications:(NSString *)group) {
|
|
265
|
+
if (group.length == 0) return;
|
|
266
|
+
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *delivered) {
|
|
267
|
+
NSMutableArray<NSString *> *ids = [NSMutableArray array];
|
|
268
|
+
for (UNNotification *notification in delivered) {
|
|
269
|
+
if ([notification.request.content.threadIdentifier isEqualToString:group]) {
|
|
270
|
+
[ids addObject:notification.request.identifier];
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (ids.count > 0) {
|
|
274
|
+
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:ids];
|
|
275
|
+
}
|
|
276
|
+
}];
|
|
277
|
+
}
|
|
278
|
+
|
|
242
279
|
RCT_EXPORT_METHOD(clearAllNotifications) {
|
|
243
280
|
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
|
|
244
281
|
dispatch_async(dispatch_get_main_queue(), ^{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amrshbib/react-native-pushbib",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
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
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -12,7 +12,10 @@ Pod::Spec.new do |s|
|
|
|
12
12
|
s.platforms = { :ios => "13.0" }
|
|
13
13
|
s.source = { :git => package["repository"]["url"], :tag => "v#{s.version}" }
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
# The extension is NOT a pod. Its target compiles ios/NotificationService straight out of
|
|
16
|
+
# node_modules and links only UserNotifications, so an app adds the target and nothing
|
|
17
|
+
# else — no Podfile entry, no pod install, no file of its own to keep in sync.
|
|
18
|
+
s.source_files = "ios/*.{h,m,mm}"
|
|
16
19
|
s.frameworks = "UserNotifications", "Security"
|
|
17
20
|
|
|
18
21
|
s.dependency "React-Core"
|
package/src/Logger.js
CHANGED
|
@@ -2,7 +2,20 @@ const LEVELS = { none: 0, error: 1, warn: 2, info: 3, verbose: 4 };
|
|
|
2
2
|
|
|
3
3
|
let current = LEVELS.warn;
|
|
4
4
|
|
|
5
|
+
// OneSignal's LogLevel enum (None 0 … Verbose 6) mapped onto our five levels, so
|
|
6
|
+
// Debug.setLogLevel(LogLevel.Verbose) and setLogLevel('verbose') both work.
|
|
7
|
+
const NUMERIC_LEVELS = [LEVELS.none, LEVELS.error, LEVELS.error, LEVELS.warn, LEVELS.info, LEVELS.verbose, LEVELS.verbose];
|
|
8
|
+
|
|
5
9
|
export function setLogLevel(level) {
|
|
10
|
+
if (typeof level === 'number') {
|
|
11
|
+
const mapped = NUMERIC_LEVELS[level];
|
|
12
|
+
if (mapped === undefined) {
|
|
13
|
+
console.warn(`[Pushbib] Unknown log level ${level} — use 0..${NUMERIC_LEVELS.length - 1} or ${Object.keys(LEVELS).join('/')}`);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
current = mapped;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
6
19
|
if (LEVELS[level] === undefined) {
|
|
7
20
|
console.warn(`[Pushbib] Unknown log level "${level}" — use ${Object.keys(LEVELS).join('/')}`);
|
|
8
21
|
return;
|
package/src/api.js
CHANGED
|
@@ -74,6 +74,9 @@ export class ApiClient {
|
|
|
74
74
|
pingSession(payload) {
|
|
75
75
|
return this.post('/devices/session', payload);
|
|
76
76
|
}
|
|
77
|
+
setProfile(payload) {
|
|
78
|
+
return this.post('/devices/profile', payload);
|
|
79
|
+
}
|
|
77
80
|
setLocation(payload) {
|
|
78
81
|
return this.post('/devices/location', payload);
|
|
79
82
|
}
|
|
@@ -102,6 +105,9 @@ export class ApiClient {
|
|
|
102
105
|
endLiveActivity(payload) {
|
|
103
106
|
return this.post('/live-activity/end', payload);
|
|
104
107
|
}
|
|
108
|
+
removePushToStart(payload) {
|
|
109
|
+
return this.delete('/live-activity/push-to-start', payload);
|
|
110
|
+
}
|
|
105
111
|
liveActivitySessions(subscriptionId) {
|
|
106
112
|
return this.get(`/live-activity/sessions?subscription_id=${encodeURIComponent(subscriptionId)}`);
|
|
107
113
|
}
|