【发布时间】:2016-08-30 12:14:11
【问题描述】:
我正在尝试使用Firebase 进行推送通知。我已经通过Cocoapods成功安装了Firebase和Firebase messaging。
我使用了下面的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshCallback:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
UIUserNotificationType allNotificationsType = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationsType categories:nil];
[application registerUserNotificationSettings:settings];
[application isRegisteredForRemoteNotifications];
return YES;
}
-(void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
NSLog(@"Disconnect from FCM");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self connectToFirebase];
}
- (void) application:(UIApplication *) application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler{
// Pring The Message Id
NSLog(@"Mesage Id : %@",userInfo[@"gcm.message_id"]);
// Print Full Message
NSLog(@"%@",userInfo);
}
#pragma mark -- Custom Firebase code
- (void)tokenRefreshCallback:(NSNotification *) notification{
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"IstanceID token: %@", refreshedToken);
// Connect To FCM since connection may have failed when attempt before having a token
[self connectToFirebase];
}
-(void) connectToFirebase{
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error)
{
if ( error != nil)
{
NSLog(@"Unable to Connect To FCM. %@",error);
}
else
{
NSLog((@"Connected To FCM"));
}
}];
}
当我使用与Xcode 连接的 iPhone 运行上述代码时,当我从 Firebase 控制台发送消息时遇到以下问题
1)。当应用程序处于前台(活动状态)时,Log 显示以下消息
Mesage Id : (null){
CustommData = "First Message";
"collapse_key" = "abcd.iospush.FireBasePush";
from = 555551391562;
notification = {
badge = 4;
body = "Test Message";
e = 1;
sound = default;
sound2 = default;
};
}
注意Message Id 是null。
2)。我的手机在通知中心中没有显示任何通知,无论应用程序处于前台、后台还是关闭状态
我希望用户在应用处于后台、前台或关闭时接收推送通知
【问题讨论】:
-
您检查过您的 APN 证书吗?您使用来自同一证书的 APN 收到通知?
标签: ios objective-c firebase apple-push-notifications firebase-cloud-messaging