通知提醒 - iOS開發(fā)

快速回復(fù)

前言

通過(guò)這篇文章你會(huì)學(xué)到:

  • 弄出本地通知
  • 弄出有選項(xiàng)操作的通知
  • 類似微信快速回復(fù)

本地通知


我寫成了函數(shù)的形式方便設(shè)置

func setNotification(body:String,timeToNotification:Double,soundName:String,category:String) -> UILocalNotification {
    let localNotification:UILocalNotification = UILocalNotification()
    localNotification.alertAction = "滑動(dòng)查看信息"
    localNotification.alertBody = body
    
//在mainBundle的短于30秒的播放文件,否則就會(huì)是系統(tǒng)默認(rèn)聲音
    localNotification.soundName = soundName
    localNotification.fireDate = NSDate(timeIntervalSinceNow: timeToNotification)
    
//下面這條category語(yǔ)句是對(duì)應(yīng)下面的“有選項(xiàng)的本地操作”章節(jié)
    localNotification.category = category

    return localNotification
}

在appDelegate里填下

//請(qǐng)求通知權(quán)限
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
            application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound , .Alert , .Badge], categories: nil))

}

使用時(shí):

//設(shè)置10秒的提醒
let completeNotification = setNotification("時(shí)間到了身腻,已完成任務(wù)",timeToNotification: 10,soundName: "提醒音樂(lè).mp3",category: nil)

UIApplication.sharedApplication().scheduleLocalNotification(completeNotification)

10秒后

通知

有選項(xiàng)的本地操作


效果如圖:


側(cè)滑出選項(xiàng)

設(shè)置Notification Action 和 Category


        //MARK: - 提醒操作 設(shè)置
        let notificationActionOk : UIMutableUserNotificationAction = UIMutableUserNotificationAction()
        notificationActionOk.identifier = "completeRemindRater"
        notificationActionOk.title = "再工作一會(huì)兒"
//是否取消提醒
        notificationActionOk.destructive = false
//是否需要權(quán)限埠巨,例如鎖屏的時(shí)候戚炫,執(zhí)行操作是否需要解鎖再執(zhí)行
        notificationActionOk.authenticationRequired = false
//啟動(dòng)app還是后臺(tái)執(zhí)行
        notificationActionOk.activationMode = UIUserNotificationActivationMode.Background
        
        let notificationActionRestNow: UIMutableUserNotificationAction = UIMutableUserNotificationAction()
        notificationActionRestNow.identifier = "relaxNow"
        notificationActionRestNow.title = "休息"
        notificationActionRestNow.destructive = false
        notificationActionRestNow.authenticationRequired = false
        notificationActionRestNow.activationMode = UIUserNotificationActivationMode.Background
        
        
        //MARK: -Notification Category 設(shè)置
        let notificationCompleteCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
        //記住這個(gè)identifier 突硝,待會(huì)用
        notificationCompleteCategory.identifier = "COMPLETE_CATEGORY"
        notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Default)
        notificationCompleteCategory.setActions([notificationActionOk,notificationActionRestNow], forContext: .Minimal)
        
        //請(qǐng)求用戶允許通知 
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound , .Alert , .Badge], categories: NSSet(array: [notificationCompleteCategory]) as? Set<UIUserNotificationCategory>))

接著在使用時(shí)预烙,則將category參數(shù)的nil改為"COMPLETE_CATEGORY"

//跟之前的一樣,10秒后出通知币他,但category不同
let completeNotification = setNotification("時(shí)間到了鳍置,已完成任務(wù)",timeToNotification: 10,soundName: "提醒鈴聲.mp3",category: "COMPLETE_CATEGORY")

UIApplication.sharedApplication().scheduleLocalNotification(completeNotification)

在appDelegate使用下面method處理按下的選項(xiàng)

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
    print("按下的選項(xiàng)的identifier是:\\\\(identifier)")
}

按下休息時(shí)登疗,終端顯示按下的選項(xiàng)的identifier是:relaxNow

信息下滑有選項(xiàng)

通知快速回復(fù)


實(shí)現(xiàn)類似微信的快速回復(fù)
效果如圖:

實(shí)現(xiàn)基本同上面的選項(xiàng)通知一樣赂韵,就是創(chuàng)建時(shí)修改 action的behavior.TextInput:

        let replyAction = UIMutableUserNotificationAction()
        replyAction.title = "回復(fù)"
        replyAction.identifier = "inline-reply"
        replyAction.activationMode = .Background
        replyAction.authenticationRequired = false
        replyAction.behavior = .TextInput

接著像設(shè)置有選項(xiàng)的操作那樣設(shè)置Category

        let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
        notificationCategory.identifier = "REPLY_CATEGORY"
        notificationCategory.setActions([replyAction], forContext: UIUserNotificationActionContext.Default)
  
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound , .Alert , .Badge], categories: NSSet(array: [notificationCategory]) as? Set<UIUserNotificationCategory>))

設(shè)置提醒娱节,使用本地操作時(shí)那個(gè)函數(shù)setNotification

        let replyNotification = setNotification("說(shuō)些什么吧", timeToNotification: 10, soundName: "提醒音樂(lè).mp3" , category: "REPLY_CATEGORY")
        UIApplication.sharedApplication().scheduleLocalNotification(replyNotification)

快速回復(fù)

處理回復(fù):則在appDelegate下添加:

    func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
        if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey]{
            
            let responseText = response as! String
                print("\(responseText)")
        }
        completionHandler()
    }

在快速回復(fù)如上圖打下Hello,world!,終端就會(huì)出現(xiàn):

終端

一些tips


//取消本地通知
UIApplication.sharedApplication().cancelAllLocalNotifications()
//修改App Icon上的數(shù)字如下圖中右上角的數(shù)字
UIApplication.sharedApplication().applicationIconBadgeNumber = 1
icon

最后


通知的實(shí)現(xiàn)基本就如此了祭示。

(END and Thank U)


參考鏈接:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末肄满,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子质涛,更是在濱河造成了極大的恐慌稠歉,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件汇陆,死亡現(xiàn)場(chǎng)離奇詭異怒炸,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)毡代,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門阅羹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)勺疼,“玉大人,你說(shuō)我怎么就攤上這事捏鱼』挚冢” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵穷躁,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我因妇,道長(zhǎng)问潭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任婚被,我火速辦了婚禮狡忙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘址芯。我一直安慰自己灾茁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布谷炸。 她就那樣靜靜地躺著北专,像睡著了一般。 火紅的嫁衣襯著肌膚如雪旬陡。 梳的紋絲不亂的頭發(fā)上拓颓,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音描孟,去河邊找鬼驶睦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛匿醒,可吹牛的內(nèi)容都是我干的场航。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼廉羔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼溉痢!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起蜜另,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤适室,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后举瑰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體捣辆,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年此迅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了汽畴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旧巾。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖忍些,靈堂內(nèi)的尸體忽然破棺而出鲁猩,到底是詐尸還是另有隱情,我是刑警寧澤罢坝,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布廓握,位于F島的核電站,受9級(jí)特大地震影響嘁酿,放射性物質(zhì)發(fā)生泄漏隙券。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一闹司、第九天 我趴在偏房一處隱蔽的房頂上張望娱仔。 院中可真熱鬧,春花似錦游桩、人聲如沸牲迫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)盹憎。三九已至,卻和暖如春谓娃,著一層夾襖步出監(jiān)牢的瞬間脚乡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工滨达, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奶稠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓捡遍,卻偏偏與公主長(zhǎng)得像锌订,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子画株,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

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