天天看点

iOS iOS8注册通知

目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。

iOS iOS8注册通知

我们先开始讲这个用户推送,我们要使用之前必须先注册这个推送,用户要允许这个程序进行推送

注册过程:

[objc] view plaincopy

iOS iOS8注册通知
iOS iOS8注册通知

if (is_ios8) {  

        //1.创建消息上面要添加的动作(按钮的形式显示出来)  

        uimutableusernotificationaction *action = [[uimutableusernotificationaction alloc] init];  

        action.identifier = @"action";//按钮的标示  

        action.title=@"accept";//按钮的标题  

        action.activationmode = uiusernotificationactivationmodeforeground;//当点击的时候启动程序  

        //    action.authenticationrequired = yes;  

        //    action.destructive = yes;  

        uimutableusernotificationaction *action2 = [[uimutableusernotificationaction alloc] init];  

        action2.identifier = @"action2";  

        action2.title=@"reject";  

        action2.activationmode = uiusernotificationactivationmodebackground;//当点击的时候不启动程序,在后台处理  

        action.authenticationrequired = yes;//需要解锁才能处理,如果action.activationmode = uiusernotificationactivationmodeforeground;则这个属性被忽略;  

        action.destructive = yes;  

        //2.创建动作(按钮)的类别集合  

        uimutableusernotificationcategory *categorys = [[uimutableusernotificationcategory alloc] init];  

        categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分  

        [categorys setactions:@[action,action2] forcontext:(uiusernotificationactioncontextminimal)];  

        //3.创建uiusernotificationsettings,并设置消息的显示类类型  

        uiusernotificationsettings *notisettings = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypebadge | uiusernotificationtypealert | uiremotenotificationtypesound) categories:[nsset setwithobjects:categorys, nil nil]];  

        [application registerusernotificationsettings:notisettings];  

    }else{  

        [application registerforremotenotificationtypes: uiremotenotificationtypealert | uiremotenotificationtypebadge | uiremotenotificationtypesound];  

    }  

iOS iOS8注册通知
iOS iOS8注册通知

- (void)application:(uiapplication *)application didregisterusernotificationsettings:(uiusernotificationsettings *)notificationsettings  

{  

//    uiusernotificationsettings *settings = [application currentusernotificationsettings];  

//    uiusernotificationtype types = [settings types];  

//    //只有5跟7的时候包含了 uiusernotificationtypebadge  

//    if (types == 5 || types == 7) {  

//        application.applicationiconbadgenumber = 0;  

//    }  

    //注册远程通知  

    [application registerforremotenotifications];  

}  

我们现在仅仅是注册了通知的设置,还要注册推送通知的行为,在ios8中,行为能直接在推送消息进行,如回复消息,拒绝消息等总结就是三个方法进行注册

iOS iOS8注册通知

我们如何能进行这些行为,首先我们需注册这些行为。

actions

iOS iOS8注册通知
iOS iOS8注册通知

uimutableusernotificationaction *acceptaction = [[uimutableusernotificationaction alloc] init];  

acceptaction.identifier = @"rickaction";  

acceptaction.title = @"accept";  

acceptaction.activationmode = uiusernotificationactivationmodebackground;  

acceptaction.destructive = no;  

acceptaction.authenticationrequired = no;  

categories[objc] view plaincopy我们需要注意这个uiusernotificationactioncontextdefault,如果我们使用这个,我们会得到这个推送行为,maybe和accept我们还可以使用uiusernotificationactioncontextminimal得到的是decline和accept行为

uimutableusernotificationcategory *invitecategory = [[uimutableusernotificationcategory alloc] init];  

invitecategory.identifier = @"invite_category";  

[invitecategory setactions:@[acceptaction] forcontext:uiusernotificationactioncontextdefault];  

settings在这些行为注册之后,我们加上之前提到的推送设置就完成了注册推送的这个流程了[objc] view plaincopy远程推送远程推送,所有消息大小不超过2kb,我们获取远程推送的json格式的消息,解析这个消息就是我们的远程推送了:[html] view plaincopy若要使用远程推送,满足两个条件:一、用户需要调用注册用户推送registerusernotificationsettings;二、在info.plist文件中uibackgroundmodes必须包含远程通知。[objc] view plaincopy[objc] view plaincopy[objc] view plaincopyios7通知代理方法后来又增加了本地通知的代理方法ios8的推送代理方法只有两个了[objc] view plaincopy地理位置推送这个推送是新的api才有的特性,必须配合cllocation定位一起使用。[objc] view plaincopy如果没有开启core location 那么上面的didreceivelocalnotification不会被调用最后再总结一下,整个推送流程我觉得是这样子的,先注册推送,然后推送消息,客户端接收推送消息,执行推送行为。如果有错误,还请在文章下面评论,欢迎指正。

//location notification  

    cllocationmanager *locman = [[cllocationmanager alloc] init];  

    locman.delegate = self;  

    [locman requestwheninuseauthorization];  

#pragma mark - cllocationmanager  

- (void)locationmanager:(cllocationmanager *)manager didchangeauthorizationstatus:(clauthorizationstatus)status  

    bool canuselocationnotifications = (status == kclauthorizationstatusauthorizedwheninuse);  

    if (canuselocationnotifications) {  

        [self startshowlocationnotification];  

- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification  

    clregion *region = notification.region;  

    if (region) {  

- (void)startshowlocationnotification  

    cllocationcoordinate2d local2d ;  

    local2d.latitude = 123.0;  

    local2d.longitude = 223.0;  

    uilocalnotification *locnotification = [[uilocalnotification alloc] init];  

    locnotification.alertbody = @"你接收到了";  

    locnotification.regiontriggersonce = yes;  

    locnotification.region = [[clcircularregion alloc] initwithcenter:local2d radius:45 identifier:@"local-identity"];  

    [[uiapplication sharedapplication] schedulelocalnotification:locnotification];  

- (void)application:(uiapplication *)application handleactionwithidentifier:(nsstring *)identifier forlocalnotification:(uilocalnotification *)notification completionhandler:(void (^)())completionhandler  

- (void)application:(uiapplication *)application handleactionwithidentifier:(nsstring *)identifier forremotenotification:(nsdictionary *)userinfo completionhandler:(void (^)())completionhandler  

    if ([identifier isequaltostring:@"rickaction"]) {  

        [self handleacceptactionwithnotification:notification];  

    completionhandler();  

- (void)handleacceptactionwithnotification:(uilocalnotification*)notification  

- (void)application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error {  

- (void)application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken {  

    nsstring *token=[nsstring stringwithformat:@"%@",devicetoken];  

    token=[token stringbyreplacingoccurrencesofstring:@"<" withstring:@""];  

    token=[token stringbyreplacingoccurrencesofstring:@">" withstring:@""];  

    token=[token stringbyreplacingoccurrencesofstring:@" " withstring:@""];  

<span style="font-family: helvetica, arial, geneva, sans-serif;">[[uiapplication sharedapplication] registerforremotenotifications];</span>  

    “aps”: {  

        "content-available": 1,  

        "alert": "this is the alert text",  

        "badge": 1,  

        "sound": "default"  

nsset *categories = [nsset setwithobjects:invitecategory, nil nil];  

uiusernotificationtype  types = uiusernotificationtypebadge | uiusernotificationtypesound | uiusernotificationtypealert ;  

uiusernotificationsettings  *mysettings  = [uiusernotificationsettings settingsfortypes:types categories:categories];  

[[uiapplication sharedapplication] registerusernotificationsettings:mysettings];  

继续阅读