美文网首页iOS开发
iOS 谷歌推送服务

iOS 谷歌推送服务

作者: coming_168 | 来源:发表于2020-08-20 15:49 被阅读0次

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

一、谷歌推送平台部署

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

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

二、集成SDK

  • 使用CocoaPods集成:
    pod 'Firebase/Core'
    pod 'Firebase/Messaging'

三、代码设置部分

- (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();
}

相关文章

  • iOS 谷歌推送服务

    简单记录一下集成谷歌推送服务的步骤: 一、谷歌推送平台部署 1、平台地址(需翻墙):https://console...

  • iOS推送通知及静默推送相关

    iOS推送 在IOS推送服务中,Apple提供了两种不同方式的推送形式,...

  • 推送通知-远程推送

    iOS远程推送通知 远程推送服务,APNs(apple push notification servers) 所有...

  • iOS 友盟推送--关键点/核心点

    |:-| totem iOS集成友盟推送 1.iOS集成“友盟推送”后,友盟服务响应的deviceToken = ...

  • iOS 远程推送

    iOS远程推送主要流程为:注册推送的token,把token上传到服务器->接收到服务器的推送->处理推送。注册t...

  • iOS_推送原理

    iOS 消息推送机制原理与实现苹果信息推送服务(Apple Push Notification Service),...

  • IOS APNS和VOIP 实现推送(本质)

    iOS 的推送本质 iOS 在系统级别有一个推送服务程序使用 5223 端口。使用这个端口的协议源于 Jabber...

  • iOS 远程通知

    iOS推送通知分为两种: 远程推送通知(Remote Notification) 苹果的远程通知服务 APNs(A...

  • ApiBoot 2.0.5.RELEASE 版本发布

    本次更新内容 ApiBoot Message Push(推送服务集成)极光推送组件(全平台、安卓平台、IOS线上、...

  • 远程推送工具 推荐 -- Push Notifications

    指路牌 推送工具 iOS APNs 适用场景 以iOS为例,模拟后台服务器连接APNs,向设备远程推送信息。 背景...

网友评论

    本文标题:iOS 谷歌推送服务

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