Search code examples
iosobjective-creact-nativeazure-notificationhub

NotificationHub Register issue at IOS (React Native)


I'm trying to register notificationHub from React Native. I created a NativeModule and I'm sending connectionString, hubName, tags and deviceToken - deviceToken was saved to AsyncStorage (I'm getting deviceToken before user logged and I can't register Notification Hub without user tag (ex: scholarshipID) so I'm saving deviceToken for later usage. Because I can't send user tags to didRegisterForRemoteNotificationsWithDeviceToken from RN )

After user login I'm getting user scholarshipID & deviceToken and sending to native module for NotificationHub register.

 // deviceToken:(NSString *)tokenForLaterUsage

 [hub
   registerNativeWithDeviceToken:tokenForLaterUsage
   tags:self->_tags
   completion:^(NSError* error)
   {
     if (error != nil) {
       NSLog(@"Error registering for notifications: %@", error);
     }else{
       NSLog(@"Success");
     }
   }];

My tokenForLaterUsage is string and if I try use directly app is crashing. Because NotificationHub register function is waiting NSData so I'm converting NSString to NSData like this way:

    NSData* data = [tokenForLaterUsage dataUsingEncoding:NSUTF8StringEncoding];

After this I'm going Azure Portal and trying to send notification but system saying no device found.

BTW : When I try to register to NotificationHub in here: didRegisterForRemoteNotificationsWithDeviceToken it's work well but I can't set tags so this is not for me. From here I'm just getting deviceToken for later usage.

--

note: I'm thinking 2 workaround to solve this issue (But I'm not sure if works.):

  1. Workaround: I don't know but if I can I'll not register at first and when user will be login I'll get deviceToken on IOS and I'll try to register to NotificationHub.
  2. Workaround: I'll send my deviceToken to directly backend with restApi and backEnd will pair user tags and deviceToken.

Sorry things mixed but I need to solve this. Also sorry for my English :).


Solution

  • Finally I found the answer.

    At first when deviceToken is generated I'm storing at NSUserDefaults

    NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
    [userPreferences setObject:deviceToken forKey:@"deviceToken"];
    [userPreferences synchronize];
    

    And when user login I'm getting the deviceToken from NSUserDefault in NativeModules and sending to NotificationHub.

    NSData *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];