iOS 接入个推消息推送

前言

消息推送包含本地推送(Local Notification)和远程推送(Remote Notification)。
本地推送相当于个推的透传消息,远程推送个推也是调用的苹果的APNS(Apple Push Notification Services)实现的。

接入个推

本文采用pod的方式导入。
1、首先要在个推开放平台申请相应的key(appId、appKey,appSecret)。
2、在Podfile文件中加入pod 'GTSDK'。
3、在终端执行pod install。
4、个推SDK就会被引进项目。
5、注册个推

    [GeTuiSdk startSdkWithAppId:appID appKey:appKey appSecret:appSeceret delegate:self];
    [GeTuiSdk runBackgroundEnable:YES];

6、注册远程推送(项目只支持iOS8及以上)

//iOS 10以上
    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 (granted) {
                DKLog(@"允许通知");
            } else {
               //无通知权限提示弹框代码
               
            }
        }];
    } else {
        // iOS10以下
        UIApplication * application = [UIApplication sharedApplication];
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
        [application registerForRemoteNotifications];
        
        UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (settings.types == UIUserNotificationTypeNone) {
           //无通知权限提示弹框代码
        }
    }
    [[UIApplication sharedApplication] registerForRemoteNotifications];

7、在接收远程推送的DeviceToken方法中, 获取DeviceToken(把获取到的DeviceToken传给自己的服务器)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSString *deviceTokenStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    deviceTokenStr = [deviceTokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    //向服务器注册deviceToken
    [GeTuiSdk registerDeviceToken:deviceTokenStr];
}

8、成功启动个推SDK后获取clientid

- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
//将个推id和登录用户进行绑定,实现推送到对应的用户
}

9、推送接收(个推的代理方法)

- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
if (@available(iOS 10.0, *)) {
        // 创建推送弹框
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title =  "标题";
        content.body = "内容";   
        content.sound = [UNNotificationSound soundNamed:"声音名"];
        content.userInfo = userInfo;
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"my_notification" content:content trigger:trigger];
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        }];
    } else {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.alertTitle = title;
        localNotification.alertBody = text;   
        localNotification.soundName = soundName;
        localNotification.userInfo = userInfo;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }
}

自定义推送声音:sound传入自定义的音频,远程推送时sound必须和个推传参的sound一致。

10、推送的点击事件

//  iOS 10以上 点击通知进入App时触发(本地及远程)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)) {

}
//iOS 10以下版本(远程)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

}

//本地(ios9的时候测试在应用内会自动调用此方法,所以做了判断)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    //非应用内再调用
    if (application.applicationState != UIApplicationStateActive) {
       
    }
}

至此,个推接入成功并能成功接收消息。

写在最后

远程推送
远程推送(Apple Push Notification Services)是在联网的情况下,由远程服务器推送给客户端的通知。
原理:
1.打开App时: 发送UDID和BundleID给APNs加密后返回deviceToken
2.获取Token后,App调用接口,将用户身份信息和deviceToken发给服务器,服务器记录
3.当推送消息时, 服务器按照用户身份信息找到存储的deviceToken,将消息和deviToken发送给APNs
4.苹果的APNs通过deviceToken, 找到指定设备的指定程序, 并将消息推送给用户

如图:
iOS 接入个推消息推送_第1张图片
屏幕快照 2019-05-18 下午12.30.30.png

你可能感兴趣的:(iOS 接入个推消息推送)