iOS开发

iOS 谷歌推送服务

2020-08-20  本文已影响0人  coming_168

简单记录一下集成谷歌推送服务的步骤:

一、谷歌推送平台部署

1、平台地址(需翻墙):
https://console.firebase.google.com/u/0/
2、注册平台账户
3、添加应用

image.png
4、项目设置
image.png
5、添加推送证书
项目概览->项目设置->云消息传递 image.png
6、下载配置文件,将下载的文件拖入到项目中 image.png

二、集成SDK

三、代码设置部分

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 谷歌推送相关
    [self setUpFirebaseConfigure];

    return YES;
}

- (void)setUpFirebaseConfigure {
    // 初始化配置
    [FIRApp configure];
    [FIRMessaging messaging].delegate = self;
    
    // 注册接受远程通知
    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 {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

#pragma mark - FIRMessagingDelegate
// 获取注册令牌
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {

    NSLog(@"fcmToken->%@", fcmToken);
    
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FCMToken" object:nil userInfo:dataDict];
    
    [[NSUserDefaults standardUserDefaults] setObject:fcmToken forKey:kFCMToken];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // 登录状态
    if (isLogin) {
       // 上传token到服务器
       ...
    }
}

// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
    NSLog(@"Received data message: %@", remoteMessage.appData);
}

// 接收通知消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSString *type = userInfo[@"type"];
    
    completionHandler(UIBackgroundFetchResultNewData);
    if(application.applicationState == UIApplicationStateInactive) {
       // 点击调转页面
       ...
    }
}

#pragma mark - UNUserNotificationCenterDelegate
// app处在前台收到推送消息执行的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)) {
    NSDictionary *userInfo = notification.request.content.userInfo;
    NSLog(@"userInfo -> %@", userInfo);
    // type = 0,跳转...
    completionHandler(UNNotificationPresentationOptionAlert);
}

// ios 10以后系统,app处在后台,点击通知栏 app执行的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    
    NSLog(@"userInfo -> %@", userInfo);
    
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
//        return;
    }
    // type = 0,跳转...
    ...
    completionHandler();
}
上一篇下一篇

猜你喜欢

热点阅读