Integration while using Expo tools - infobip/mobile-messaging-react-native-plugin GitHub Wiki

Resolving the error: ExpoDevLauncherReactDelegateHandler.swift:43: Fatal error: TheUIApplication.shared.delegate is not a RCTAppDelegate instance"

If you encounter this or a similar error while running your application on iOS with Expo tools, you can resolve it by making the following change:

Instead of using [MobileMessagingPluginApplicationDelegate install]; in the AppDelegate.mm directly call the corresponding MobileMessaging SDK methods to handle the device token and push notifications when they are received.

To implement it follow the steps:

  1. Create an Objective-C wrapper for MobileMessaging SDK methods to be able to call them from the Objective-C++ code which is AppDelegate.mm:
//
//  MobileMessagingCustomWrapper.h
//
 
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
 
NS_ASSUME_NONNULL_BEGIN
 
@interface MobileMessagingCustomWrapper : NSObject
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
+ (void)didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
+ (BOOL)isMessagePayloadCorrect:(NSDictionary *)payload;
@end
 
NS_ASSUME_NONNULL_END
//
//  MobileMessagingCustomWrapper.m
//
 
#import "MobileMessagingCustomWrapper.h"
@import MobileMessaging;
 
@implementation MobileMessagingCustomWrapper
 
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [MobileMessaging didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
 
+ (void)didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  [MobileMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
 
+ (BOOL)isMessagePayloadCorrect:(NSDictionary *)payload {
  return [MM_MTMessage isCorrectPayload:payload];
}
 
@end
  1. Replace [MobileMessagingPluginApplicationDelegate install] in the AppDelegate.mm:
#import "AppDelegate.h"
...
 
// Not needed any more
//#import <MobileMessaging/MobileMessagingPluginApplicationDelegate.h>
 
 
// Added the import of the wrapper's header
#import "MobileMessagingCustomWrapper.h"
 
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   
  // Not needed if token and received message provided to the MobileMessaging SDK via calling MobileMessagingCustomWrapper methods
  // [MobileMessagingPluginApplicationDelegate install];
 
  ...
  return YES;
}
 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [MobileMessagingCustomWrapper didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  if ([MobileMessagingCustomWrapper isMessagePayloadCorrect:userInfo]) {
    [MobileMessagingCustomWrapper didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
  } else {
    completionHandler(UIBackgroundFetchResultNoData);
  }
}
 
@end