ios - 几周后 Firebase 推送通知不起作用

标签 ios objective-c firebase push-notification firebase-cloud-messaging

我在我的应用中使用 Firebase 云消息传递。它在几周内运行良好,但最近两天,它没有。

我从 Firebase 控制台发送消息。我处理 token 刷新。可能是什么问题?

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [FIRApp configure];
   [self registerForPush];
}

这是我注册推送通知的地方:

-(void)registerForPush
{
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
    UNAuthorizationOptions authOptions =
    UNAuthorizationOptionAlert
    | UNAuthorizationOptionSound
    | UNAuthorizationOptionBadge;

    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error)
    {
        if (error)
        {
            NSLog(@"\n\n %@ \n\n",error.description);
        }

        NSLog(@"");

    }];

    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    // For iOS 10 data message (sent via FCM)
    [FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}


[[UIApplication sharedApplication] registerForRemoteNotifications];
}

是否使用设备 token 注册了远程通知:

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[FIRInstanceID instanceID] token];
    NSLog(@"%@", token);

   // Add observer to listen for the token refresh notification.
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTokenRefresh) name:kFIRInstanceIDTokenRefreshNotification object:nil];

    if(token)
    {
      [self subscribeToTopics];
    }
}

这是 firebase 刷新 token 时的观察者:

- (void)onTokenRefresh
{
   // Get the default token if the earlier default token was nil. If the we already
   // had a default token most likely this will be nil too. But that is OK we just
   // wait for another notification of this type.
   NSString *token = [[FIRInstanceID instanceID] token];

   if (token)
   {
       [self subscribeToTopics];
   }
}

这是我订阅的 firebase 主题:

-(void)subscribeToTopics
{
   [[FIRMessaging messaging] subscribeToTopic:@"/topics/ios"];
   [[FIRMessaging messaging] subscribeToTopic:@"/topics/all"];

   #ifdef DEBUG
   [[FIRMessaging messaging] subscribeToTopic:@"/topics/developer_devices"];
   #else
   [[FIRMessaging messaging]   unsubscribeFromTopic:@"/topics/developer_devices"];
   #endif
}

最佳答案

您的 APNs 证书可能由于某种原因被吊销了。尝试生成一个新的并在 firebase 控制台中重新上传它!

https://firebase.google.com/docs/notifications/ios/console-audience#upload_your_apns_certificate

关于ios - 几周后 Firebase 推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42109208/

相关文章:

android - 检查 key 是否存在 firebase Android

java - Firestore 回收器适配器未获取文档名称

ios - FairPlay 流媒体 : Calling copyPixelBufferForItemTime on AVPlayerItemVideoOutput returns NULL

ios - NSDate显示错误的一天

ios - 如何更改 textView 中预选单词的样式?

ios - iOS 9.1 中的希伯来语文本没有从右到左显示?

firebase - Next/Image + Firebase Storage 403 读取错误

ios - 仅在顶部或底部具有圆角半径的 UITextField

ios - PageViewController 之前和之后调用两次并且索引不计为真

objective-c - 防止 AlertView 解雇