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

时间:2019-09-23 08:19:09

标签: 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"
    }
  ]
}

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

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

2 个答案:

答案 0 :(得分:1)

您是否已正确完成此设置“创建身份验证密钥”?

https://firebase.google.com/docs/cloud-messaging/ios/certs

答案 1 :(得分:0)

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

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

然后我将其更改为与控制台和邮递员匹配,并且可以正常工作!:

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

事实证明,每当我使用此自定义数组时,它就永远不会进入IOS,但会进入Android。现在在android上,消息将进入应用的通知中心,而不是进入我的应用消息中心。希望这对您也有帮助。