美文网首页
iOS firebase推送集成和注意事项

iOS firebase推送集成和注意事项

作者: 左岸花不开 | 来源:发表于2019-10-10 13:40 被阅读0次
    1. 添加您要安装的 Pod,在 Podfile 中纳入 Pod:
    pod 'Firebase/Core'
    pod 'Firebase/Messaging'
    
    1. 从 Firebase 控制台中下载一个 GoogleService-Info.plist 文件并将其添加到您的应用中。

    将您的 APNs 身份验证密钥上传到 Firebase。如果您还没有 APNs 身份验证密钥,请参阅配置 FCM APNs

    1. 在 Firebase 控制台中,在您的项目内依次选择齿轮图标、项目设置以及云消息传递标签。
    2. iOS 应用配置下的 APNs 身份验证密钥中,点击上传按钮。
    3. 转到您保存密钥的位置,选择该密钥,然后点击打开。添加该密钥的密钥 ID(可在 Apple 开发者会员中心Certificates, Identifiers & Profiles 中找到),然后点击上传
    1. UIApplicationDelegate 中导入 Firebase 模块:
    @import  Firebase;
    //添加代理
    @interface AppDelegate ()<FIRMessagingDelegate,UNUserNotificationCenterDelegate>
    
    1. 在您的应用中初始化 Firebase,配置一个 FirebaseApp 共享实例(通常在应用的 application:didFinishLaunchingWithOptions: 方法中配置):
    // Use Firebase library to configure APIs
    [FIRApp configure];
    [FIRMessaging messaging].delegate = self;
    
    1. // 注册接收远程通知
        if (@available(iOS 10.0, *)) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = self;
            [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
                if (!error) {
                    NSLog(@"request authorization succeeded!");
                }
            }];
        } else {
            // Fallback on earlier versions
            UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    1. 实现的FIRMessaging代理方法
    // [END ios_10_message_handling]
    // [START refresh_token]
    //获取注册令牌
    - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
        NSLog(@"FCM registration token: %@", fcmToken);
        // Notify about received token.
        NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"FCMToken" object:nil userInfo:dataDict];
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }
    // [END refresh_token]
    
    // [START ios_10_data_message]
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES.
    - (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
        NSLog(@"Received data message: %@", remoteMessage.appData);
    }
    // [END ios_10_data_message]
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSLog(@"Unable to register for remote notifications: %@", error);
    }
    

    pragma mark - 通知相关方法

    // 需要将您的 APNs 令牌明确映射到 FCM 注册令牌

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        //firebase
        [FIRMessaging messaging].APNSToken = deviceToken;
    }
    
    //接收通知消息
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        NSLog(@"userInfo=%@",userInfo);
        
        // firebase推送
        // Print message ID.
        if (userInfo[kGCMMessageIDKey]) {
            NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
        }    
        completionHandler(UIBackgroundFetchResultNewData);
        if(application.applicationState==UIApplicationStateInactive){//点击
           [self showClickPushNotice:userInfo];
        }
    }
    


    FireBase使用过程注意事项及可能出现的问题

    注册令牌会在发生下列情况时更改:
    用户卸载/重新安装应用
    用户清除应用数据
    

    出现不执行messaging didReceiveRegistrationToken的代理方法原因如下:

    1. 这是由于该服务是谷歌提供的,会因为网络问题无法执行代理方法获得token,国内需要使用VPN翻墙工具.
    2. pod 'Firebase/Core' 的版本过低,需要pod最新版本,如果pod还是旧版本,可以 如:pod 'Firebase/Core',  '5.15.0'
    

    可以参考Firebase官方文档
    Firebase SDK版本

    相关文章

      网友评论

          本文标题:iOS firebase推送集成和注意事项

          本文链接:https://www.haomeiwen.com/subject/bchauctx.html