react-native-notifications icon indicating copy to clipboard operation
react-native-notifications copied to clipboard

RN 0.77 Support (swift)

Open bhandanyan-nomad opened this issue 10 months ago • 10 comments

Support for RN 0.77 and documentation to support with AppDelegate.swift 🙏

bhandanyan-nomad avatar Feb 01 '25 22:02 bhandanyan-nomad

Here's what I got

  1. Make sure you have a Bridging Header and add this line:
#import <RNNotifications.h>
  1. Modify AppDelegate.swift

Inside override func application(…) before the super call, add: RNNotifications.startMonitorNotifications()

Then add these method overrides

  override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
    RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
  }

  override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }

  override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }

bananer avatar Feb 19 '25 13:02 bananer

@bananer how do you import RNNotifications in swift file? when i try i get - No such module 'RNNotifications' and build fails, i tryed to remove and re-add react-native-notifications but the link some how does not add the Lib to ios

DanGDroid avatar Feb 26 '25 08:02 DanGDroid

@DanGDroid I updated my comment above, should now include everything needed.

bananer avatar Feb 26 '25 09:02 bananer

thanx @bananer , but thats not my problem, i doent see this library in the pod folder, something went wrong with the auto link

DanGDroid avatar Feb 26 '25 09:02 DanGDroid

Here's what I got

  1. Make sure you have a Bridging Header and add this line:
#import <RNNotifications.h>
  1. Modify AppDelegate.swift

Inside override func application(…) before the super call, add: RNNotifications.startMonitorNotifications()

Then add these method overrides

override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) { RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken) }

override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error) }

override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler) }

Can i ask you to share you Bridging Header with us?

maksymhcode-care avatar Mar 11 '25 06:03 maksymhcode-care

Can i ask you to share you Bridging Header with us?

It contains just the single import line

bananer avatar Mar 13 '25 18:03 bananer

@bananer Sorry for tagging, just trying to figure out what to do. I get the error "Method does not override any method from its superclass" for this methods

  override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
    RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
  }

  override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }

  override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }

maksymhcode-care avatar May 05 '25 15:05 maksymhcode-care

@bananer Sorry for tagging, just trying to figure out what to do. I get the error "Method does not override any method from its superclass" for this methods

  override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
    RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
  }

  override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }

  override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }

just remove override

vacoo avatar May 08 '25 10:05 vacoo

Works for me.

react-native: 0.79.1

import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import UserNotifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  var window: UIWindow?

  var reactNativeDelegate: ReactNativeDelegate?
  var reactNativeFactory: RCTReactNativeFactory?

  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    let delegate = ReactNativeDelegate()
    let factory = RCTReactNativeFactory(delegate: delegate)
    delegate.dependencyProvider = RCTAppDependencyProvider()

    reactNativeDelegate = delegate
    reactNativeFactory = factory

    window = UIWindow(frame: UIScreen.main.bounds)

    factory.startReactNative(
      withModuleName: "example_app",
      in: window,
      launchOptions: launchOptions
    )
    
    RNNotifications.startMonitorNotifications()

    return true
  }
  
  func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
      RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
   }
  
  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
      RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }
  
  func application(
    _ application: UIApplication,
    didReceiveRemoteNotification userInfo: [AnyHashable: Any],
    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }
}

vacoo avatar May 08 '25 10:05 vacoo

Also figured it out. If you moved to AppDelegate and ReactNativeDelegate classes with 0.79, methods should be under AppDelegate but without override

import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import RNNotifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  
  var reactNativeDelegate: ReactNativeDelegate?
  var reactNativeFactory: RCTReactNativeFactory?
  
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    let delegate = ReactNativeDelegate()
    let factory = RCTReactNativeFactory(delegate: delegate)
    delegate.dependencyProvider = RCTAppDependencyProvider()
    
    reactNativeDelegate = delegate
    reactNativeFactory = factory
    
    window = UIWindow(frame: UIScreen.main.bounds)
    
    factory.startReactNative(
      withModuleName: "reflexMobile",
      in: window,
      launchOptions: launchOptions
    )
    RNNotifications.startMonitorNotifications()
    return true
  }
  
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
    RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
  }
  
  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }
}

 class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
  override func sourceURL(for bridge: RCTBridge) -> URL? {
    self.bundleURL()
  }
   
  override func bundleURL() -> URL? {
#if DEBUG
    RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
#else
    Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
  }

  func application(_ application: UIApplication,continue userActivity: NSUserActivity,restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return RCTLinkingManager.application(application,continue: userActivity,restorationHandler: restorationHandler)
  }

   override func createRootView(with bridge: RCTBridge, moduleName: String, initProps initialProperties: [AnyHashable: Any]) -> UIView {
     let rootView = super.createRootView(with: bridge, moduleName: moduleName, initProps: initialProperties)
     rootView.backgroundColor = UIColor(red: 0.121568627, green: 0.121568627, blue: 0.121568627, alpha: 1.0)
    return rootView
}
}

