iOS 10.0前的Notification推送
前言
推送为远程推送,一般由苹果APNS服务器发送给苹果设备(iPhone,iPad)
推送分在前台和后台。在前台时 用户可以在application 的代理回调接口中做相应处理;在后台时 系统会全权接手所有事宜 所以需要的字端一定要让后端人员配合做好。
{
"aps": {
"alert": {
"title": "This is .",
"body": "who are you"
},
"badge": 1,
"sound": "default",
"category": "Huang_Category",
"mutable-content": "1"
},
"messageId": 123,
"messageType": 0,
"userInboxId": "{\"2535025486\":\"123\"}",
"image": "http://172.16.3.129/pic/1224.jpg"
}
客户端发送一个请求给服务端 --> 服务端发送一个请求给APNS --> APNS给特定的设备发送 --> 特定设备的系统收到消息 (在前台 事件传递到app中,这个时候开发者可以操作了,在后台 系统会根据系统设置的通知设置自行处理 )
1. 在配置文件中开启推送权限
2. 在appDelegate的didFinishLaunchingWithOptions 中注册notificationsetting
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:set];
[application registerUserNotificationSettings:settings]; // set 是一个装载模型为UIMutableUserNotificationCategory的NSSet对象
在回调中注册远程通知
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
此时苹果服务器会返回一个devicetoken给我们,这个devicetoken一般是不会变的
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"deviceToken %@",deviceToken);
} - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"error %@",error);
}
3. 本地推送
3.1 发送本地通知
- (void)showLocalNotificationWithUserInfo:(NSDictionary *)userInfo fireDate:(NSDate *)firDate{
NSDictionary * aps = [userInfo objectForKey:@"aps"];
NSString * body;
NSDictionary * alert = [aps objectForKey:@"alert"];
if ([alert isKindOfClass:[NSString class]]) {
body = (NSString *)alert;
}else{
body = [alert objectForKey:@"body"];
}
UILocalNotification * notification = [[UILocalNotification alloc] init];
NSDictionary * hgl = @{
@"huang":@5,
@"gu":@(2),
@"long":@"4"
};
NSMutableDictionary * newInfo = [NSMutableDictionary dictionaryWithDictionary:userInfo];
[newInfo setObject:hgl forKey:@"hgl"];
notification.userInfo = newInfo;
notification.fireDate = firDate;
notification.alertBody = body;
notification.category = [aps objectForKey:@"category"];
if (@available(iOS 8.2, *)) {
notification.alertTitle = [alert objectForKey:@"title"];
} else {
// Fallback on earlier versions
}
notification.applicationIconBadgeNumber = 1;
if ([UIApplication sharedApplication].currentUserNotificationSettings.types & UIUserNotificationTypeSound) {
notification.soundName = UILocalNotificationDefaultSoundName;
if (notification.fireDate.timeIntervalSinceNow <= 1) {
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
AudioServicesPlaySystemSound(1007); /*
NSArray * array = @[@(1002),@(1007),@(1011),@(1012),@(1015),@(1050),@(1055)]; static int m = 0;//02,07,11,12,15,50,55,
NSNumber * n = array[m%7];
AudioServicesPlaySystemSound(n.integerValue);
NSLog(@"------%ld",n.integerValue);
m++;
*/
}
}
}
//生效时间根据fireDate确定
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//立即生效的通知,与fireDate无关
// [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
3.2 前台
发送本地推送后,此时会回调 didReceiveLocalNotification的方法,并不会弹框 ,但是会在通知中心留有一个推送信息
在通知中心点击通知后 也会回调didReceiveLocalNotification
后台
一般用延时操作可以达到后台效果,此时会系统接收做处理
点击通知后 也会回调didReceiveLocalNotification 不过此时application.applicationState的为 UIApplicationStateInactive
4. 远程
使用一些网上的工具可以给指定的设备 发送远程推送
eg. SmartPush
前台
收到推送时 didReceiveLocalNotification会回调,这时你可以做自己想要做的事啦 (一般无法就是再发一个本地推送,这时又回调 3啦)
点击通知后 会回调didReceiveLocalNotification
后台
系统会帮忙处理的 是否弹框
点击通知后 didReceiveLocalNotification会回调
离线
系统会帮忙处理的 是否弹框
点击通知后 didReceiveLocalNotification会回调
------------------------------------------------------------------------------------------------
注 :
注册的时候 我们生成 UIUserNotificationSettings对象时 用到了一个set ,这个set一般我会用nil代替,他的左右是在通知右滑的时候显示按钮用的 (至多2个)
UIMutableUserNotificationAction * joinAction = [[UIMutableUserNotificationAction alloc] init];
joinAction.title = @"接受交友";
joinAction.authenticationRequired = YES;
joinAction.identifier = @"gu.join"; UIMutableUserNotificationAction * lookAction = [[UIMutableUserNotificationAction alloc] init];
lookAction.title = @"查看信息";
lookAction.identifier = @"gu.look";
lookAction.activationMode = UIUserNotificationActivationModeForeground; UIMutableUserNotificationAction * cancelAction = [[UIMutableUserNotificationAction alloc] init];
cancelAction.title = @"取消";
cancelAction.identifier = @"gu.cancel";
cancelAction.destructive = YES; UIMutableUserNotificationCategory * noticeCategory = [[UIMutableUserNotificationCategory alloc] init];
noticeCategory.identifier = @"Huang_Category";
[noticeCategory setActions:@[joinAction,lookAction] forContext:(UIUserNotificationActionContextDefault)]; NSMutableSet * set = [NSMutableSet setWithObject:noticeCategory]; if (@available(iOS 9.0, *)) {
UIMutableUserNotificationAction * inputAction = [[UIMutableUserNotificationAction alloc] init];
inputAction.title = @"评论";
inputAction.identifier = @"gu.input";
inputAction.behavior = UIUserNotificationActionBehaviorTextInput;
inputAction.parameters = @{
UIUserNotificationTextInputActionButtonTitleKey:@"我说"
}; UIMutableUserNotificationCategory * inputCategory = [[UIMutableUserNotificationCategory alloc] init];
inputCategory.identifier = @"Long_Category";
[inputCategory setActions:@[inputAction] forContext:(UIUserNotificationActionContextDefault)];
[set addObject:inputCategory];
}
点击通知上的按钮后 根据远程和本地 8.0/9.0 会有四个不同方法的回调
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
[self gl_handleActionWithIdentifier:identifier userInfo:notification.userInfo withResponseInfo:nil];
NSLog(@"%s",__FUNCTION__);
completionHandler();
} - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{
[self gl_handleActionWithIdentifier:identifier userInfo:userInfo withResponseInfo:nil];
NSLog(@"%s",__FUNCTION__);
completionHandler();
} - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
[self gl_handleActionWithIdentifier:identifier userInfo:notification.userInfo withResponseInfo:responseInfo];
NSLog(@"%s",__FUNCTION__);
completionHandler();
} - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler{
[self gl_handleActionWithIdentifier:identifier userInfo:userInfo withResponseInfo:responseInfo];
NSLog(@"%s",__FUNCTION__);
completionHandler();
}
iOS 10.0前的Notification推送的更多相关文章
- iOS 10.0之前和之后的Local Notification有神马不同
在iOS 10.0之前apple还没有将通知功能单独拿出来自成一系.而从10.0开始原来的本地通知仍然可用,只是被标记为过时.于是乎我们可以使用10.0全新的通知功能.别急-让我们慢慢来,先从iOS ...
- 李洪强iOS经典面试题154- 通知与推送
李洪强iOS经典面试题154- 通知与推送 通知与推送 本地通知和远程推送通知对基本概念和用法? image 本地通知和远程推送通知都可以向不在前台运行的应用发送消息,这种消息既可能是即将发生的事 ...
- iOS 10.0 更新点(开发者视角)
html, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0 ...
- Android、iOS和Windows Phone中的推送技术
推送并不是什么新技术,这种技术在互联网时代就已经很流行了.只是随着进入移动互联网时代,推送技术显得更加重要.因为在智能手机中,推送从某种程度上,可以取代使用多年的短信,而且与短信相比,还可以向用户展示 ...
- iOS直播-基于RTMP的视频推送
iOS直播-基于RTMP的视频推送 所谓的视频推送就是把摄像头和麦克风捕获到视频和音频推送到直播服务器上.我们这里使用推送协议是RTMP协议. 扩展:腾讯直播平台,阿里直播平台,百度直播平台提供均为R ...
- iOS打包后收不到推送信息
今天遇到的一个特别神奇的问题: 应用在测试环境下打的包收不到推送了,打包之后都没有推送,但是我真机测试又是可以收到推送消息的.经过好久才找到原因,感觉很坑.这里记录一下问题: 1.由于我的推送第三方使 ...
- iOS---iOS10适配iOS当前所有系统的远程推送
一.iOS推送通知简介 众所周知苹果的推送通知从iOS3开始出现, 每一年都会更新一些新的用法. 譬如iOS7出现的Silent remote notifications(远程静默推送), iOS8出 ...
- iOS监听模式系列之推送消息通知
推送通知 和本地通知不同,推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: ...
- IOS开发之实现App消息推送
转自:http://blog.csdn.net/shenjie12345678/article/details/41120637 第一部分 首先第一步当然是介绍一下苹果的推送机制(APNS)咯(ps: ...
随机推荐
- 【mybatis源码学习】mybatis的sql语句映射
一.重要的接口和类 org.apache.ibatis.scripting.LanguageDriver //语言驱动org.apache.ibatis.scripting.xmltags.XMLLa ...
- Oracle系列十 创建和管理表
常见的数据库对象 Oracle 数据库中的表 用户定义的表: 用户自己创建并维护的一组表 包含了用户所需的信息 如:SELECT * FROM user_tables;查看用户创建的表 数据字典: 由 ...
- 两种Redis持久化原理的详解
Redis为持久化提供了两种方式: RDB:在指定的时间间隔能对你的数据进行快照存储. AOF:记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据. 本文将通过下面内容的介 ...
- 系统运维工程师装逼完全指南(转载Mark)
1.全球化的认证有助于提升逼格,什么OCM.CCIE.RHCA.CISSP等等能考都考,再不济,也要有一张系统架构设计师或者网络规划设计师的信产部认证.每过一个认证,逼格提升一档. 2.TCP/IP协 ...
- JIT(just in time)即时编译器
JIT(just in time) 前端vs后端 在编译原理中,通常将编译分为前端和后端.其中前端会对程序进行词法分析.语法分析.语义分析,然后生成一个中间表达形式(称为IR:Intermediate ...
- 【面试】C++类中的相关函数【构造,拷贝构造,析构,友元】
构造函数:值的初始化,可带参数,无返回值,可重载,可存在多个 析构函数:释放对象内存空间,无参数,无返回值,不可重载,只能存在一个 拷贝构造函数:拷贝对象,其形参必须是引用 1.空类会默认添加哪些东西 ...
- Django REST Framework批量更新rest_framework_extensions
Django REST framework 是一套基于Django框架编写RESTful风格API的组件. 其中mixins配合viewsets能极其方便简化对数据的增删改查, 但本身并没有对数据的批 ...
- centos发布 7.7.1908版本了,怎么把老版本更新到新版本了?
CENTOS升级 7.6 升级到7.7.1908 0.查看目前版本 cat /etc/issue cat /etc/redhat-release 1.下载系统镜像文件 https://www.cent ...
- SPI通讯(Serial Peripheral interface)
1. SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线:SCLK,MISO,MOSI,CS 2. SPI结构简图: 可以看出,SPI主从设备两端都有一个位移寄存器,数据在位 ...
- [清晰] kubernets权威指南第2版
------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...