Skip to content

Add inbox silent notifications support #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ UPCOMING
- Added `messagingCustomPayload` property to `BatchMessagingEventPayload` (only for In-App Message).
- Added `pushPayload` property to `BatchMessagingEventPayload` (only for Landing Mobile).

**Inbox**
- Added `isSilent` property to `IInboxNotification`.
- Added `setFilterSilentNotifications` method to `BatchInboxFetcher`. Default value is true.
- ⚠️ BREAKING: `body` property from `IInboxNotification` is now nullable since the inbox fetcher may not filter silent notifications.


9.0.2
----
Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/com/batch/batch_rn/RNBatchInbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static WritableMap getWritableMapNotification(BatchInboxNotificationCont
output.putString("identifier", notification.getNotificationIdentifier());
output.putString("body", notification.getBody());
output.putBoolean("isUnread", notification.isUnread());
output.putBoolean("isSilent", notification.isSilent());
output.putDouble("date", notification.getDate().getTime());

int source = 0; // UNKNOWN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,15 @@ public void inbox_fetcher_displayLandingMessage(Activity currentActivity, String
promise.resolve(null);
}

public void inbox_fetcher_setFilterSilentNotifications(String fetcherIdentifier, boolean filterSilentNotifications, Promise promise) {
if (!this.batchInboxFetcherMap.containsKey(fetcherIdentifier)) {
promise.reject("InboxError", "FETCHER_NOT_FOUND");
return;
}
BatchInboxFetcher fetcher = this.batchInboxFetcherMap.get(fetcherIdentifier);
fetcher.setFilterSilentNotifications(filterSilentNotifications);
}

// USER MODULE

public void user_getInstallationId(Promise promise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ public void inbox_fetcher_displayLandingMessage(String fetcherIdentifier, String
impl.inbox_fetcher_displayLandingMessage(getCurrentActivity(), fetcherIdentifier, notificationIdentifier, promise);
}

@Override
public void inbox_fetcher_setFilterSilentNotifications(String fetcherIdentifier, boolean filterSilentNotifications, Promise promise) {
impl.inbox_fetcher_setFilterSilentNotifications(fetcherIdentifier, filterSilentNotifications, promise);
}

// USER MODULE

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ public void inbox_fetcher_displayLandingMessage(String fetcherIdentifier, String
impl.inbox_fetcher_displayLandingMessage(getCurrentActivity(), fetcherIdentifier, notificationIdentifier, promise);
}

@ReactMethod
public void inbox_fetcher_setFilterSilentNotifications(String fetcherIdentifier, boolean filterSilentNotifications, final Promise promise) {
impl.inbox_fetcher_setFilterSilentNotifications(fetcherIdentifier, filterSilentNotifications, promise);
}

// USER MODULE

@ReactMethod
Expand Down
31 changes: 27 additions & 4 deletions ios/RNBatch.mm
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,27 @@ - (BatchInboxNotificationContent *) findNotificationInList: (NSArray<BatchInboxN
reject(@"InboxError", @"NOTIFICATION_NOT_FOUND", nil);
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[notification displayLandingMessage];
resolve([NSNull null]);
});
}

RCT_EXPORT_METHOD(inbox_fetcher_setFilterSilentNotifications:
(NSString *) fetcherIdentifier
filterSilentNotifications:
(BOOL) filterSilentNotifications
resolve:
(RCTPromiseResolveBlock) resolve
reject:
(RCTPromiseRejectBlock) reject) {
BatchInboxFetcher* fetcher = _batchInboxFetcherMap[fetcherIdentifier];
if (!fetcher) {
reject(@"InboxError", @"FETCHER_NOT_FOUND", nil);
return;
}

[notification displayLandingMessage];
[fetcher setFilterSilentNotifications:filterSilentNotifications];
resolve([NSNull null]);
}

Expand Down Expand Up @@ -871,22 +890,26 @@ - (NSDictionary*) dictionaryWithNotification:(BatchInboxNotificationContent*)not
}

NSString *title = notification.message.title;
NSString *body = notification.message.body;

NSDictionary *output = @{
@"identifier": notification.identifier,
@"body": notification.message.body,
@"isUnread": @(notification.isUnread),
@"isSilent": @(notification.isSilent),
@"date": [NSNumber numberWithDouble:notification.date.timeIntervalSince1970 * 1000],
@"source": source,
@"payload": notification.payload,
@"hasLandingMessage": @(notification.hasLandingMessage)
};

NSMutableDictionary *mutableOutput = [output mutableCopy];
if (title != nil) {
NSMutableDictionary *mutableOutput = [output mutableCopy];
mutableOutput[@"title"] = title;
output = mutableOutput;
}
if (body != nil) {
mutableOutput[@"body"] = body;
}
output = mutableOutput;
return output;
}

Expand Down
9 changes: 7 additions & 2 deletions src/BatchInbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export interface IInboxNotification {
title?: string;

/**
* Notification alert body
* Notification alert body (if notification not silent)
*/
body: string;
body?: string;

/**
* URL of the rich notification attachment (image/audio/video) - iOS Only
Expand All @@ -44,6 +44,11 @@ export interface IInboxNotification {
*/
isUnread: boolean;

/**
* Flag indicating whether this notification is silent or not
*/
isSilent: boolean;

/**
* The push notification's source, indicating what made Batch send it.
* It can come from a push campaign via the API or the dashboard, or from the transactional API, for example.
Expand Down
12 changes: 11 additions & 1 deletion src/BatchInboxFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ export class BatchInboxFetcher {
return RNBatch.inbox_fetcher_displayLandingMessage(this.identifier, notificationIdentifier);
}

/**
* Sets whether the SDK should filter silent notifications (pushes that don't result in a message being
* shown to the user). Default: true
*
* @param filterSilentNotifications Whether the SDK should filter silent notifications
*/
public setFilterSilentNotifications(filterSilentNotifications: boolean): Promise<void> {
return RNBatch.inbox_fetcher_setFilterSilentNotifications(this.identifier, filterSilentNotifications);
}

/**
* Fetches new notifications (and resets pagination to 0).
*
Expand Down Expand Up @@ -109,7 +119,7 @@ const parseNotifications = (notifications: IInboxNotification[]): IInboxNotifica
...notification,
payload: {
...(notification.payload as Record<string, unknown>),
'com.batch': JSON.parse(batchPayload),
'com.batch': typeof batchPayload == 'string' ? JSON.parse(batchPayload) : batchPayload,
},
};
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions src/NativeRNBatchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface Spec extends TurboModule {
endReached: boolean;
}>;
inbox_fetcher_displayLandingMessage(fetcherIdentifier: string, notificationIdentifier: string): Promise<void>;
inbox_fetcher_setFilterSilentNotifications(fetcherIdentifier: string, filterSilentNotifications: boolean): Promise<void>;

// User Module
user_getInstallationId(): Promise<string>;
Expand Down
Loading