Есть ли способ вызвать RCT_EXPORT_METHOD внутри собственного iOS кода - PullRequest
0 голосов
/ 11 марта 2020

Я создал RCT_EXPORT_METHOD, который я хочу вызвать на основании какого-либо условия, используя цель c.

#import <Foundation/Foundation.h>
#import "PushNotificationAlert.h"
#import <React/RCTLog.h>
#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <MarketingCloudSDK/MarketingCloudSDK.h>
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "AppDelegate.h"
@implementation PushNotificationAlert

RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)applicationId accessToken:(NSString *)accessToken marketingCloudServerUrl:(NSString *)marketingCloudServerUrl)
{
    MarketingCloudSDKConfigBuilder *mcsdkBuilder = [MarketingCloudSDKConfigBuilder new];
      [mcsdkBuilder sfmc_setApplicationId:applicationId];
  [mcsdkBuilder sfmc_setAccessToken:accessToken];
      [mcsdkBuilder sfmc_setMarketingCloudServerUrl:marketingCloudServerUrl];

      NSError *error = nil;
      BOOL success =
          [[MarketingCloudSDK sharedInstance] sfmc_configureWithDictionary:[mcsdkBuilder sfmc_build]
                                                                     error:&error];
      if (success == YES) {
          dispatch_async(dispatch_get_main_queue(), ^{
            if (@available(iOS 10, *)) {
                // set the UNUserNotificationCenter delegate - the delegate must be set here in
                // didFinishLaunchingWithOptions
                [UNUserNotificationCenter currentNotificationCenter].delegate = self;
                [[UIApplication sharedApplication] registerForRemoteNotifications];

                [[UNUserNotificationCenter currentNotificationCenter]
                    requestAuthorizationWithOptions:UNAuthorizationOptionAlert |
                                                    UNAuthorizationOptionSound |
                                                    UNAuthorizationOptionBadge
                                  completionHandler:^(BOOL granted, NSError *_Nullable error) {
                                    if (error == nil) {
                                        if (granted == YES) {
                                          self.isEnabled = true;
//                                            dispatch_async(dispatch_get_main_queue(), ^{
//                                                           });
                                        } else {
                                          self.isEnabled = false;
                                        }

                                    }
                                  }];
            } else {
  #if __IPHONE_OS_VERSION_MIN_REQUIRED < 100000
                UIUserNotificationSettings *settings = [UIUserNotificationSettings
                    settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound |
                                     UIUserNotificationTypeAlert
                          categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  #endif
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }
          });
      } else {
          //  MarketingCloudSDK sfmc_configure failed
      }
}

RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback)
{
   NSMutableString *deviceToken = ((AppDelegate *)[UIApplication sharedApplication].delegate).deviceToken;
  if(self.isEnabled) {

    if (deviceToken != nil) {
      NSArray *events = [[NSArray alloc] initWithObjects:deviceToken, nil];
      callback(@[[NSNull null], events]);
    }
  } else {
    NSMutableString *deviceToken = ((AppDelegate *)[UIApplication sharedApplication].delegate).deviceToken;
    if (deviceToken != nil) {
      NSArray *events = [[NSArray alloc] initWithObjects:deviceToken, nil];
      callback(@[[NSNull null], events]);
    }
  }
}
@end

Как я могу позвонить RCT_EXPORT_METHOD, когда предоставлено разрешение?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...