IOSIO注册通知
第一步: 导入 #import
且要遵守
这里需要注意,我们最好写成这种形式(防止低版本找不到头文件出现问题)
#ifdef nsfoundationversionnumber_ios_9_x_max
#import
#endif
第二步:我们需要在
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions中注册通知,代码如下
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
[self replypushnotificationauthorization:application];
return yes; }
#pragma mark - 申请通知权限// 申请通知权限
- (void)replypushnotificationauthorization:(uiapplication *)application{
if (ios10_or_later) {
//ios 10 later
unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
//必须写代理,不然无法监听通知的接收与点击事件
center.delegate = self;
[center requestauthorizationwithoptions:(unauthorizationoptionbadge | unauthorizationoptionsound | unauthorizationoptionalert) completionhandler:^(bool granted, nserror * _nullable error) {
if (!error && granted) {
//用户点击允许
nslog(@"注册成功");
}else{
//用户点击不允许
nslog(@"注册失败");
}
}];
// 可以通过 getnotificationsettingswithcompletionhandler 获取权限设置
//之前注册推送服务,用户点击了同意还是不同意,以及用户之后又做了怎样的更改我们都无从得知,现在 apple 开放了这个 api,我们可以直接获取到用户的设定信息了。注意unnotificationsettings是只读对象哦,不能直接修改!
[center getnotificationsettingswithcompletionhandler:^(unnotificationsettings * _nonnull settings) { nslog(@"========%@",settings);
}];
}else if (ios8_or_later){
//ios 8 - ios 10
uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes:uiusernotificationtypealert | uiusernotificationtypebadge | uiusernotificationtypesound categories:nil];
[application registerusernotificationsettings:settings];
}else{
//ios 8.0系统以下
[application registerforremotenotificationtypes:uiremotenotificationtypebadge | uiremotenotificationtypealert | uiremotenotificationtypesound];
}
//注册远端消息通知获取device token
[application registerforremotenotifications];
}
上面需要注意:
之前注册推送服务,用户点击了同意还是不同意,以及用户之后又做了怎样的更改我们都无从得知,现在 apple 开放了这个 api,我们可以直接获取到用户的设定信息了。注意unnotificationsettings是只读对象哦,不能直接修改!只能通过以下方式获取
[center getnotificationsettingswithcompletionhandler:^(unnotificationsettings * _nonnull settings) {
nslog(@"========%@",settings);
}];
打印信息如下: ========
4、 远端推送需要获取设备的device token的方法是没有变的,代码如下
#pragma mark - 获取device token//获取devicetoken成功
- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken{ //解析nsdata获取字符串
//我看网上这部分直接使用下面方法转换为string,你会得到一个nil(别怪我不告诉你哦)
//错误写法
//nsstring *string = [[nsstring alloc] initwithdata:devicetoken encoding:nsutf8stringencoding];
//正确写法
nsstring *devicestring = [[devicetoken description] stringbytrimmingcharactersinset:[nscharacterset charactersetwithcharactersinstring:@"<>"]];
devicestring = [devicestring stringbyreplacingoccurrencesofstring:@" " withstring:@""];
nslog(@"devicetoken===========%@",devicestring); }
//获取devicetoken失败
- (void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error{ nslog(@"[devicetoken error]:%@\n",error.description);
}
5、这一步吊了,这是ios 10系统更新时,苹果给了我们2个代理方法来处理通知的接收和点击事件,这两个方法在
此外,苹果把本地通知跟远程通知合二为一。区分本地通知跟远程通知的类是
unpushnotificationtrigger.h类中,
unpushnotificationtrigger的类型是新增加的,通过它,我们可以得到一些通知的触发条件 ,解释如下:
-
unpushnotificationtrigger (远程通知) 远程推送的通知类型
-
untimeintervalnotificationtrigger (本地通知) 一定时间之后,重复或者不重复推送通知。我们可以设置timeinterval(时间间隔)和repeats(是否重复)。
-
uncalendarnotificationtrigger(本地通知) 一定日期之后,重复或者不重复推送通知 例如,你每天8点推送一个通知,只要datecomponents为8,如果你想每天8点都推送这个通知,只要repeats为yes就可以了。
-
当用户进入或离开一个地理区域来通知。unlocationnotificationtrigger (本地通知)地理位置的一种通知,
现在先提出来,后面我会一一代码演示出每种用法。还是回到两个很吊的代理方法吧
#pragma mark - ios10 收到通知(本地和远端) unusernotificationcenterdelegate //app处于前台接收通知时 - (void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler{ //收到推送的请求 unnotificationrequest *request = notification.request; //收到推送的内容 unnotificationcontent *content = request.content; //收到用户的基本信息 nsdictionary *userinfo = content.userinfo; //收到推送消息的角标 nsnumber *badge = content.badge; //收到推送消息body nsstring *body = content.body; //推送消息的声音 unnotificationsound *sound = content.sound; // 推送消息的副标题 nsstring *subtitle = content.subtitle; // 推送消息的标题 nsstring *title = content.title; if([notification.request.trigger iskindofclass:[unpushnotificationtrigger class]]) { //此处省略一万行需求代码。。。。。。 nslog(@"ios10 收到远程通知:%@",userinfo); }else { // 判断为本地通知 //此处省略一万行需求代码。。。。。。 nslog(@"ios10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserinfo:%@\\\\n}",body,title,subtitle,badge,sound,userinfo); } // 需要执行这个方法,选择是否提醒用户,有badge、sound、alert三种类型可以设置 completionhandler(unnotificationpresentationoptionbadge| unnotificationpresentationoptionsound| unnotificationpresentationoptionalert); } //app通知的点击事件 - (void)usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler{ //收到推送的请求 unnotificationrequest *request = response.notification.request; //收到推送的内容 unnotificationcontent *content = request.content; //收到用户的基本信息 nsdictionary *userinfo = content.userinfo; //收到推送消息的角标 nsnumber *badge = content.badge; //收到推送消息body nsstring *body = content.body; //推送消息的声音 unnotificationsound *sound = content.sound; // 推送消息的副标题 nsstring *subtitle = content.subtitle; // 推送消息的标题 nsstring *title = content.title; if([response.notification.request.trigger iskindofclass:[unpushnotificationtrigger class]]) { nslog(@"ios10 收到远程通知:%@",userinfo); //此处省略一万行需求代码。。。。。。 }else { // 判断为本地通知 //此处省略一万行需求代码。。。。。。 nslog(@"ios10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserinfo:%@\\\\n}",body,title,subtitle,badge,sound,userinfo); } //2016-09-27 14:42:16.353978 usernotificationsdemo[1765:800117] warning: unusernotificationcenter delegate received call to -usernotificationcenter:didreceivenotificationresponse:withcompletionhandler: but the completion handler was never called. completionhandler(); // 系统要求执行这个方法 }
需要注意的:
1.下面这个代理方法,只会是app处于前台状态 前台状态 and 前台状态下才会走,后台模式下是不会走这里的 - (void)usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:(void (^)(unnotificationpresentationoptions))completionhandler 2.下面这个代理方法,只会是用户点击消息才会触发,如果使用户长按(3dtouch)、action等并不会触发。 - (void)usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:(void (^)())completionhandler 3.点击代理最后需要执行:completionhandler(); // 系统要求执行这个方法 不然会报: 2016-09-27 14:42:16.353978 usernotificationsdemo[1765:800117] warning: unusernotificationcenter delegate received call to -usernotificationcenter:didreceivenotificationresponse:withcompletionhandler: but the completion handler was never called. 4.不管前台后台状态下。推送消息的横幅都可以展示出来!后台状态不用说,前台时需要在前台代理方法中设置 ,设置如下: // 需要执行这个方法,选择是否提醒用户,有badge、sound、alert三种类型可以设置 completionhandler(unnotificationpresentationoptionbadge| unnotificationpresentationoptionsound| unnotificationpresentationoptionalert);
上一篇: C#窗体打包步骤