react-native-notifications
react-native-notifications copied to clipboard
RN 0.77 Support (swift)
Support for RN 0.77 and documentation to support with AppDelegate.swift 🙏
Here's what I got
- Make sure you have a Bridging Header and add this line:
#import <RNNotifications.h>
- 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 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 I updated my comment above, should now include everything needed.
thanx @bananer , but thats not my problem, i doent see this library in the pod folder, something went wrong with the auto link
Here's what I got
- Make sure you have a Bridging Header and add this line:
#import <RNNotifications.h>
- Modify AppDelegate.swift
Inside
override func application(…)before thesupercall, 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?
Can i ask you to share you Bridging Header with us?
It contains just the single import line
@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)
}
@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
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)
}
}
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
}
}
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
- 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 if you're importing it in the bridging header, you shouldn't also import it in the Swift file.
@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 RNNotificationsfromAppDelegate.swift.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
The issue has been closed for inactivity.