gpt4 book ai didi

ios - 无法在真实设备上通过 FCM 获取通知

转载 作者:行者123 更新时间:2023-11-29 00:46:47 25 4
gpt4 key购买 nike

我已经在 firebase 上添加了 APNs 证书..但是当我从 FCM 发送通知时,我的设备没有显示任何通知,而且 InstanceID 和 messageID 在输出中给出 null

这是我的应用程序 delegate.m 代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
sleep(3);
SplitsVC*log=[[SplitsVC alloc] initWithNibName:@"SplitsVC" bundle:nil];

self.navVC=[[MyNavigationController alloc] initWithRootViewController:log];

[self.navVC.navigationBar setHidden:YES];
[self.window setRootViewController:self.navVC];
[self.window makeKeyAndVisible];
// Add observer to listen for the token refresh notification.
// Register for remote notifications
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
} else {
// iOS 8 or later
// [START register_for_notifications]
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
// [END register_for_notifications]
}

// [START configure_firebase]
[FIRApp configure];
// [END configure_firebase]

// Add observer for InstanceID token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
return YES;
}

// [START receive_message]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification

// Print message ID.
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

// Pring full message.
NSLog(@"%@", userInfo);
}
// [END receive_message]

// [START refresh_token]
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);

// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];

// TODO: If necessary send token to appliation server.
}
// [END refresh_token]

// [START connect_to_fcm]
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
}
}];
}
// [END connect_to_fcm]

- (void)applicationDidBecomeActive:(UIApplication *)application {
[self connectToFcm];
}

// [START disconnect_from_fcm]
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
NSLog(@"Disconnected from FCM");
}
// [END disconnect_from_fcm]

这是输出- enter image description here

最佳答案

请确保 firebase 上的证书与您在 xcode 中使用的证书相同。也请尝试查看 Firebase 的文档。

关于ios - 无法在真实设备上通过 FCM 获取通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540243/

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