firebase_messaging :没有通知通过

Posted

技术标签:

【中文标题】firebase_messaging :没有通知通过【英文标题】:firebase_messaging : no notifications coming through 【发布时间】:2019-12-29 16:18:33 【问题描述】:

解释问题:

我现在已经尝试让 firebase_messaging 工作了将近一周。 我成功设置了一个旧版 Xcode APNS 应用程序,该应用程序在生成所有新证书等后工作。 但是使用 firebase_messaging 我根本没有收到任何通知。 我什至重新创建了一个新的颤振、firebase 和 appstoreconnect 项目/应用程序。 但是我根本没有收到来自 firebase 的任何通知,无论是在本机 Xcode 中实现还是在颤振中实现。 如果我订阅了一个尚不存在的主题,它正在被创建; android 和 Analytics 上的通知以及 ios 和 Android 上的 InAppMessaging 工作正常,所以肯定有一些工作正常。

到目前为止我所做的尝试:

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

编码 sn-ps 以重新创建配置:

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


实施的插件(所有最新版本):

firebase_messaging firebase_core firebase_analytics

颤振/飞镖(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/Messaging' 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)不返回任何错误;调试日志中也没有错误。该应用程序报告运行正常,但消息没有通过。

有人可以帮我解决这个问题吗?谢谢!

【问题讨论】:

请阅读How to Ask,然后阅读edit您的问题。不要发布您尝试过的链接;没有人会看他们。取而代之的是edit你的问题,并在你的问题中描述你做了什么和发生了什么。 (此外,链接可能会中断,使您的问题毫无意义。)将您的代码发布为minimal reproducible example。运行时会发生什么?你期望会发生什么?有什么错误吗? 格式化你的答案。 @Robert 我希望这已经足够好了。 【参考方案1】:

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

【讨论】:

我收到了Received message missing local start time, dropped,无法找出原因,删除了 Firebase 控制台中的 APNS .p8 密钥并将 .p12 文件恢复正常...谢谢@klydra

以上是关于firebase_messaging :没有通知通过的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 firebase_messaging 在 iOS 上的 Flutter 应用中订阅主题

Flutter firebase_message 插件设置错误

UITableView 不会更新数据

Flutter 推送通知应用背景:firebase_messaging

firebase_messaging 如何清除通知?

firebase_messaging:^4.0.0+4 不工作