iOS 无法在 react-native-firebase 中接收通知
Posted
技术标签:
【中文标题】iOS 无法在 react-native-firebase 中接收通知【英文标题】:iOS can't receive notifications in react-native-firebase 【发布时间】:2021-07-09 09:52:56 【问题描述】:我正在使用react-native-firebase
和其他一些库来显示来自 Firebase 的通知。这是我的package.json
的样子。
"dependencies":
...
"react-native-push-notification": "^7.2.3",
"@react-native-community/push-notification-ios": "^1.8.0",
"@react-native-firebase/app": "^11.4.1",
"@react-native-firebase/messaging": "^11.4.1",
...
由于某些原因,我的应用程序在 iOS 设备上根本收不到通知。即使是通过设备令牌发送的来自 Firebase 控制台的测试通知也不起作用。所以我建议我的问题不在react-native
代码中。顺便说一下,通知可以在 android 上正常运行。
在我的 XCode 的“签名和功能”选项卡中,推送通知已启用,后台获取和远程通知后台模式也已启用。
这是我的AppDelegate.m
#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <Firebase.h>
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import <AppCenterReactNative.h>
#import <AppCenterReactNativeAnalytics.h>
#import <AppCenterReactNativeCrashes.h>
#import <UIKit/UIKit.h>
#import <TSBackgroundFetch/TSBackgroundFetch.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([FIRApp defaultApp] == nil)
[FIRApp configure];
[AppCenterReactNative register];
[AppCenterReactNativeAnalytics registerWithInitiallyEnabled:true];
[AppCenterReactNativeCrashes registerWithAutomaticProcessing];
[[TSBackgroundFetch sharedInstance] didFinishLaunching];
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"ModuleName"
initialProperties:nil];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
return YES;
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
// Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
[RNCPushNotificationIOS didReceiveNotificationResponse:response];
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification withCompletionHandler:(void
(^)(UNNotificationPresentationOptions options))completionHandler
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
@end
还有我的info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.transistorsoft.fetch</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>App name</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>app.bundle.identifier</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>app.bundle.name</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>MinimumOSVersion</key>
<string>9.0</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
<key>NSAppleMusicUsageDescription</key>
<string>Allow the app access music.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera. You can make photos to attach to reports or to update your avatar.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app requires access to the user’s location information to search nearby units and buidings.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires access to the user’s location information to search nearby units and buidings.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library. You can select photos to attach to reports or to update your avatar.</string>
<key>UIAppFonts</key>
<array>
<string>Montserrat-Bold.ttf</string>
<string>Montserrat-Light.ttf</string>
<string>Montserrat-Regular.ttf</string>
<string>SFProDisplay-Bold.otf</string>
<string>SFProDisplay-Light.otf</string>
<string>SFProDisplay-Medium.otf</string>
<string>SFProDisplay-Regular.otf</string>
<string>SFProDisplay-Semibold.otf</string>
<string>SFProDisplay-Thin.otf</string>
<string>SFProText -Regular.otf</string>
<string>SFProText-Bold.otf</string>
<string>SFProText-Light.otf</string>
<string>SFProText-Medium.otf</string>
<string>SFProText-Semibold.otf</string>
<string>SFProText-Thin.otf</string>
<string>AntDesign.ttf</string>
<string>Entypo.ttf</string>
<string>EvilIcons.ttf</string>
<string>Feather.ttf</string>
<string>FontAwesome.ttf</string>
<string>FontAwesome5_Brands.ttf</string>
<string>FontAwesome5_Regular.ttf</string>
<string>FontAwesome5_Solid.ttf</string>
<string>Foundation.ttf</string>
<string>Ionicons.ttf</string>
<string>MaterialIcons.ttf</string>
<string>MaterialCommunityIcons.ttf</string>
<string>SimpleLineIcons.ttf</string>
<string>Octicons.ttf</string>
<string>Zocial.ttf</string>
<string>Fontisto.ttf</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
为什么会发生这种情况?
附注Bundle 标识符、应用程序标识符或模块名称被替换为占位符
【问题讨论】:
您是否在 Firebase 控制台中设置了 APN(Apple 推送通知)?请在此处查看。Firebase Console -> Project Settings -> Cloud Messaging (Tab)
您需要在那里设置 APN Auth 密钥。您也可以在物理设备上进行测试。
@ErickMaeda 是的。 APNs 身份验证密钥包含在项目中,但我不确定它是否有效。有没有办法检查我是否无权访问 developer.apple.com?
你在物理设备上测试过吗?
【参考方案1】:
不正确的 APNs 证书存在问题。我已经在 firebase 控制台中对其进行了更改,现在可以使用了! :)
【讨论】:
以上是关于iOS 无法在 react-native-firebase 中接收通知的主要内容,如果未能解决你的问题,请参考以下文章