Firebase 推送通知在几周后不起作用
Posted
技术标签:
【中文标题】Firebase 推送通知在几周后不起作用【英文标题】:Firebase push notification doesn't work after a few weeks 【发布时间】:2017-02-08 09:26:18 【问题描述】:我在我的应用中使用 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 刷新令牌时的观察者:
- (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】:您的 APNs 证书可能由于某种原因已被吊销。尝试生成一个新的并在 firebase 控制台中重新上传!
https://firebase.google.com/docs/notifications/ios/console-audience#upload_your_apns_certificate
【讨论】:
以上是关于Firebase 推送通知在几周后不起作用的主要内容,如果未能解决你的问题,请参考以下文章