gpt4 book ai didi

ios - 在检索发件人 ID 的 FCM token 之前未设置 APNS 设备 token - React Native Firebase

转载 作者:行者123 更新时间:2023-11-29 13:53:25 28 4
gpt4 key购买 nike

我一直在关注this使用 react-native-firebase 版本 5.2.0 在我的 react-native 应用程序上设置远程推送通知的教程。在我配置好一切并运行应用程序后,我得到一个错误:

APNS device token not set before retrieving FCM Token for Sender ID ''. Notifications to this FCM Token will not be delievered over APNS. Be sure to re-retrieve the FCM token once the APNS token is set.

一直在想办法解决这个问题,但不太成功。在 react-native 上运行:0.61.2,react-native-firebase:5.2.0

以下是我的AppDelegate.m

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <RNCPushNotificationIOS.h>
#import <UserNotifications/UserNotifications.h>
#import <Firebase.h>

@import Firebase;
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FIRApp configure];
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"helloworld"
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 to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// 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 the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog(@"User Info : %@",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}


@end

和我的 App.js 上的 token 检索:

const messaging = firebase.messaging();

messaging.hasPermission()
.then((enabled) => {
if (enabled) {
messaging.getToken()
.then(token => { console.log("+++++ TOKEN ++++++" + token) })
.catch(error => { console.log(" +++++ ERROR GT +++++ " + error) })
} else {
messaging.requestPermission()
.then(() => { console.log("+++ PERMISSION REQUESTED +++++") })
.catch(error => { console.log(" +++++ ERROR RP ++++ " + error) })
}

})
.catch(error => { console.log(" +++++ ERROR +++++ " + error) });

任何帮助将不胜感激!谢谢!

最佳答案

我在 3 天内解决了这个问题。您需要将 p8 key 连接到您的 firebase

创建 p8 key 的链接 https://stackoverflow.com/a/67533665/13033024

enter image description here

将以下代码添加到您的 AppDelegate.swift

 if #available(iOS 10.0, *) { 
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

application.registerForRemoteNotifications()

转到 xCode 并单击“功能”选项卡。添加后台模式和推送通知。在后台模式选项卡中,启用后台获取和远程通知

不要忘记为发布、调试、配置文件添加它

enter image description here

从手机中删除您的应用并再次运行。

希望这对你也有帮助!

关于ios - 在检索发件人 ID 的 FCM token 之前未设置 APNS 设备 token - React Native Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58246620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com