天天看點

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];  

繼續閱讀