码迷,mamicode.com
首页 > 其他好文 > 详细

本地通知 LocalNotification

时间:2015-12-23 22:37:31      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

本地通知 LocalNotification 

在Appdelegate中实现以下两个方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //设置桌面红点内的数字
//    application.applicationIconBadgeNumber = 0;
    
    //请求通知权限
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil];
    [application registerUserNotificationSettings:settings];
    
    return YES;
}

收到本地通知的代理方法

1.点击本地通知弹框

2.程序在前台, 收到本地通知

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"%@", notification);
}

添加一个button到ViewController中

#import "ViewController.h"

@interface ViewController ()
- (IBAction)localNoti:(id)sender;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad]; 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)localNoti:(id)sender {
    //创建本地通知
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    //提示框内容
    notification.alertBody = @"起床啦";
    //提示框标题
    notification.alertTitle = @"醒啦";
    //通知声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    //触发时间
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    //icon旁边的数字
    notification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
    //本地通知重复
    notification.repeatInterval = NSCalendarUnitMinute;
    //发送通知
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
@end

cmd + shift + H 退出程序, 本地通知就弹出

技术分享 

 

本地通知 LocalNotification

标签:

原文地址:http://www.cnblogs.com/OrangesChen/p/5071403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!