0
votes

Recently google announce the new Firebase SDK with lot more feature. So, I'm looking for the perfect documentation of How to implement Remote Notification feature using new Firebase Messaging SDK(FCM) in iOS.

Thanks a lot in advance.

1
Why you are not do some google before ans question. Firebase provide step by step guide spend some time to learn and read it from : firebase.google.com/docs/cloud-messagingNitin Gohel
@NitinGohel I have already done with that, but It's not work for me. Do have any your own sample project?Sagar Unagar
@NitinGohel my app is crashing on following functions first line, - (void)connectToFcm { [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Unable to connect to FCM. %@", error); } else { NSLog(@"Connected to FCM."); } }]; } Error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSData gtm_dataByGzippingData:]: unrecognized selector sent to class 0x10fd1f110' Do you have any suggestion?Sagar Unagar
Can you post the entire stacktrace? It seems like a bug in the library. Also you might want to raise a ticket here github.com/google/gcm/issuesevanescent

1 Answers

2
votes

Integrate without CocoaPods

First Read Firebase Doc. => Firebase Doc

  1. Register project on Firebase here => Register Project here

  2. Get GoogleService-Info.plist file from here=> project=> settings => General

  3. GoogleService-Info.plist file drop in your project.

  4. set Notification .p12 Certificates (Production and Development) in Firebase => project=> settings => Cloud Messaging

  5. Download Firebase SDK here => Firebase SDK Download

  6. Create SDK folder in your project and Drop all SDK Folder in it.

  7. Now Add this Framework in your Xcode => libicucore.tbd

  8. Set Background Modes ON in Xcode => Projects => Capabilities => Background Mode ON => RemoteNotification

enter image description here

In Objective-c your Appdelegate.m file

#import "AppDelegate.h"
#import "Firebase.h"
#import "AFNHelper.h"

@interface AppDelegate (){
    
    NSString *InstanceID;
}
@property (nonatomic, strong) NSString *strUUID;
@property (nonatomic, strong) NSString *strDeviceToken;
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    [FIRApp configure];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
    
    return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
    
    NSLog(@"userInfo=>%@", userInfo);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
    NSLog(@"deviceToken1 = %@",deviceToken);
    
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
   NSLog(@"instanceId_notification=>%@",[notification object]);
    InstanceID = [NSString stringWithFormat:@"%@",[notification object]];
   
 [self connectToFcm];  
}

- (void)connectToFcm {
    
    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Unable to connect to FCM. %@", error);
        } else {
            
            // you can send your token here with api or etc.... 
                   
        }
}