iOS 本地通知

来源:互联网 发布:windows装mac双系统 编辑:程序博客网 时间:2024/06/08 12:55

具体代码:

AppDelegate.m:

 

#pragma mark- 接收本地通知的方法

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

//接收到 通知之后的操作

    NSLog(@"--------------%@",notification.userInfo);

    

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:notification.alertTitle message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];

    

}


ViewController.m:


/*

本地通知  UILocalNotification

 

 操作流程:

   1.接收通知

   2.注册 发送通知

 

 

  提示事件   闹钟


  

 

 AppDelegate 里面实现代码


*/

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

  

    [self pushNotfation];

    

}


#pragma mark-注册发送通知的方法

-(void)pushNotfation

{

// 注册  发送通知

//   初始化 alloc init

    UILocalNotification *not = [[UILocalNotification alloc]init];

//   fireDate通知启动的时间

    not.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

//    设置通知的标题

    not.alertTitle = @"起床了";

// 设置通知的内容

    not.alertBody = @"生前何必久睡,死后自会长眠";

//   通过通知 传递内容

    not.userInfo = @{@"key":@"value"};

//   设置App图标上面红点显示的数字

    not.applicationIconBadgeNumber = 1; //在远程通知的时候 是系统提供给我们的

    

//    重复发送通知的方法

    /*

     NSCalendarUnitEra                = kCFCalendarUnitEra,一个世纪

     NSCalendarUnitYear               = kCFCalendarUnitYear,一年

     NSCalendarUnitMonth              = kCFCalendarUnitMonth,一个月

     NSCalendarUnitDay                = kCFCalendarUnitDay,

     NSCalendarUnitHour               = kCFCalendarUnitHour,

     NSCalendarUnitMinute             = kCFCalendarUnitMinute,

     NSCalendarUnitSecond             = kCFCalendarUnitSecond,

     NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,一个礼拜

     NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,

     */

    not.repeatInterval = kCFCalendarUnitDay;

    

    

    

    

// 注册通知

    

//    判断方法 是否可以响应某个方法

//    [self respondsToSelector:<#(SEL)#>]

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        

        [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeNone categories:nil]];

//        UIUserNotificationTypeBadge| 圆圈内提示的数字UIUserNotificationTypeSound| 通知提示的声音UIUserNotificationTypeAlert|震动 UIUserNotificationTypeNone

        

    }

    

    

    

    

    not.soundName = UILocalNotificationDefaultSoundName;

    

    

    

    

//   发送通知

    [[UIApplication sharedApplication]scheduleLocalNotification:not];

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end





0 0
原创粉丝点击