maksymhcode-care avatar May 08 '25 10:05 maksymhcode-care

I followed the steps below, but I'm still getting the error error no such module 'RNNotifications':

Completely replaced AppDelegate.swift as instructed

Added ios/RN-Bridging-Header.h

Set Defines Module to Yes

Set Objective-C Bridging Header to RN-Bridging-Header.h

Image Image
  • ios/RN-Bridging-Header.h
#ifndef RN_Bridging_Header_h
#define RN_Bridging_Header_h

#import <RNNotifications.h>

#endif
  • AppDelegate.swift
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import RNNotifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  
  var reactNativeDelegate: ReactNativeDelegate?
  var reactNativeFactory: RCTReactNativeFactory?
  
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    let delegate = ReactNativeDelegate()
    let factory = RCTReactNativeFactory(delegate: delegate)
    delegate.dependencyProvider = RCTAppDependencyProvider()
    
    reactNativeDelegate = delegate
    reactNativeFactory = factory
    
    window = UIWindow(frame: UIScreen.main.bounds)
    
    factory.startReactNative(
      withModuleName: "reflexMobile",
      in: window,
      launchOptions: launchOptions
    )
    RNNotifications.startMonitorNotifications()
    return true
  }
  
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
    RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
  }
  
  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }
}

 class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
  override func sourceURL(for bridge: RCTBridge) -> URL? {
    self.bundleURL()
  }
   
  override func bundleURL() -> URL? {
#if DEBUG
    RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
#else
    Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
  }

  func application(_ application: UIApplication,continue userActivity: NSUserActivity,restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return RCTLinkingManager.application(application,continue: userActivity,restorationHandler: restorationHandler)
  }

   override func createRootView(with bridge: RCTBridge, moduleName: String, initProps initialProperties: [AnyHashable: Any]) -> UIView {
     let rootView = super.createRootView(with: bridge, moduleName: moduleName, initProps: initialProperties)
     rootView.backgroundColor = UIColor(red: 0.121568627, green: 0.121568627, blue: 0.121568627, alpha: 1.0)
    return rootView
}
}

Also figured it out. If you moved to AppDelegate and ReactNativeDelegate classes with 0.79, methods should be under AppDelegate but without override

import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import RNNotifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  
  var reactNativeDelegate: ReactNativeDelegate?
  var reactNativeFactory: RCTReactNativeFactory?
  
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    let delegate = ReactNativeDelegate()
    let factory = RCTReactNativeFactory(delegate: delegate)
    delegate.dependencyProvider = RCTAppDependencyProvider()
    
    reactNativeDelegate = delegate
    reactNativeFactory = factory
    
    window = UIWindow(frame: UIScreen.main.bounds)
    
    factory.startReactNative(
      withModuleName: "reflexMobile",
      in: window,
      launchOptions: launchOptions
    )
    RNNotifications.startMonitorNotifications()
    return true
  }
  
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken withDeviceToken: Data) {
    RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: withDeviceToken)
  }
  
  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    RNNotifications.didFailToRegisterForRemoteNotificationsWithError(error)
  }
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RNNotifications.didReceiveBackgroundNotification(userInfo, withCompletionHandler: completionHandler)
  }
}

 class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
  override func sourceURL(for bridge: RCTBridge) -> URL? {
    self.bundleURL()
  }
   
  override func bundleURL() -> URL? {
#if DEBUG
    RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
#else
    Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
  }

  func application(_ application: UIApplication,continue userActivity: NSUserActivity,restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return RCTLinkingManager.application(application,continue: userActivity,restorationHandler: restorationHandler)
  }

   override func createRootView(with bridge: RCTBridge, moduleName: String, initProps initialProperties: [AnyHashable: Any]) -> UIView {
     let rootView = super.createRootView(with: bridge, moduleName: moduleName, initProps: initialProperties)
     rootView.backgroundColor = UIColor(red: 0.121568627, green: 0.121568627, blue: 0.121568627, alpha: 1.0)
    return rootView
}
}

ReySun avatar Jun 06 '25 12:06 ReySun

@ReySun if you're importing it in the bridging header, you shouldn't also import it in the Swift file.

tamlyn avatar Jun 06 '25 14:06 tamlyn

@ReySun if you're importing it in the bridging header, you shouldn't also import it in the Swift file.

I’m not a native iOS/Android developer and lack experience, this truly solved my problem, Thank you very much!

I removed import RNNotifications from AppDelegate.swift.

ReySun avatar Jun 06 '25 15:06 ReySun

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Jul 18 '25 18:07 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Jul 29 '25 01:07 stale[bot]