IOS實現(xiàn)本地推送

本地推送(UILocalNotification)和遠程推送其目的都是要提醒用戶去做某件事情,其本質(zhì)區(qū)別有兩點:(1)是否需要聯(lián)網(wǎng)?(2)觸發(fā)者是誰?簡單來說,本地推送是由用戶(App使用者)觸發(fā),不需要聯(lián)網(wǎng),類似的比如ToDoList,一對一推送;而遠程推送觸發(fā)者是App運營者,為了宣傳產(chǎn)品提高用戶粘合度,推送形式單對多,需要聯(lián)網(wǎng)。

Step1:iOS8之后推送要求必須注冊App支持的用戶交互類型,注冊代碼和遠程推送注冊代碼相同如下

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge,UIUserNotificationType.Alert,UIUserNotificationType.Sound]categories: [category]))

如果沒有特殊需求(比如使用到UIMutableUserNotificationAction)可以將后面的categories設(shè)置為nil
在介紹下一步之前有必要說一下UILocalNotification的基本屬性便于大家理解使用

屬性名 類型 備注 英文原版
fireDate NSDate? 代表推送的啟動時間 timer-based scheduling
timeZone NSTimeZone? 說明設(shè)置的fireDate在哪個時區(qū) the time zone to interpret fireDate in
repeatInterval NSCalendarUnit 重復(fù)次數(shù) nil
repeatCalendar NSCalendar? 重復(fù)推送時間 nil
alertBody String? 通知內(nèi)容 pass a string or localized string key to show an alert
alertAction String? 解鎖滑動時的事件 used in UIAlert button or 'slide to unlock...' slider in place of unlock
soundName String 消息推送提示聲音 name of resource in app's bundle to play or UILocalNotificationDefaultSoundName
alertLaunchImage String? 啟動圖片,設(shè)置此字段點擊通知時會顯示該圖片 used as the launch image (UILaunchImageFile) when launch button is
alertTitle String? 消息標(biāo)題,默認(rèn)無 defaults to nil. pass a string or localized string key
applicationIconBadgeNumber Int App icon的角標(biāo) 0 means no change. defaults to 0
userInfo [NSObject : AnyObject]? 自定義參數(shù) nil

Step2:添加推送通知

@IBAction func sendLocalNotification(sender: UIButton) {
        
        let localNotification = UILocalNotification();
        //觸發(fā)通知時間
        localNotification.fireDate = NSDate(timeIntervalSinceNow:5);
        //重復(fù)間隔
        //    localNotification.repeatInterval = kCFCalendarUnitMinute;
        localNotification.timeZone = NSTimeZone.defaultTimeZone();
        //通知內(nèi)容
        localNotification.alertBody = "本地消息推送測試";
        localNotification.applicationIconBadgeNumber = 1;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        //通知參數(shù)
        localNotification.userInfo = ["message": "本地消息"];
        
        localNotification.category = "categoryIdentifier";
        
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification);
    }

至此,本地推送已經(jīng)完成,但需要說明的是,如果推送發(fā)生時,App處于Foreground會直接調(diào)用

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

如果App處于Background狀態(tài)時,此時觸發(fā)推送會在通知欄產(chǎn)生一條消息,當(dāng)你點擊推送消息后才會觸發(fā)上面的方法。

如果你想實現(xiàn)特殊的消息推送,比如快速點贊、回復(fù)等,可使用UIMutableUserNotificationAction定義不同的action并指定其唯一的identifier,因為無需打開App可直接將action的activationMode設(shè)置為Background,處理函數(shù)為:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void){
        if identifier == "yourIdentifier"{
            //do something..
        }else{
            //else do...
        }
        completionHandler()
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容