Skip to content

[@nativescript/firebase-messaging-core] feat: support async callbacks for messages and notification taps #252

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
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
16 changes: 7 additions & 9 deletions packages/firebase-messaging-core/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ function deserialize(data: any): any {

export class MessagingCore implements IMessagingCore {
_APNSToken;
_onMessage(message: any) {
_onMessage(message: any, completionHandler: () => void) {
console.log('_onMessage', message);
if (onMessageCallbacks.size > 0) {
const msg = deserialize(message);
onMessageCallbacks.forEach((cb) => {
cb(msg);
});
Promise.all(Array.from(onMessageCallbacks).map((cb) => cb(msg))).finally(() => completionHandler());
} else {
MessagingCore._messageQueues._onMessage.push(message);
completionHandler();
}
}
_onToken(token: string) {
Expand All @@ -65,15 +64,14 @@ export class MessagingCore implements IMessagingCore {
MessagingCore._messageQueues._onToken.push(token);
}
}
_onNotificationTap(message: any) {
_onNotificationTap(message: any, completionHandler: () => void) {
console.log('_onNotificationTap', message);
if (onNotificationTapCallbacks.size > 0) {
const msg = deserialize(message);
onNotificationTapCallbacks.forEach((cb) => {
cb(msg);
});
Promise.all(Array.from(onNotificationTapCallbacks).map((cb) => cb(msg))).finally(() => completionHandler());
} else {
MessagingCore._messageQueues._onNotificationTap.push(message);
completionHandler();
}
}

Expand Down Expand Up @@ -336,7 +334,7 @@ export class MessagingCore implements IMessagingCore {
if (queue.length > 0) {
MessagingCore._messageQueues[type] = [];
queue.forEach((message) => {
this[type](message);
this[type](message, () => {});
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import <UIKit/UIKit.h>

typedef void(^BoolCallback)(BOOL success, NSError* _Nullable error);
typedef void(^DictionaryCallback)(NSDictionary* _Nullable value);
typedef void(^CompletableDictionaryCallback)(NSDictionary* _Nullable value, void(^_Nonnull complete)(void));
typedef void(^StringCallback)(NSString* _Nullable value);

@interface NSCFirebaseMessagingCore: NSObject
Expand All @@ -11,9 +11,9 @@ typedef void(^StringCallback)(NSString* _Nullable value);

@property (nonatomic, strong, class) BoolCallback _Nullable registerDeviceForRemoteMessagesCallback;

@property (nonatomic, strong, class) DictionaryCallback _Nullable onNotificationTapCallback;
@property (nonatomic, strong, class) CompletableDictionaryCallback _Nullable onNotificationTapCallback;

@property (nonatomic, strong, class) DictionaryCallback _Nullable onMessageCallback;
@property (nonatomic, strong, class) CompletableDictionaryCallback _Nullable onMessageCallback;

@property (nonatomic, strong, class) StringCallback _Nullable onTokenCallback;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ @implementation NSCFirebaseMessagingCore

BOOL ShowNotificationsWhenInForeground = NO;
BoolCallback RegisterDeviceForRemoteMessagesCallback = nil;
DictionaryCallback OnNotificationTapCallback = nil;
DictionaryCallback OnMessageCallback = nil;
CompletableDictionaryCallback OnNotificationTapCallback = nil;
CompletableDictionaryCallback OnMessageCallback = nil;
StringCallback OnTokenCallback = nil;

+ (void)setShowNotificationsWhenInForeground:(BOOL)showNotificationsWhenInForeground {
Expand All @@ -23,19 +23,19 @@ + (void)setRegisterDeviceForRemoteMessagesCallback:(BoolCallback)callback {
RegisterDeviceForRemoteMessagesCallback = callback;
}

+ (DictionaryCallback)onNotificationTapCallback {
+ (CompletableDictionaryCallback)onNotificationTapCallback {
return OnNotificationTapCallback;
}

+ (void)setOnNotificationTapCallback:(DictionaryCallback)callback {
+ (void)setOnNotificationTapCallback:(CompletableDictionaryCallback)callback {
OnNotificationTapCallback = callback;
}

+ (DictionaryCallback)onMessageCallback {
+ (CompletableDictionaryCallback)onMessageCallback {
return OnMessageCallback;
}

+ (void)setOnMessageCallback:(DictionaryCallback)callback {
+ (void)setOnMessageCallback:(CompletableDictionaryCallback)callback {
OnMessageCallback = callback;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public class NSCUIApplicationDelegate: UIResponder , UIApplicationDelegate {
message = parseRemoteMessage(userInfo)
#endif
message["foreground"] = application.applicationState == UIApplication.State.active
NSCFirebaseMessagingCore.onMessageCallback?(message)
completionHandler(.newData)

NSCFirebaseMessagingCore.onMessageCallback?(message) {
completionHandler(.newData)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ public class NSCUNUserNotificationCenterDelegate: NSObject, UNUserNotificationCe
if (remoteNotification["gcm.message_id"] != nil) {
var message = parseNotification(response.notification)
message["foreground"] = UIApplication.shared.applicationState == UIApplication.State.active
NSCFirebaseMessagingCore.onNotificationTapCallback?(message)
NSCFirebaseMessagingCore.onNotificationTapCallback?(message) {
completionHandler()
}
}else {
if((response.notification.request.trigger as? UNPushNotificationTrigger) != nil){
var message = remoteNotification
message["foreground"] = UIApplication.shared.applicationState == UIApplication.State.active
NSCFirebaseMessagingCore.onNotificationTapCallback?(message)
NSCFirebaseMessagingCore.onNotificationTapCallback?(message) {
completionHandler()
}
}
}

Expand All @@ -82,18 +86,22 @@ public class NSCUNUserNotificationCenterDelegate: NSObject, UNUserNotificationCe
if (notification.request.content.userInfo["gcm.message_id"] != nil) {
var message = parseNotification(notification)
if (message["contentAvailable"] == nil) {
NSCFirebaseMessagingCore.onMessageCallback?(message)
NSCFirebaseMessagingCore.onMessageCallback?(message) {
completionHandler(options)
}
message["foreground"] = UIApplication.shared.applicationState == UIApplication.State.active
} else {
completionHandler(options)
}
completionHandler(options)
return
}else {
if((notification.request.trigger as? UNPushNotificationTrigger) != nil){

var message = notification.request.content.userInfo
message["foreground"] = UIApplication.shared.applicationState == UIApplication.State.active
NSCFirebaseMessagingCore.onMessageCallback?(message)
completionHandler(options)
NSCFirebaseMessagingCore.onMessageCallback?(message) {
completionHandler(options)
}
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ declare class NSCFirebaseMessagingCore extends NSObject {

static new(): NSCFirebaseMessagingCore; // inherited from NSObject

static onMessageCallback: (p1: NSDictionary<any, any>) => void;
static onMessageCallback: (p1: NSDictionary<any, any>, p2: () => void) => void;

static onNotificationTapCallback: (p1: NSDictionary<any, any>) => void;
static onNotificationTapCallback: (p1: NSDictionary<any, any>, p2: () => void) => void;

static onTokenCallback: (p1: string) => void;

Expand Down