I am trying to use Firebase
for push notification. i have successfully installed the Firebase
And Firebase messaging
via Cocoapods
.
I used the below code
- (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"));
}
}];
}
When i run the above code using my iPhone connected with Xcode
i am facing the following issues when i send a message from Firebase Console
1). when the app is in foreground (active), Log shows the below message
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;
};
}
Notice Message Id
is null
.
2). My phone doesn't show any notification in Notification Centre whether App is in Foreground , Background or Closed
I want user to receive push notifications when App is in Background , Foreground or Closed