Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Unfortunately this is no longer working, can you provide some insight? #14

Open
bradennapier opened this issue Jun 16, 2016 · 2 comments

Comments

@bradennapier
Copy link

bradennapier commented Jun 16, 2016

Hello,
Absolutely love the idea of this package but it unfortunately no longer works. Your update categories is calling to register the app, whcih is call the registration events, which your app is listening to so that it can again register the app - therefore we have ourselves an endless loop :(

From what I can tell, this would have been broken sinc eReact Native version 0.17 when they started emitting the loop

RCT_EXPORT_METHOD(updateCategories:(NSArray *)json)
{
    NSMutableArray *categories = [[NSMutableArray alloc] init];
    // Create the category
    for (NSDictionary *categoryJSON in json) {
        [categories addObject:[self categoryFromJSON:categoryJSON]];
    }

    // Get the current types
    UIUserNotificationSettings *settings;
    UIUserNotificationType types = settings.types;

    NSLog(@"Settings: %@", settings);
    // Update the settings for these types
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithArray:categories]]];
}

in the code above at the end you are calling: [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithArray:categories]]];

React's official implementation listens to changes to this value:

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  NSLog(@"Register Push!");
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}

As you can see here it is then emitting the didRegister command...

This then calls the paired part in React Native's push notification implementation:

+ (void)didRegisterUserNotificationSettings:(__unused UIUserNotificationSettings *)notificationSettings
{
  if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)]) {
    NSLog(@"REGISTER NOW!");
    [[UIApplication sharedApplication] registerForRemoteNotifications];
  }
}

It would appear the "if" is always true because REGISTER NOW loops as well.

This emits this function:

- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
{
  NSLog(@"Remote Notifications Registered: %@", notification);
  [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered"
                                              body:notification.userInfo];
}

which then emits the registration event which is being listened to by updateCategories:

NativeAppEventEmitter.addListener('remoteNotificationsRegistered', () => {
    console.info('updating notification categories in response to permission change');
    RNNotificationActions.updateCategories(cats);
  });

and thus, the loop continues, indefinitely.

image

@RajanJS
Copy link

RajanJS commented Mar 28, 2019

@bradennapier @antonkulyk did you found a solution for the tokens generating infinitely?

@rayzeller
Copy link

@RajanJS this is what i'm doing right now and it seems to work:

ourfabriq@d7287b0

Basically, add and remove an event listener right away. Haven't tested it in production yet, but I'm now able to use the onRegister callback in react-native-push-notification

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants