ios - FCM 成功,但设备未显示通知 iOS

标签 ios iphone firebase notifications firebase-cloud-messaging

我已经构建了我的应用来接收 Firebase 通知。我通过 FCM 控制台对其进行了测试,设备收到了通知。但是,当我从 testflight 设备进行测试时,没有收到任何通知,但当我检查有效负载时,它显示成功。

1. Uploaded the production .p12 certificate in firebase.
2. Capabilities -> Background Modes = ON with Remote Notifications = true; 
   PushNotifications = set to 'true'
3. In Info.plist FirebaseAppDelegateProxyEnabled = YES , 
   FirebaseScreenReportingEnable = NO

这是我的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  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];
            [FIRApp configure];
            [self connectToFcm];
        [FIRMessaging messaging].delegate = self;

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];

}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
        [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeSandbox];
  [FIRMessaging messaging].APNSToken = deviceToken;


}
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    self.DeviceId=fcmToken;

    // Notify about received token.
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:
     @"FCMToken" object:nil userInfo:dataDict];

}
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {

    NSLog(@"%@", remoteMessage.appData);

}
- (void)tokenRefreshNotification:(NSNotification *)notification {


    [[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                        NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error fetching remote instance ID: %@", error);
        } else {
            NSString *refreshedToken = result.token;
            self.DeviceId = refreshedToken;
            NSLog(@"InstanceID token: %@", refreshedToken);
            NSLog(@"Remote instance ID token: %@", result.token);
        }
    }];


}
- (void)connectToFcm {
    [[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                        NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error fetching remote instance ID: %@", error);
        } else {
            NSLog(@"Remote instance ID token: %@", result.token);
        }
    }];
}

通过restclient测试并得到这个结果

{
  "multicast_id": 4659854677338425000,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1569222922386579%12eeef7ecccfb49c"
    }
  ]
}

为什么我的设备即使成功也没有显示通知?

任何想法/帮助将不胜感激。

最佳答案

我在同一个问题上花了很多时间。然后我发现当我使用 fcm 通知控制台发送测试消息时,我在通知中心的 iphone 上收到了通知。但是在我的 php 应用程序服务器中我从来没有得到它。我总是得到安卓。然后我读了Postman Send FCM我得到了与 fcm 控制台相同的结果。然后我发现问题是我的代码正在使用:

$fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

然后我将其更改为匹配控制台和 postman 并且成功了!

    $fields = 
           [
  'to' => 'TOKEN',
  'notification' => [
    'title' => "It Works",
    'body' => "12 hours later"
  ]
];

事实证明,每当我使用这个自定义数组时,它都无法通过 IOS,但可以通过 Android。现在在 android 上,消息进入应用程序的通知中心,而不是在我的应用程序消息中心内。希望这对您也有帮助。

关于ios - FCM 成功,但设备未显示通知 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058293/

相关文章:

iphone - 可以向下滑动UITabBarController吗?

ios - 有些单元格不会显示

IOS - 限制用户最多选择 8 个图像并使用 For 循环将多个图像上传到 Firebase

ios - 扩展不会 swift 进入派生类

ios - 如何将自定义项目添加到导航栏

ios - 为什么 NSKeyedArchiver 和 NSKeyedUnarchiver 在这里没有按预期工作?

firebase - 通过 REST API 取消订阅 Firebase 云消息传递客户端

objective-c - UIWebView 使用本地镜像和 javascript 文件加载远程 html 页面

iphone - 将 TTTableViewController(来自 Three20)添加到另一个 UIViewController

iphone - 我的 iOS 应用程序在 iOS 5 上运行非常慢,这可能是什么原因?