国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

本地通知之鬧鐘

2019-11-14 18:30:39
字體:
來源:轉載
供稿:網(wǎng)友

  在現(xiàn)階的APP中關于消息的處理需求越來越大,系統(tǒng)需要將一下消息以音頻或者文字的形式提示用戶,這里便用到推送,推送消息主要有本地和遠程推送,今天我們先研究一下簡單的本地通知,下面以鬧鐘為例子。

1、我們首先要注冊通知

  UIapplication * application=[UIApplication sharedApplication];    //如果當前應用程序沒有注冊本地通知,需要注冊    if([application currentUserNotificationSettings].types==UIUserNotificationTypeNone){                //設置提示支持的提示方式//        UIUserNotificationTypeBadge   提示圖標//        UIUserNotificationTypeSound   提示聲音//        UIUserNotificationTypeAlert   提示彈框        UIUserNotificationSettings * setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];        [application registerUserNotificationSettings:setting];        }    //刪除之前的重復通知    [application cancelAllLocalNotifications];通知注冊完成之后可以在設置里面進行查看,同時也可以刪除。如圖:

 

2、設置通知

#PRagma mark - 添加本地通知- (void) _addLocalNotification:(NSDate *) date{        UILocalNotification * noti=[[UILocalNotification alloc] init];    //設置開始時間    noti.fireDate=date;        //設置body    noti.alertBody=@"該起床了";        //設置action    noti.alertAction=@"解鎖";        //設置鬧鈴    noti.soundName=@"4195.mp3";    #warning 注冊完之后如果不刪除,下次會繼續(xù)存在,即使從模擬器卸載掉也會保留    //注冊通知    [[UIApplication sharedApplication] scheduleLocalNotification:noti];    }

這樣就會在設置的時間內鬧鐘響起來,如圖:

3、這樣鬧鐘的功能基本實現(xiàn),但是還有一個問題,因為如果當前程序是打開的會導致鬧鐘不會響起來,那我們如何解決問題呢。此時我們需要借助播放器來解決

   @interface AppDelegate (){    //定義播放器播放音樂    AVAudioPlayer * player;    //用來判斷是不是從通知窗口打開    BOOL isFromNotification;}- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{     //如果是從通知窗口進來的則不需要播放音頻    if (isFromNotification) {        return;    }        //初始化音樂播放音樂    NSURL * url=[[NSBundle mainBundle] URLForResource:@"4195.mp3" withExtension:nil];    player=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];    player.numberOfLoops=0;        [player prepareToPlay];    [player play];    }

這樣便大功告成了。

 

  想要了解更多內容的小伙伴,可以點擊查看源碼,親自運行測試。

  疑問咨詢或技術交流,請加入官方QQ群:JRedu技術交流 (452379712)

 

作者:杰瑞教育
出處:http://www.survivalescaperooms.com/jerehedu/ 
本文版權歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 罗江县| 龙井市| 芦山县| 汨罗市| 乌什县| 天气| 禄劝| 牙克石市| 郓城县| 若尔盖县| 阳东县| 乌兰县| 休宁县| 永福县| 察哈| 霍州市| 大宁县| 翼城县| 阜新市| 沛县| 崇州市| 紫阳县| 玉林市| 无为县| 乌海市| 恭城| 孙吴县| 凭祥市| 彰化县| 东兰县| 锦屏县| 永平县| 拉萨市| 赤壁市| 亚东县| 平远县| 满城县| 岳阳市| SHOW| 项城市| 霍林郭勒市|