版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
布局如下:(重點講本地通知)
本地notification所使用的對象是uilocalnotification,uilocalnotification的屬性涵蓋了所有處理notification需要的内容。uilocalnotification的屬性有firedate、timezone、repeatinterval、repeatcalendar、alertbody、 alertaction、hasaction、alertlaunchimage、applicationiconbadgenumber、 soundname和userinfo。
1.首先要明白模拟器和真機的差別:模拟器不會有音頻提示,另外就是沒有檢測允許接受通知,是以我補充一下幾點:
1.添加監測通知:
if ([uiapplication instancesrespondtoselector:@selector(registerusernotificationsettings:)]){
[application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound categories:nil]];
}
上代碼:
#import "viewcontroller.h"
#import "detailviewcontroller.h"
@interface viewcontroller ()
@property (weak, nonatomic) iboutlet uibutton *schedule;
@property (weak, nonatomic) iboutlet uibutton *unschedule;
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
// do any additional setup after loading the view, typically from a nib.
}
// 排程通知
- (ibaction)schedule:(uibutton *)sender {
// 1.建立通知
uilocalnotification *ln = [[uilocalnotification alloc]init];
if (ln) {
// 設定時區
ln.timezone = [nstimezone defaulttimezone];
// 通知第一次發出的時間
ln.firedate = [[nsdate date]datebyaddingtimeinterval:5];
// 2.設定通知屬性
ln.soundname = @"click.wav"; // 音效檔案名
// 通知的具體内容
ln.alertbody = @"重大新聞:小韓哥的部落格又更新了,趕快進來看看吧!....";
// 鎖屏界面顯示的小标題,完整标題:(“滑動來”+小标題)
ln.alertaction = @"檢視新聞吧";
// 設定app圖示數字
ln.applicationiconbadgenumber = 10;
// 設定app的額外資訊
ln.userinfo = @{
@"icon":@"text.png",
@"title":@"重大新聞",
@"time":@"2016-02-28",
@"body":@"重大新聞:小韓哥的部落格又更新了,趕快進來看看吧!"
};
// 設定重新開機圖檔
ln.alertlaunchimage = @"101339g76j7j9t2zgzdvkj.jpg";
// 設定重複發出通知的時間間隔
// ln.repeatinterval = nscalendarunitminute;
// 3.排程通知(啟動任務,在規定的時間發出通知)
[[uiapplication sharedapplication]schedulelocalnotification:ln];
// 直接發出通知沒意義
// [[uiapplication sharedapplication]presentlocalnotificationnow:ln];
}
- (ibaction)noschedule:(uibutton *)sender
{
// [[uiapplication sharedapplication]cancelalllocalnotifications];
// 已經發出且過期的通知會從數組裡自動移除
nsarray *notes = [uiapplication sharedapplication].scheduledlocalnotifications;
nslog(@"%@",notes);
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(uilocalnotification *)note
detailviewcontroller *detailvc = segue.destinationviewcontroller;
detailvc.userinfo = note.userinfo;
2.通知詳情頁面設定基本屬性:
.h
#import <uikit/uikit.h>
@interface detailviewcontroller : uiviewcontroller
@property (nonatomic, strong) nsdictionary *userinfo;
.m
@interface detailviewcontroller ()
@property (weak, nonatomic) iboutlet uilabel *userinfocontent;
@implementation detailviewcontroller
self.userinfocontent.text = self.userinfo[@"body"];
- (void)setuserinfo:(nsdictionary *)userinfo
_userinfo = userinfo;
3.didfinishlaunchingwithoptions 實時監測:
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
//注冊本地通知
if ([uiapplication instancesrespondtoselector:@selector(registerusernotificationsettings:)]){
[application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound categories:nil]];
// nslog(@"-----didfinishlaunchingwithoptions---");
uilabel *label = [[uilabel alloc]init];
label.frame = cgrectmake(0, 64, 320, 100);
label.backgroundcolor = [uicolor redcolor];
label.font = [uifont systemfontofsize:11];
label.numberoflines = 0;
label.textcolor = [uicolor whitecolor];
label.text = [launchoptions description];
[[[self.window.rootviewcontroller.childviewcontrollers firstobject] view]addsubview:label];
uilocalnotification *note = launchoptions[uiapplicationlaunchoptionsurlkey];
if (note) {
label.text = @"點選本地通知啟動的程式";
}else{
label.text = @"直接點選app圖示啟動的程式";
self.label = label;
return yes;
/**
* 當使用者點選本地通知進入app的時候調用(app當時并沒有被關閉)
* 若app已關閉不會被調用此方法
*/
- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification
self.label.text = @"點選通知再次回到前台";
viewcontroller *homevc = [self.window.rootviewcontroller.childviewcontrollers firstobject];
// [homevc performseguewithidentifier:@"tohome" sender:notification];
[homevc performseguewithidentifier:@"tohome" sender:notification];
三種情況展示:(重要)
1.程式運作在背景
原文位址:http://blog.csdn.net/qq_31810357/article/details/50760917