天天看點

iOS9開發之新增通知行為詳解

蘋果在ios8釋出時,收到短信時可以直接在通知欄輸入文字并回複,非常炫酷,然而這一功能并未真正開放給開發者。

ios9新增了使用者通知行為uiusernotificationactionbehaviortextinput,蘋果終于将這一炫酷的功能開放給我們。

具體方式為:

1. 設定通知行為:

//1.建立可變通知行為

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

//2.設定通知行為的表現為文本輸入

 [ua setbehavior:uiusernotificationactionbehaviortextinput];

//3.給action給一個标示符

 [ua setidentifier:@"myreply"];

//4.設定行為激活模式為保持背景運作

 [ua setactivationmode:uiusernotificationactivationmodebackground];

這裡設定激活模式時,如果在通知欄回複/閱讀之後希望跳轉回自己的app,應當原則前台激活模式,如果回複/閱讀之後希望保持背景運作才選擇這個模式

2. 設定通知政策:

//1.建立一個可變通知政策

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

//2.給category一個标示符

 [cate setidentifier:@"textcategory"];

//3.為這個政策category制定相關的通知行為action

 [cate setactions:@[ua] forcontext:uiusernotificationactioncontextdefault];

3. 注冊通知配置(ios8以後的方式):

//1.通過上面的政策來建立使用者通知配置

uiusernotificationsettings * settings = [uiusernotificationsettings settingsfortypes:uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound categories:[nsset setwitharray:@[cate]]];

//2.注冊這個通知配置

 [[uiapplication sharedapplication] registerusernotificationsettings:settings];

經過這三部之後完成了一個自定義通知政策的激活,其通知行為為文本框輸入并回複。

發送本地通知

在需要發送通知的時候,隻需要設定對應的政策和行為,就能以這種方式來彈出通知。以本地通知uilocalnotification為例:

//1.初始化本地通知

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

//2.設定通知正文

 [noti setalertbody:[nsstring stringwithformat:@"您有新的消息:%@",[nsdate new].description]];

//3.配置對應的政策和行為(必須之前已經注冊過了)

[noti setcategory:@"textcategory"];

[noti setalertaction:@"myreply"];

//4.彈出通知

[[uiapplication sharedapplication] presentlocalnotificationnow:noti];

接收通知行為文本框的回複内容

由于新的通知行為是有輸入的,是以跟目前(截止本文撰寫日期2015.9月)主流的聊天應用的通知提示方式不同,例如qq在通知欄設定了ok和取消的按鈕,這種方式雖然能夠通過ok按鈕回複”ok”兩個字,但是也隻能是這兩個字。回複的消息内容是寫死(hard write)的。

而新的通知行為是使用者在通知欄輸入什麼則回複什麼,是以app需要擷取到使用者輸入的内容。

下面2個uiapplicationdelegate的代理方法提供了這種帶reply的通知代理:

//本地通知的帶回複通知代理

- (void)application:(uiapplication *)application handleactionwithidentifier:(nullable nsstring *)identifier forlocalnotification:(uilocalnotification *)notification withresponseinfo:(nsdictionary *)responseinfo completionhandler:(void(^)())completionhandler ns_available_ios(9_0);

//遠端通知的帶回複通知代理

- (void)application:(uiapplication *)application handleactionwithidentifier:(nullable nsstring *)identifier forremotenotification:(nsdictionary *)userinfo withresponseinfo:(nsdictionary *)responseinfo completionhandler:(void(^)())completionhandler ns_available_ios(9_0);

不管具體是本地通知和本地通知,擷取到通知欄使用者輸入的文本内容,隻需要去responseinfo這個字典中取uiusernotificationactionresponsetypedtextkey這個key對應的string就好了。

還是以本地通知為例:

- (void)application:(uiapplication *)application handleactionwithidentifier:(nullable nsstring *)identifier forlocalnotification:(uilocalnotification *)notification withresponseinfo:(nsdictionary *)responseinfo completionhandler:(void(^)())completionhandler { 

 nslog(@"使用者在文本框中輸入的内容:%@",responseinfo [uiusernotificationactionresponsetypedtextkey]);

 completionhandler();

}

具體方式就是這樣了,小夥伴們趕緊試試吧,你可以比手機qq先做出這個功能哦!

繼續閱讀