首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FCM提示成功,但设备未显示通知iOS

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

Stack Overflow用户
提问于 2019-09-23 16:19:10
回答 2查看 1.8K关注 0票数 1

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

代码语言:javascript
复制
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

以下是我的代码

代码语言:javascript
复制
- (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进行了测试,得到了如下结果

代码语言:javascript
复制
{
  "multicast_id": 4659854677338425000,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1569222922386579%12eeef7ecccfb49c"
    }
  ]
}

为什么我的设备在成功后仍不显示通知?

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

EN

回答 2

Stack Overflow用户

发布于 2019-09-23 16:51:33

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

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

票数 1
EN

Stack Overflow用户

发布于 2019-11-30 02:48:35

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

代码语言:javascript
复制
$fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

然后我将其更改为匹配控制台和邮递员,它起作用了!:

代码语言:javascript
复制
    $fields = 
           [
  'to' => 'TOKEN',
  'notification' => [
    'title' => "It Works",
    'body' => "12 hours later"
  ]
];

我发现,每当我使用这个自定义数组时,它都无法到达IOS,但却到达了Android。此外,现在在android上,消息进入应用程序的通知中心,而不是我的应用程序消息中心。希望这对你也有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58058293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档