Skip to content
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

fix(ios): Remote issues: #1041 / #1096 onForegroundEvent() and getInitialNotification() #1118

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 12 additions & 7 deletions ios/NotifeeCore/NotifeeCore+NSNotificationCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ + (void)load {
#pragma mark Application Notifications

- (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)notification {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *launchNotification =
(UILocalNotification *)notification.userInfo[UIApplicationLaunchOptionsLocalNotificationKey];
[[NotifeeCoreUNUserNotificationCenter instance]
onDidFinishLaunchingNotification:launchNotification.userInfo];
[[NotifeeCoreUNUserNotificationCenter instance] getInitialNotification];
NSDictionary *notifUserInfo =
notification.userInfo[UIApplicationLaunchOptionsLocalNotificationKey];

if (!notifUserInfo) {
// Fallback to remote notification key if local notification key is not available
notifUserInfo = notification.userInfo[UIApplicationLaunchOptionsRemoteNotificationKey];
}

if (notifUserInfo) {
[[NotifeeCoreUNUserNotificationCenter instance] onDidFinishLaunchingNotification:notifUserInfo];
[[NotifeeCoreUNUserNotificationCenter instance] getInitialNotification];
}
mikehardy marked this conversation as resolved.
Show resolved Hide resolved

[[NotifeeCoreUNUserNotificationCenter instance] observe];
}
Expand Down
36 changes: 23 additions & 13 deletions ios/NotifeeCore/NotifeeCore+UNUserNotificationCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#import "NotifeeCoreUtil.h"

@implementation NotifeeCoreUNUserNotificationCenter

struct {
unsigned int willPresentNotification : 1;
unsigned int didReceiveNotificationResponse : 1;
Expand Down Expand Up @@ -73,12 +72,31 @@ - (void)onDidFinishLaunchingNotification:(nonnull NSDictionary *)notifUserInfo {

- (nullable NSDictionary *)getInitialNotification {
if (_initialNotificationGathered && _initialNotificationBlock != nil) {

// copying initial notification
if (_initialNotification != nil &&
[_initialNoticationID isEqualToString:_notificationOpenedAppID]) {
NSDictionary *initialNotificationCopy = [_initialNotification copy];

NSMutableDictionary *event = [NSMutableDictionary dictionary];
NSMutableDictionary *initialNotificationCopy = [_initialNotification mutableCopy];
initialNotificationCopy[@"initialNotification"] = @1;

_initialNotification = nil;

// Sets the return payload for getInitialNotification()
_initialNotificationBlock(nil, initialNotificationCopy);

// Prepare onForegroundEvent() payload
event[@"detail"] = [initialNotificationCopy copy];
if ([event[@"detail"][@"pressAction"][@"id"] isEqualToString:@"default"]) {
event[@"type"] = @1; // PRESS
} else {
event[@"type"] = @2; // ACTION_PRESS
}

// Call onForegroundEvent() on Javascript side with payload:
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:event];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚧 Update:

I found another funny issue with the patch :( But should be an easy fix. This is the workaround however:

useEffect(() => {
    /** 🚧 Bug with Notifee patch
     * if we don't call this, we don't get onForegroundEvent if app was opened
     * from a terminated state by pressing notification
     * @todo Remove once this is fixed:
     * @link https://github.com/invertase/notifee/pull/1118 */
    notifee.getInitialNotification();

    const unsubscribe = notifee.onForegroundEvent(async ({ type, detail }) => {

    });

    return unsubscribe;
  }, [dispatch]);
};


} else {
_initialNotificationBlock(nil, nil);
}
Expand All @@ -104,7 +122,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
NSDictionary *notifeeNotification =
notification.request.content.userInfo[kNotifeeUserInfoNotification];

// we only care about notifications created through notifee
if (notifeeNotification != nil) {
UNNotificationPresentationOptions presentationOptions = UNNotificationPresentationOptionNone;
NSDictionary *foregroundPresentationOptions =
Expand Down Expand Up @@ -148,23 +165,16 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
presentationOptions |= UNNotificationPresentationOptionAlert;
}

NSDictionary *notifeeTrigger = notification.request.content.userInfo[kNotifeeUserInfoTrigger];
if (notifeeTrigger != nil) {
// post DELIVERED event
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:@{
// post DELIVERED event
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:@{
@"type" : @(NotifeeCoreEventTypeDelivered),
@"detail" : @{
@"notification" : notifeeNotification,
}
}];
}
}];

completionHandler(presentationOptions);

} else if (_originalDelegate != nil && originalUNCDelegateRespondsTo.willPresentNotification) {
[_originalDelegate userNotificationCenter:center
willPresentNotification:notification
withCompletionHandler:completionHandler];
}
}

Expand Down