OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.56k stars 369 forks source link

OneSignal Background Notification Issue in iOS #1435

Open shamique opened 1 year ago

shamique commented 1 year ago

I followed this guide to setup the OneSignal background notification in My react native App: https://documentation.onesignal.com/docs/rn-android-native-module-setup-for-notification-service-extension#ios-notification-service-extension-module

Background notification is working as expected when I installed the App directly to My device through Xcode. But when I archive the build and install it from TestFlight, background notification doesn't work. Unless emitNotificationEvent event I added is not get triggered, even though the push notification is received.

When I trace the issue with Archived Build in Xcode (Using device console), noticed that _instance is null in NotificationExtensionModule.m. Anyone experienced similar issue or any idea what could be the reason ?

Note

Adding the code snippet for further understanding of My issue:

AppDelegate.h

#import <Foundation/Foundation.h>
#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeDelegate,UNUserNotificationCenterDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  //access NotificationServiceExtensionModule emitNotificationEvent method
  [NotificationServiceExtensionModule.sharedInstance emitNotificationEvent:userInfo ];
  completionHandler(UIBackgroundFetchResultNoData);
}

NotificationServiceExtensionModule.h

#import <foundation/Foundation.h>

// NotificationServiceExtensionModule.h
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface NotificationServiceExtensionModule : RCTEventEmitter <RCTBridgeModule>

+ (NotificationServiceExtensionModule*) sharedInstance;
- (void)emitNotificationEvent:(NSDictionary *)userInfo;

@end

NotificationServiceExtensionModule.m

#import <Foundation/Foundation.h>

// NotificationServiceExtensionModule.m
#import "NotificationServiceExtensionModule.h"

@implementation NotificationServiceExtensionModule

static NotificationServiceExtensionModule* _instance = nil;

+(NotificationServiceExtensionModule*) sharedInstance {
//    @synchronized( _instance ) {
//        if( !_instance ) {
//            _instance = [[NotificationServiceExtensionModule alloc] init];
//        }
//    }

    return _instance; // this returns null when installed from TestFlight. 
}

// To export a module named NotificationServiceExtensionModule
RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents
{
  NSLog(@"Supported EVENTS__________________________");
  _instance = self;
  return @[@"NotificationEvent"];
}

- (void)emitNotificationEvent:(NSDictionary *)userInfo
{
  NSString *eventName = userInfo[@"custom"][@"a"];
  [self sendEventWithName:@"NotificationEvent" body:@{@"notificationPayload": eventName}];
}

@end
jennantilla commented 1 year ago

Hi @shamique

Thanks for your patience!

How are you sending these notifications?

If you are sending the notification with content_available and no body, then you might need to double check that your production version also has the Background Modes/Remote Notifications capability added: https://documentation.onesignal.com/docs/react-native-sdk-setup#step-4-install-for-ios-using-cocoapods-for-ios-apps

Let us know if that helps!

angusmcleod commented 1 year ago

I am experiencing exactly the same issue as the OP, and my native code is identical to that posted in the OP.

The iOS Notification Service Extension Module sends notification events to the javascript in a build installed directly from xcode, but does not when a beta build is released via TestFlight. Here's some more detail

I'm using fastlane to deploy the beta build. This is the relevant logic in my fastfile.

lane :prepare do
    produce(
      app_identifier: "com.project.Project.OneSignalNotificationServiceExtension",
      app_name: "OneSignalNotificationServiceExtension",
      skip_itc: true
    )
    match(
      app_identifier: ["com.project.Project","com.project.Project.OneSignalNotificationServiceExtension"],
      type: "development",
    )
    match(
      app_identifier: ["com.project.Project","com.project.Project.OneSignalNotificationServiceExtension"],
      type: "appstore",
    )
end

desc "Push a new beta build to TestFlight"
lane :beta do
    app_store_connect_api_key(
      key_id: [key],
      issuer_id: [issuer],
      key_content: [secret],
      duration: 1200,
      in_house: false
    )
    prepare
    increment_build_number(xcodeproj: "Project.xcodeproj")
    build_app(
      workspace: "Project.xcworkspace",
      scheme: "Project (Production)",
      clean: true
    )
    upload_to_testflight
end
angusmcleod commented 1 year ago

@shamique Did you ever resolve this issue? @jennantilla Sorry to ping you again, but any other ideas?

Shamique99x commented 1 year ago

Hi @angusmcleod. No I couldn't resolve this issue. Instead, I downgraded the OneSignal library back to v3.4.2 since it's support background notification by default.

angusmcleod commented 1 year ago

@kesheshyan Is downgrading the only way here?

bwoodlt commented 8 months ago

Any update on this?

eduardojigub commented 2 months ago

Guys, @angusmcleod @shamique, any updates here? I have exactly the same problem I've been debugging for days and can't figure out anything else. My application works 100% perfect in debug mode, but in release it receives the push but doesn't do the actions I need to be done

shamique commented 2 months ago

Hi @eduardojigub, I couldn't resolve the issue. My last resort was to downgrade the One Signal version to v3.4.2, in which it supports background notification.