【发布时间】:2016-05-31 06:28:00
【问题描述】:
我正在我的应用中集成 FCM 通知和云消息传递。我遵循了 Firebase 文档中提到的完全相同的步骤。即使我尝试过 FCM 给出的示例代码。它只是抛出一些警告:
<FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "The operation couldn’t be completed. and <FIRMessaging/WARNING> FIRMessaging registration is not ready with auth credentials.
我在 Appdelegate.m 中编写的代码是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 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
// [END_EXCLUDE]
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
// [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]
【问题讨论】:
-
你是如何发送消息的?您是否使用 Firebase 控制台并针对所有设备?您是否添加了 GoogleService-Info.plist?您也可以尝试实现 application:didFailToRegisterForRemoteNotificationsWithError 了解更多信息
-
我正在通过 firebase 控制台(通知)发送消息,并且我添加了 googleService-Info.plist。 didFailtoRegister 没有被调用。我正在获取设备令牌,但设备上没有通知
-
是的,我的目标是单个设备,并将 FCM 注册令牌放在必填字段中。在 firebase 控制台中,消息状态已完成,但设备上没有推送通知。我已经检查了这两种状态,即在后台和前台都有应用程序。
-
代码看起来正确。当您的应用程序处于活动状态时,您应该会看到 Connected to FCM,如果您重新安装应用程序,您应该会获得最新的令牌,但这并不重要,因为您可以从 firebase 控制台定位所有应用程序用户。我一定会仔细检查您的配置文件和证书是否设置正确:firebase.google.com/docs/cloud-messaging/ios/… 我无法真正发布答案,因为我也在设置它,我很幸运在职的。也没有太多可用的调试信息:(
-
它正在发送消息“已连接到 FCM”,但没有通知,我遵循了相同的文档 :(
标签: ios objective-c