FCM 通知在 iOS 应用程序中不起作用

2024-05-26

我正在我的应用程序中集成 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 IOS 推送通知不起作用 https://stackoverflow.com/questions/37830621/firebase-ios-push-notification-doest-not-work-when-app-is-closed/38093488#38093488

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FCM 通知在 iOS 应用程序中不起作用 的相关文章

随机推荐

  • 如何显示证书的主题备用名称?

    我发现的最接近的答案是使用 grep gt openssl x509 text noout in cert pem grep DNS 有更好的方法吗 我只喜欢命令行 Thanks 较新版本的 openssl 有一个 ext 选项 允许您仅打
  • 从“Google 我的商家”获取 PHP 格式的营业时间

    我需要一些 PHP 代码来从我的 google 我的商家列表中提取我的营业时间 这在我的本地计算机上有效 但当我实时推送网站时不起作用 此代码停止我的 css 渲染并且不加载任何其他内容
  • php - 当存在 CDATA 时,将 xml 转换为 json 不起作用

    如果我使用以下php代码来转换xml to json I get Company fcsf Details n fgrtgrthtyfgvb n 但是 如果我使用CDATA in the Details元素如下
  • window.top.document.body.scrollTop 在 Chrome 或 FireFox 中不起作用

    我有下面的代码将打开一个模式窗口 这适用于 IE 8 但不适用于 Chrome 或 FF 我是跨浏览器功能领域的新手 function ShowModal WindowID FramesetID window onscroll functi
  • 如何使用带有“transfer-encoding: chunked”的 winhttp api

    我正在尝试将一些数据发送到需要 传输编码 分块 标头的网络服务 它可以很好地处理普通的 POST 请求 但一旦我添加标题 我总是会得到 由于以下情况 内容无法交付 收到客户端的无效请求 这是发送请求的部分 std vector
  • 不同保护条件下的状态转换

    在状态模式中这是如何建模的 当当前状态为 A 时 在触发器 X 和条件 C1 上状态 A 到状态 B 当当前状态为 A 时 在触发器 X 和条件 C2 上状态 A 到状态 C 这通常是如何实现的 我有很多可能需要实施的守卫条件 这是相当标准
  • Android Google Cloud Messaging (GCM) 和不匹配的发件人 ID

    我正在尝试在我的 Android 应用程序中使用 GCM 服务 为此 我使用了 android 文档http developer android com guide google gcm gcm html http developer an
  • 如何从前端使用 AWS CloudWatch Logs 提交简单日志?

    经过大约 1 小时的搜索 我没有找到任何有关 如何向 AWS CloudWatch Logs 提交简单日志 的信息从前端侧 几乎所有示例都是针对 Node js 的 但我需要从前端提交错误 而不是从后端提交错误 我什至没有找到应该用于前端的
  • 具有自定义集合属性的 JPA 投影

    我们正在使用 Spring Data 并尝试使用子查询创建自定义查询 结果投影有一个数组和其他属性 我们的问题在于子查询数组 public interface ProfesionalRepository extends JpaReposit
  • 安排复杂功能更新

    我在 Apple Watch 上有一个自定义复杂功能 我试图每小时更新一次 它应该每小时 ping 一个 API 端点 如果数据自上次检查以来发生了变化 则应更新复杂性 这是我目前所拥有的 似乎只有一次的效果 当它起作用时 它确实会 pin
  • Mojolicious:我应该使用一个还是多个 websocket?

    我正在自学 Mojolicious 和 websockets 到目前为止 我已经有了一个网页 它显示数据库中的行 并具有用于添加 删除和更新行以及选择用于排序的列的按钮 目前 它在每个按钮的 javascript onclick 处理程序中
  • 运行SpringBootTest时访问H2控制台

    如果我正在运行测试 SpringBootTest有什么办法可以访问H2控制台吗 我有一个访问 H2 数据库 成功 的测试 但如果我想自己检查数据库 我该怎么做 我首先运行测试webEnvironment DEFINED PORT and h
  • 声明与定义

    在 C 中 声明与定义有何不同 即 类声明与类定义 变量声明与定义 方法参数声明与定义 在 C 中 这是相当明显的 但在 C 中 从 ECMA 标准和 MSDN 可以看出 一切都是声明 并且在使用定义一词的地方 它与声明具有相同的含义 在使
  • 什么是 boost::asio::ssl::context::load_verify_file 以及如何使用它?

    有一个谨慎的小量 boost asio ssl http en wikipedia org wiki Transport Layer Security小型 C 在线教育代码库 甚至更少boost asio ssl context load
  • Java FX 8 - Tableview 显示对象内的对象

    我正在尝试加载一个表格视图来自对象列表的组件 在我的例子中是客户端对象 一个客户端对象包含一个Address对象和反之亦然 这Client和Address对象定义如下 public class Client private String f
  • 如何在 SequelizeJS 中创建模型时设置额外属性?

    我的模型 Recipe id name Ingredient id name Recipe Ingredient recipeId ingredientId quantity 我的协会 Recipe belongsToMany Ingred
  • 不使用 Django 的 Python 数据库(适用于 Heroku)

    令我惊讶的是 我没有发现其他地方提出过这个问题 简而言之 我正在编写一个应用程序 计划部署到云 可能使用 Heroku 它将执行各种网络抓取和数据收集 它将位于云中的原因是 我可以将其设置为每天自行运行 并将数据提取到数据库 而无需我的计算
  • 提交表单后如何关闭浏览器选项卡?

  • 如何在 Haskell 中使 CAF 不是 CAF?

    如何将常量应用形式变成 而不是常量应用形式 以阻止它在程序的生命周期中保留 我尝试过这种方法 Dummy parameter to avoid creating a CAF twoTrues gt Bool twoTrues map Tru
  • FCM 通知在 iOS 应用程序中不起作用

    我正在我的应用程序中集成 FCM 通知和云消息传递 我已按照 Firebase 文档中提到的完全相同的步骤进行操作 即使我已经尝试过 FCM 给出的示例代码 它只是发出一些警告