未使用 firebase 和 Objective C 接收推送通知
Not receiving Push Notifications Using firebase and Objective C
我正在尝试使用 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"));
}
}];
}
当我 运行 使用我的 iPhone 与 Xcode
连接的上述代码时,当我从 Firebase 控制台
发送消息时,我面临以下问题
1).当应用程序处于前台(活动)时,日志显示以下消息
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).我的 phone 在 通知中心 中没有显示任何通知,无论 App 处于前台、后台还是关闭状态
我希望用户在应用处于后台、前台或关闭状态时收到推送通知
您必须调用 [[UIApplication sharedApplication] registerForRemoteNotifications];
才能正确注册远程通知。
为后台使用配置远程通知
您是否将应用程序的后台模式配置为接受远程通知?
您可以通过单击:项目名称 -> 功能 -> 后台模式
打开它,然后勾选远程通知旁边的框,如下面的屏幕截图所示。
您应该调用 (iOS 8+) registerForRemoteNotifications
,而不是 isRegisteredForRemoteNotifications
。
否则,您永远不会从 Firebase 服务器获得 "high priority" 消息,使用 APNS 本机传送。
请注意,当您在 XCode 中 运行 时,您处于沙盒模式 运行,因此请务必使用正确的参数进行注册。
尝试在 "high priority" 中发送并查看您的负载是否采用正确的 APNS 格式:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
}
请注意,您的负载可能 =
而不是 :
,这是可以接受的。
您需要在您的开发者帐户中创建一个启用了推送通知授权的 App ID。幸运的是,Xcode 有一种简单的方法可以做到这一点。转到应用程序设置 -> 功能并将推送通知的开关切换到开。
一些加载后,它应该是这样的:
推送通知
同时检查是否为 Firebase 设置了 p12 证书,因为它使用 .p12 证书进行推送通知。
我正在尝试使用 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"));
}
}];
}
当我 运行 使用我的 iPhone 与 Xcode
连接的上述代码时,当我从 Firebase 控制台
1).当应用程序处于前台(活动)时,日志显示以下消息
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).我的 phone 在 通知中心 中没有显示任何通知,无论 App 处于前台、后台还是关闭状态
我希望用户在应用处于后台、前台或关闭状态时收到推送通知
您必须调用 [[UIApplication sharedApplication] registerForRemoteNotifications];
才能正确注册远程通知。
为后台使用配置远程通知
您是否将应用程序的后台模式配置为接受远程通知? 您可以通过单击:项目名称 -> 功能 -> 后台模式
打开它,然后勾选远程通知旁边的框,如下面的屏幕截图所示。
您应该调用 (iOS 8+) registerForRemoteNotifications
,而不是 isRegisteredForRemoteNotifications
。
否则,您永远不会从 Firebase 服务器获得 "high priority" 消息,使用 APNS 本机传送。
请注意,当您在 XCode 中 运行 时,您处于沙盒模式 运行,因此请务必使用正确的参数进行注册。
尝试在 "high priority" 中发送并查看您的负载是否采用正确的 APNS 格式:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
}
请注意,您的负载可能 =
而不是 :
,这是可以接受的。
您需要在您的开发者帐户中创建一个启用了推送通知授权的 App ID。幸运的是,Xcode 有一种简单的方法可以做到这一点。转到应用程序设置 -> 功能并将推送通知的开关切换到开。
一些加载后,它应该是这样的:
推送通知
同时检查是否为 Firebase 设置了 p12 证书,因为它使用 .p12 证书进行推送通知。