【问题标题】:Unable to receive pushNotifications from Firebase connected through application server无法从通过应用服务器连接的 Firebase 接收 pushNotifications
【发布时间】:2017-08-17 19:25:20
【问题描述】:

我在 firebase 控制台中为苹果 APNs pushNotifications 创建了一个新项目。我遵循了 firebase 文档中的所有说明,例如生成 Ssl 证书并将 p12 证书(开发和生产)上传到 firebase 以及配置文件。并且还在 Appdelegate 中添加了所需的全部代码。并且还在功能和授权文件 APNs 环境中激活了 pushNotifications 设置为开发。

当我从 firebase 通知消息发送示例消息时,我的 iPhone(6) 能够接收通知(通过 FCM 令牌发送,并且 bundelId 也能够接收通知)。但是从我的具有相同 FCM 令牌的应用程序服务器,我无法接收通知,但我的服务器端收到回复,因为通知发送了 Ok,来自 firebase 的 200 代码。

我无法从上周获得解决方案,也无法从移动端或服务器端获取问题所在。

在此先感谢....

【问题讨论】:

  • 你配置完成了吗?
  • 我在控制台日志中显示为 无法获取 APNS 令牌错误 Domain=com.firebase.iid Code=1001 "(null)"

标签: ios firebase apple-push-notifications firebase-cloud-messaging


【解决方案1】:

我找到了解决问题的方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
kGCMMessageIDKey = @"gcm.message_id";
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
    // iOS 7.1 or earlier. Disable the deprecation warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    UIRemoteNotificationType allNotificationTypes =
    (UIRemoteNotificationTypeSound |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeBadge);
    [application registerForRemoteNotificationTypes:allNotificationTypes];
#pragma clang diagnostic pop
} else {
    // iOS 8 or later
    // [START register_for_notifications]
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
        // iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
        // 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) {
        }];

        // For iOS 10 data message (sent via FCM)
        [FIRMessaging messaging].remoteMessageDelegate = self;
#endif
    }

    [[UIApplication sharedApplication] registerForRemoteNotifications];
    // [END register_for_notifications]
}

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

[FIRApp configure];

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

在我上面的代码中我没有写

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

这一行,实际上不在 firebase 直接文档中。

并检查是否可以使用此方法获得将在控制台中打印的通知

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"Notification :%@", remoteMessage.appData);
}

但通知屏幕上可能不会出现通知,因为 apns 格式是

{"aps":{"alert":"Testing.. (0)","badge":1,"sound":"default"}}

但从服务器端它可能是不同的格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 2019-10-19
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    相关资源
    最近更新 更多