天天看点

iOS中 本地通知/本地通知详解

版权声明:本文为博主原创文章,未经博主允许不得转载。

布局如下:(重点讲本地通知)

本地notification所使用的对象是uilocalnotification,uilocalnotification的属性涵盖了所有处理notification需要的内容。uilocalnotification的属性有firedate、timezone、repeatinterval、repeatcalendar、alertbody、 alertaction、hasaction、alertlaunchimage、applicationiconbadgenumber、 soundname和userinfo。

iOS中 本地通知/本地通知详解

1.首先要明白模拟器和真机的区别:模拟器不会有音频提示,另外就是没有检测允许接受通知,所以我补充一下几点:

1.添加监测通知:

iOS中 本地通知/本地通知详解

if ([uiapplication instancesrespondtoselector:@selector(registerusernotificationsettings:)]){  

       [application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert|uiusernotificationtypebadge|uiusernotificationtypesound categories:nil]];  

   }  

上代码:

iOS中 本地通知/本地通知详解

#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.通知详情页面设置基本属性:

iOS中 本地通知/本地通知详解

.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 实时监测:

iOS中 本地通知/本地通知详解

- (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.程序运行在后台

iOS中 本地通知/本地通知详解
iOS中 本地通知/本地通知详解
iOS中 本地通知/本地通知详解

原文地址:http://blog.csdn.net/qq_31810357/article/details/50760917

继续阅读