直播平台开发中关于iOS系统推送主要分为三种:在线Push、本地Push、离线/远程Push,三者中 离线/远程Push在客户App的推广扮演者重要地位;下面就介绍下离线/远程Push在iOS中的实现:
第一步:注册
-(void)registerNotification{
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//必须写代理,不然无法监听通知的接收与点击事件
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error && granted) {
//用户点击允许
NSLog(@"iOS10注册通知成功");
// 可以通过 getNotificationSettingsWithCompletionHandler 获取权限设置
//之前注册推送服务,用户点击了同意还是不同意,以及用户之后又做了怎样的更改我们都无从得知,现在 apple 开放了这个 API,我们可以直接获取到用户的设定信息了。注意UNNotificationSettings是只读对象哦,不能直接修改!
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
// NSLog(@"========%@",settings);
if(settings.authorizationStatus!=UNAuthorizationStatusAuthorized){
//用户没有授权或拒绝权限
}
}];
}else{
//用户点击不允许
NSLog(@"iOS10注册通知失败");
//提示用户是否打开或设备支持授权
}
}];
} else {
// Fallback on earlier versions
if (@available(iOS 8.0, *)) {
//iOS8+
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
}
//iOS8+,包括iOS10+都是需要这个方法,才能获取
[[UIApplication sharedApplication]registerForRemoteNotifications];
}
#pragma mark - PushKit 和 PKPushRegistryDelegate
-(void)registerPKPush{
PKPushRegistry *pushRegistry=[[PKPushRegistry alloc]initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate=self;
pushRegistry.desiredPushTypes=[NSSet setWithObject:PKPushTypeVoIP];
}
第二步:获取token
-(void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)pushCredentials forType:(PKPushType)type{
//获取token,这个token需要上传到服务器
NSData *data=pushCredentials.token;
NSString *token=[NSString stringWithFormat:@"%@",data];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
token = [token stringByTrimmingCharactersInSet:set];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"PushKit Token:%@",token);
//MARK: 在这里补充自己的pushkit token上传到服务器的逻辑
}
第三步:处理收到的Push消息
//收到pushkit的通知时会调用这个方法,但是不会有UI上的显示
-(void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type{
//iOS8.0-11.0
[FanPushManager fan_sendLocalNotificationBody:@"iOS8 PushKit发送的推送!"];
NSDictionary *dic=payload.dictionaryPayload[@"aps"];
//MARK: 解析远程推送的消息,并做处理和跳转
}
//收到pushkit的通知时会调用这个方法,但是不会有UI上的显示
-(void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion{
//iOS11.0+
[FanPushManager fan_sendLocalNotificationBody:@"iOS11 PushKit发送的推送!"];
}
#pragma mark - 正常的推送代理,iOS 10++, iOS8的代理直接在APPdelegate里面
//iOS10+在前台模式下接受消息,正常推送
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)){
UNNotificationContent * content = notification.request.content;//通知消息体内容 title subtitle body
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]
]) {
//远程通知
NSLog(@"userinfo:%@\n body:%@ \n title:%@",content.userInfo,content.body,content.title);
// if(self.fanEvent){
// self.fanEvent(content.userInfo);
// }
}else{
//本地通知
}
//MARK:解析推送的消息,并做处理和跳转
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
//这里是有点问题 的,如果用户关闭通知,但是角标不会消失,
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
// if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
// [UIApplication sharedApplication].applicationIconBadgeNumber--;
// }
}
//iOS10+在后台模式下打开消息(3DTouch不会触发次方法)
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
UNNotificationContent * content = response.notification.request.content;//通知消息体内容 title subtitle body
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//打开远程通知
if(self.fanEvent){
self.fanEvent(content.userInfo);
}
}else{
//打开本地通知
}
//MARK: 解析推送的消息,并做处理和跳转
completionHandler(); // 系统要求执 这个 法
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
// if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
// [UIApplication sharedApplication].applicationIconBadgeNumber--;
// }
}
第四步:关闭Push
#pragma mark - 关闭通知
-(void)cancelAllPush{
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
if (@available(iOS 10.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
}else{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
}
推送的加入使我们的直播平台、短视频平台、一对一视频交友平台的推广机制更加完善,欢迎下载平台体验。
想要了解我们产品更多功能请关注我们的账号,我们会持续更新直播平台开发功能实现的文章。
声明:本篇文章为原创文章,转载请注明出处及作者。