React-native-onesignal: received, opened never fired on IOS.

Created on 2 Mar 2017  路  13Comments  路  Source: OneSignal/react-native-onesignal

version: 3.0.3

I'm able to receive a notification but the functions received or opened are never fired or at least console log doesn't show the infromation.

I checked this when the app was enabled as well as working in a background.

What is more, if I click on the notification it doesn't change the value in OneSignal (Clicked).

class RootContainer extends Component {
  componentDidMount () {
    // if redux persist is not active fire startup action
    if (!ReduxPersist.active) {
      this.props.startup()
    }
  }


  componentWillMount() {
    OneSignal.addEventListener('received', this._onReceived.bind(this));
    OneSignal.addEventListener('opened', this._onOpened.bind(this));
    OneSignal.addEventListener('registered', this._onRegistered.bind(this));
    OneSignal.addEventListener('ids', this._onIds.bind(this));
  }

  componentWillUnmount() {
    //OneSignal.removeEventListener('received', this._onReceived.bind(this));
    //OneSignal.removeEventListener('opened', this._onOpened.bind(this));
    OneSignal.removeEventListener('registered', this._onRegistered.bind(this));
    OneSignal.removeEventListener('ids', this._onIds.bind(this));
  }

  _onIds ({pushToken, userId}){
    this.props.pushTokenIdsAvailable(pushToken, userId)
  }
  _onReceived(notification) {
    console.log("Notification received: ", notification);
  }

  _onOpened(openResult) {
    console.log('Message: ', openResult.notification.payload.body);
    console.log('Data: ', openResult.notification.payload.additionalData);
    console.log('isActive: ', openResult.notification.isAppInFocus);
    console.log('openResult: ', openResult);
  }

  _onRegistered(notifData) {
    console.log("Device had been registered for push notifications!", notifData);
  }



  render () {
    return (
          <NavigationRouter  />
    )
  }
}

const mapStateToDispatch = dispatch => ({
  startup: () => dispatch(StartupActions.startup()),
  pushTokenIdsAvailable: (pushToken, userId) => dispatch(ConfigActions.pushTokenAvailable(pushToken, userId))
})

export default connect(null, mapStateToDispatch)(RootContainer)

My AppDelegate.m

#import "AppDelegate.h"

#import "RCTBundleURLProvider.h"
#import "RCTRootView.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <OneSignal/OneSignal.h>

@implementation AppDelegate

@synthesize oneSignal = _oneSignal;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  self.oneSignal = [[RCTOneSignal alloc] initWithLaunchOptions:launchOptions
                                                         appId:@"ecae5810-fcc6-4c40-8568-adb3947d5b0c"
                    settings:@{kOSSettingsKeyInFocusDisplayOption : @(OSNotificationDisplayTypeNotification), kOSSettingsKeyAutoPrompt : @YES}];

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"TennisApp"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  //return YES;
  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                  didFinishLaunchingWithOptions:launchOptions];
}

// Facebook SDK
- (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                        openURL:url
                                              sourceApplication:sourceApplication
                                                     annotation:annotation];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification {
  [RCTOneSignal didReceiveRemoteNotification:notification];
}

@end

Most helpful comment

@avishayil @zawarudo guys, after setting content_available to true when sending notification through REST API, onreceive is fired. Thanks for the help :)

All 13 comments

I' ve the same problem, someone know how to solve it?

Having the same issue, cannot track down the issue.

Received will fire if the app is in focus, but in the background it will not.

Edit: I have other things receiving remote data in the background just fine.

@zawarudo zawarudo, have you got any updates on this issue? I faced the exact same issue as you, where onreceived will fire when app is in focus, but not when its on background.

Hi guys, could any one of you share a repo with the issue reproduced? I'll try to take a look on this.

@LEEY19 Unsolved, moved away from onesignal to compensate.

@avishayil we literally just followed the example on this github readme. everything works as expected except that onreceive is not fired when app is on background on iOS

@LEEY19 I'll have a look on this.

@avishayil @zawarudo guys, after setting content_available to true when sending notification through REST API, onreceive is fired. Thanks for the help :)

@LEEY19 Glad you sorted it out, if I remember correctly I did have this set to no avail.

@avishayil Ok this is really weird. setting content_available to true did fire onreceive on iOS for debug build. But when it comes to release build, the issue happens again. Also, is it a limitation of the library, that when the app is originally closed (neither foreground nor background), tapping the notif on the notif drawer does not trigger onopened?

@avishayil i think this onreceive not firing issue on iOS has something to do with this
https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623013-application?language=objc

can you look into this ? Our current declaration is this:

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification {
    [RCTOneSignal didReceiveRemoteNotification:notification];
    }

but seems like the 'fetchCompletionHandler:' needs to be added

@avishayil Further testing reveals this finding:
When iOS release build has the scheme running on xcode, everything works fine. But when the scheme is stopped and the app is run again, onreceive does not fire when app is on background.

However, Android release build works fine.

@LEEY19 Hi, did you solve the problem? I'm having the same problem.

Was this page helpful?
0 / 5 - 0 ratings