几周后,Firebase推送通知无效

时间:2017-02-08 09:26:18

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

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

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

这是我的代码:

- (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];
}

使用设备令牌注册远程通知:

- (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刷新令牌时的Observer:

- (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
}

1 个答案:

答案 0 :(得分:1)

您的APN证书可能因某些原因被撤销。尝试生成一个新的并在firebase控制台中重新上传它!

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