5

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

  • Xcode Version: 13.4
  • Receiving the push notification in both instance (from test flight or direct install)
  • To put a new version in TestFlight, used to increase the build number with same version number.
  • Tried cleaning the build folder, re-installing the pods, nothing worked still

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

8
  • 1. What do you mean it doesn't work? 2. Share code, not screenshots 3. Are you certain that the notification has arrived to the device? You can verify this by connecting your iPhone to macOS's console and look into the logs 4. Silent notifications have content-available:1 — without the alert field. However I see you trying to handle your code within NotificationServiceExtension. NotificationServiceExtension is meant to be used for notifications *which have alerts. Do you have the alert field in your payload? 1/2 Commented Sep 27, 2022 at 15:03
  • From docs: "Notification service app extensions only operate on remote notifications configured in the system to display an alert to the user. If alerts are disabled for your app, or if the payload specifies only the playing of a sound or the badging of an icon, the extension isn’t employed." 2/2 Commented Sep 27, 2022 at 15:04
  • 5. last but not least, are you sure your payloads are identical? 3/2 Commented Sep 27, 2022 at 15:14
  • 1. emitNotificationEvent I added in NotificationServiceExtensionModule doesn't trigger. But it get trigger if I install the App directly to the device. 2. Updated the main question 3. Yes, notification is received. It's just that emitNotificationEvent is not get triggered. Tried tracing the issue using logs in device console. 4. I am not using alert field in the request. 5. What does that mean ? You mean the payload I send is unique ? Commented Sep 28, 2022 at 11:26
  • 5. I mean is the apns payload identical? Did you verify that they're identical jsons? Commented Sep 28, 2022 at 13:21

1 Answer 1

0

You can use the following code to check if the app is running in the background or foreground:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  //access NotificationServiceExtensionModule emitNotificationEvent method
  [NotificationServiceExtensionModule.sharedInstance emitNotificationEvent:userInfo ];
  if (application.applicationState == UIApplicationStateActive) {
    // App was in the foreground when the notification was received
  } else {
    // App was in the background when the notification was received
  }
  completionHandler(UIBackgroundFetchResultNoData);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.