gpt4 book ai didi

xcode - firebase_messaging : no notifications coming through

转载 作者:行者123 更新时间:2023-12-03 04:28:15 28 4
gpt4 key购买 nike

解释问题:
我现在已经尝试让 firebase_messaging 工作近一周。
我成功设置了一个旧版 Xcode APNS 应用程序,该应用程序在生成所有新证书等后工作。
但是使用 firebase_messaging 我根本没有收到任何通知。
我什至重新创建了一个新的 flutter 、firebase 和 appstoreconnect 项目/应用程序。
但是我根本没有收到来自 firebase 的任何通知,无论是在 native Xcode 中实现还是在 flutter 中实现。
如果我订阅了一个尚不存在的主题,它正在被创建; Android 和 Analytics 上的通知以及 iOS 和 Android 上的 InAppMessaging 运行良好,所以肯定有一些东西在运行。
到目前为止我尝试过的:

  • 原生 Xcode 和 Flutter/Dart 上 firebase_messaging 的官方示例项目
  • 重新创建所有许可证/项目
  • 按主题发送消息(我本来打算这样做)
  • 从 GitHub/stackoverflow 应用补丁/修复/提示。

  • 重新创建配置的代码片段:
    重新创建的步骤可能只是下载 Firebase Messaging 示例项目或遵循为原生 Xcode 或 Flutter 设置 Firebase Messaging 的官方文档。重新创建此处列出的示例的基本步骤:

    实现的插件(所有最新版本):
  • firebase_messaging
  • firebase_core
  • firebase_analytics

  • flutter/Dart (main.dart):
    void firebaseCloudMessaging_Listeners() {
    if (Platform.isIOS) iOS_Permission();

    firebaseMessaging.getToken().then((token){
    print(token);
    });

    firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
    print('on message $message');
    },
    onResume: (Map<String, dynamic> message) async {
    print('on resume $message');
    },
    onLaunch: (Map<String, dynamic> message) async {
    print('on launch $message');
    },
    );
    }

    void iOS_Permission() {
    firebaseMessaging.requestNotificationPermissions(
    IosNotificationSettings(sound: true, badge: true, alert: true)
    );
    firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings settings)
    {
    print("Settings registered: $settings");
    });
    }

    void subscribeToFB() {
    firebaseMessaging.subscribeToTopic('-------------Sandbox');
    print("--- SUBSCRIBED ---");
    }

    void unsubscribeFromFB() {
    firebaseMessaging.unsubscribeFromTopic('-------------Sandbox');
    print("--- UNSUBSCRIBED ---");
    }

    已实现的 Pod(所有最新版本):
  • pod 'Firebase/消息'
  • pod 'Firebase/Analytics'

  • 原生 Xcode (AppDelegate.h):
    #import "AppDelegate.h"

    @import UserNotifications;

    @interface AppDelegate () <UNUserNotificationCenterDelegate>
    @end

    @implementation AppDelegate

    NSString *const kGCMMessageIDKey = @"gcm.message_id";

    - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // [START configure_firebase]
    [FIRApp configure];
    // [END configure_firebase]

    // [START set_messaging_delegate]
    [FIRMessaging messaging].delegate = self;
    // [END set_messaging_delegate]

    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if ([UNUserNotificationCenter class] != nil) {
    // iOS 10 or later
    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
    UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter]
    requestAuthorizationWithOptions:authOptions
    completionHandler:^(BOOL granted, NSError * _Nullable error) {
    // ...
    }];
    } else {
    // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
    }

    [application registerForRemoteNotifications];
    // [END register_for_notifications]

    return YES;
    }

    // [START receive_message]
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // 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

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

    // Print message ID.
    if (userInfo[kGCMMessageIDKey]) {
    NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }

    // Print full message.
    NSLog(@"%@", userInfo);
    }

    - (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

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

    // Print message ID.
    if (userInfo[kGCMMessageIDKey]) {
    NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }

    // Print full message.
    NSLog(@"%@", userInfo);

    completionHandler(UIBackgroundFetchResultNewData);
    }
    // [END receive_message]

    // [START ios_10_message_handling]
    // Receive displayed notifications for iOS 10 devices.
    // Handle incoming notification messages while app is in the foreground.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
    willPresentNotification:(UNNotification *)notification
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

    // Print message ID.
    if (userInfo[kGCMMessageIDKey]) {
    NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }

    // Print full message.
    NSLog(@"%@", userInfo);

    // Change this to your preferred presentation option
    completionHandler(UNNotificationPresentationOptionNone);
    }

    // Handle notification messages after display notification is tapped by the user.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
    didReceiveNotificationResponse:(UNNotificationResponse *)response
    withCompletionHandler:(void(^)(void))completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    if (userInfo[kGCMMessageIDKey]) {
    NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }

    // Print full message.
    NSLog(@"%@", userInfo);

    completionHandler();
    }

    // [END ios_10_message_handling]

    // [START refresh_token]
    - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
    // Notify about received token.
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:
    @"FCMToken" object:nil userInfo:dataDict];
    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
    }
    // [END refresh_token]

    // [START ios_10_data_message]
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES.
    - (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
    NSLog(@"Received data message: %@", remoteMessage.appData);
    }
    // [END ios_10_data_message]

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Unable to register for remote notifications: %@", error);
    }

    // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
    // If swizzling is disabled then this function must be implemented so that the APNs device token can be paired to
    // the FCM registration token.
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"APNs device token retrieved: %@", deviceToken);

    // With swizzling disabled you must set the APNs device token here.
    // [FIRMessaging messaging].APNSToken = deviceToken;
    }
    @end

    IDE(Xcode/VS Code @ macOS HS)不返回任何错误;调试日志中也没有错误。该应用程序报告工作正常,但消息没有通过。
    有人可以帮我吗?谢谢!

    最佳答案

    解决方法是删除建议的 .p8 key 文件(从 Firebase 控制台 > 项目设置 > 云消息传递),然后放入 APNS 开发和发布证书。

    关于xcode - firebase_messaging : no notifications coming through,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57641550/

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