天天看點

SWIFT推送之本地推送(UILocalNotification)

本地推送通知是通過執行個體化uilocalnotification實作的。要實作本地化推送可以在appdelegate.swift中添加代碼實作,本事例是一個當app進入背景時推送一條消息給使用者。

1.首先在didfinishlaunchingwithoptions方法内添加代碼,ios8推送消息首先要獲得使用者的同意,在初次安裝app時會提示使用者是否允許程式推送消息,此方法是app第一次運作的時候被執行一次,每次從背景激活時不執行該方法.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<code>func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [</code><code>nsobject</code><code>: anyobject]?) -&gt; bool {</code>

<code>        </code><code>if</code> <code>(uidevice.currentdevice().systemversion as</code><code>nsstring</code><code>).floatvalue &gt;= 8 {</code>

<code>            </code><code>//apservice.registerforremotenotificationtypes(</code>

<code>                </code><code>//uiusernotificationtype.badge.rawvalue |</code>

<code>                </code><code>//uiusernotificationtype.sound.rawvalue |</code>

<code>                </code><code>//uiusernotificationtype.alert.rawvalue,</code>

<code>                </code><code>//categories: nil)</code>

<code>            </code> 

<code>            </code><code>application.registerusernotificationsettings(uiusernotificationsettings(fortypes:</code>

<code>                </code><code>uiusernotificationtype.badge |</code>

<code>                </code><code>uiusernotificationtype.sound |</code>

<code>                </code><code>uiusernotificationtype.alert, categories:</code><code>nil</code><code>))</code>

<code>        </code> 

<code>        </code><code>}</code>

<code>        </code><code>//apservice.setupwithoption(launchoptions)</code>

<code>        </code><code>return</code> <code>true</code>

<code>    </code><code>}</code>

 2.有幾個方法要說一下,

   1.func applicationwillresignactive(application: uiapplication){} 當app既将進入背景、鎖屏、有電話進來時會觸發此事件

   2.func applicationdidenterbackground(application: uiapplication) {} 當app進入背景時觸發此事件

   3.func applicationwillenterforeground(application: uiapplication) {} 當app從背景即将回到前台時觸發此事件

   4.func applicationdidbecomeactive(application: uiapplication) {}當app變成活動狀态時觸發此事件

   5.func applicationwillterminate(application: uiapplication) {} 當app退出時觸發此方法,一般用于儲存某些特定的資料

此時在applicationdidenterbackground方法内寫入以下代碼:

19

20

21

22

23

24

25

26

<code>func applicationdidenterbackground(application: uiapplication) {</code>

<code>        </code><code>// use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.</code>

<code>        </code><code>// if your application supports background execution, this method is called instead of applicationwillterminate: when the user quits.</code>

<code>        </code><code>uiapplication.sharedapplication().cancelalllocalnotifications()</code>

<code>        </code><code>var notification = uilocalnotification()</code>

<code>        </code><code>//notification.firedate = nsdate().datebyaddingtimeinterval(1)</code>

<code>        </code><code>//setting timezone as localtimezone</code>

<code>        </code><code>notification.timezone =</code><code>nstimezone</code><code>.localtimezone()</code>

<code>        </code><code>notification.repeatinterval =</code><code>nscalendarunit</code><code>.calendarunitday</code>

<code>        </code><code>notification.alerttitle =</code><code>"this is a local notification"</code>

<code>        </code><code>notification.alertbody =</code><code>"hey,it's great to see you again"</code>

<code>        </code><code>notification.alertaction =</code><code>"ok"</code>

<code>        </code><code>notification.soundname = uilocalnotificationdefaultsoundname</code>

<code>        </code><code>//setting app's icon badge</code>

<code>        </code><code>notification.applicationiconbadgenumber = 1</code>

<code>        </code><code>var userinfo:[</code><code>nsobject</code> <code>: anyobject] = [</code><code>nsobject</code> <code>: anyobject]()</code>

<code>        </code><code>userinfo[</code><code>"klocalnotificationid"</code><code>] =</code><code>"localnotificationid"</code>

<code>        </code><code>userinfo[</code><code>"key"</code><code>] =</code><code>"attention please"</code>

<code>        </code><code>notification.userinfo = userinfo</code>

<code>        </code><code>//uiapplication.sharedapplication().schedulelocalnotification(notification)</code>

<code>        </code><code>//uiapplication.sharedapplication().presentlocalnotificationnow(notification)</code>

<code>        </code><code>application.presentlocalnotificationnow(notification)</code>

 此時将按home鍵将app切換到背景時會有一條推送消息,app角标變為了“1”

SWIFT推送之本地推送(UILocalNotification)

3.當使用者點選消息時會觸發didreceivelocalnotification事件,在這個事件内寫些代碼:

<code>func application(application: uiapplication, didreceivelocalnotification notification: uilocalnotification) {</code>

<code>        </code><code>let userinfo = notification.userinfo!</code>

<code>        </code><code>let title = userinfo[</code><code>"key"</code><code>] as! string</code>

<code>        </code><code>var alert = uialertview()</code>

<code>        </code><code>alert.title = title</code>

<code>        </code><code>alert.message = notification.alertbody</code>

<code>        </code><code>alert.addbuttonwithtitle(notification.alertaction!)</code>

<code>        </code><code>alert.cancelbuttonindex = 0</code>

<code>        </code><code>alert.show()</code>

<code>        </code><code>//apservice.showlocalnotificationatfront(notification, identifierkey: nil)</code>

SWIFT推送之本地推送(UILocalNotification)

4.當程式處于活動狀态的時候清除icon的角标

<code>func applicationdidbecomeactive(application: uiapplication) {</code>

<code>        </code><code>// restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previously in the background, optionally refresh the user interface.</code>

<code>        </code><code>//setting the desk top application icon's badge as zero</code>

<code>        </code><code>//application.applicationiconbadgenumber = 0</code>

<code>        </code><code>application.cancelalllocalnotifications()</code>

<code>        </code><code>application.applicationiconbadgenumber = 0</code>