天天看點

iOS 玩轉推送通知

前言

推送通知,想必大家都很熟悉,關于原理之類的,這裡就不過多闡述。在這裡我們主要介紹下ios8及ios9之後關于推送的新功能。大家可能見過聽說過,但可能有一些朋友并沒有上手做過。這篇文章便給大家詳細介紹推送中的快捷按鈕及快捷回複等功能的實作。

快捷點贊,如微網誌點贊功能

效果圖大家可以先看下:

iOS 玩轉推送通知

首先先說如何為自己的推送添加快捷功能,該demo中的動作 “贊”,代碼如下:

<code>//建立消息上面要添加的動作</code> <code>    </code><code>uimutableusernotificationaction *action1 = [[uimutableusernotificationaction alloc] init];</code> <code>    </code><code>action1.identifier = knotificationactionidentifilestar;</code> <code>    </code><code>action1.title = @</code><code>"贊"</code><code>;</code> <code>    </code><code>//當點選的時候不啟動程式,在背景處理</code> <code>    </code><code>action1.activationmode = uiusernotificationactivationmodebackground;</code> <code>    </code><code>//需要解鎖才能處理(意思就是如果在鎖屏界面收到通知,并且iphone設定了螢幕鎖,點選了贊不會直接進入我們的回調進行處理,而是需要輸入螢幕鎖密碼之後才進入我們的回調),如果action.activationmode = uiusernotificationactivationmodeforeground;則這個屬性被忽略;</code> <code>    </code><code>action1.authenticationrequired = yes;</code> <code>    </code><code>/*</code> <code>     </code><code>destructive屬性設定後,在通知欄或鎖屏界面左劃,按鈕顔色會變為紅色</code>
<code>     </code><code>如果兩個按鈕均設定為yes,則均為紅色(略難看)</code> <code>     </code><code>如果兩個按鈕均設定為no,即預設值,則第一個為藍色,第二個為淺灰色</code> <code>     </code><code>如果一個yes一個no,則都顯示對應的顔色,即紅藍雙色 (cp色)。</code> <code>     </code><code>*/</code> <code>    </code><code>action1.destructive = no;</code>

關于參數的意思,上方代碼中已經有詳細的解釋了,那麼該動作按鈕建立完之後,我們需要的就是建立動作集合,并進行注冊,代碼如下:

<code>//建立動作(按鈕)的類别集合</code> <code>    </code><code>uimutableusernotificationcategory *category = [[uimutableusernotificationcategory alloc] init];</code> <code>    </code><code>//這組動作的唯一标示(knotificationcategoryidentifile為我定義的一個宏,可自行定義)</code> <code>    </code><code>category.identifier = knotificationcategoryidentifile;</code> <code>    </code><code>//最多支援兩個,如果添加更多的話,後面的将被忽略</code> <code>    </code><code>[category setactions:@[action1, action2] forcontext:(uiusernotificationactioncontextminimal)];</code> <code>    </code><code>//建立uiusernotificationsettings,并設定消息的顯示類類型</code> <code>    </code><code>uiusernotificationsettings *uns = [uiusernotificationsettings settingsfortypes:(uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound) categories:[nsset setwithobject:category]];</code> <code>    </code><code>[[uiapplication sharedapplication] registerusernotificationsettings:uns];</code>

到這裡便能實作推送中快捷點贊的功能了。當然我們還會注意到iphone中短信有快捷回複功能,這個是怎麼實作的呢。 客官請往下繼續看。

快捷回複,如短信中下拉快捷回複功能

老規矩,還是先上效果圖,如下:

iOS 玩轉推送通知

這個評論的動作和上面的“贊“隻是多了一個屬性的設定,即”behavior“。代碼如下:

<code>//第二個動作</code> <code>    </code><code>uimutableusernotificationaction *action2 = [[uimutableusernotificationaction alloc] init];</code> <code>    </code><code>action2.identifier = knotificationactionidentifilecomment;</code> <code>    </code><code>action2.title = @</code><code>"評論"</code><code>;</code> <code>    </code><code>action2.activationmode = uiusernotificationactivationmodebackground;</code> <code>    </code><code>//設定了behavior屬性為 uiusernotificationactionbehaviortextinput 的話,則點選了該按鈕會出現輸入框供輸入</code> <code>    </code><code>action2.behavior = uiusernotificationactionbehaviortextinput;</code>

細心的朋友可能注意到我在快捷回複輸入内容時候,輸入框右邊的按鈕名字和短信的快捷回複按鈕名字并不一樣(短信的為”發送“,該demo為”評論“),這個按鈕我們也是可以進行自定義的,代碼如下:

<code>//這個字典定義了當點選了評論按鈕後,輸入框右側的按鈕名稱,如果不設定該字典,則右側按鈕名稱預設為 “發送”</code> <code>    </code><code>action2.parameters = @{uiusernotificationtextinputactionbuttontitlekey: @</code><code>"評論"</code><code>};</code>

到這裡,該demo的基本功能就已經差不多完全實作了。剩下的就是回調了,回調走的函數便是appdelegate中的代理方法了,如下:

<code>// 本地通知回調函數,當應用程式在前台時調用</code> <code>- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification;</code> <code>//在非本app界面時收到本地消息,下拉消息會有快捷回複的按鈕,點選按鈕後調用的方法,根據identifier來判斷點選的哪個按鈕,notification為消息内容</code> <code>- (void)application:(uiapplication *)application handleactionwithidentifier:(nullable nsstring *)identifier forlocalnotification:(uilocalnotification *)notification withresponseinfo:(nsdictionary *)responseinfo completionhandler:(void(^)())completionhandler;</code>

結束語

1:以上代碼是隻注冊了本地推送的,遠端推送和本地推送類似,推送消息如果要觸發快捷動作,則需要在消息本體中含有上面category的identifier标志。如 

<code>//這個"identifile"的需要和你在代碼中設定的knotificationcategoryidentifile保持一緻</code> <code>{</code><code>"aps"</code><code>:{</code><code>"identifile"</code><code>:</code><code>"test remote notification"</code><code>, </code><code>"sound"</code><code>:</code><code>"default"</code><code>, </code><code>"badge"</code><code>: 1, </code><code>"category"</code><code>:</code><code>"category"</code><code>}}</code>

2:在碼代碼時候如果需要相容低版本的話,請注意在代碼中進行版本的判斷。

繼續閱讀