UILocalNotification

一、UILocalNotification相关参数

(1)fireDate,执行本地通知的时间;

(2)soundName,通知声音的名称;

(3)repeatInterval,重复的周期(如kCFCalendarUnitDay,每天通知一次);

(4)timeZone,当地时区;

(5)applicationIconBadgeNumber,app icon上得数字提示;

(6)alertBody,弹出的通知内容;

(7)alertAction,点击动作;


二、创建UILocalNotification

(1)创建一个本地通知,10秒后弹出通知。

//进入后台,创建一个本地通知
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    if (notification) {
       
        notification.repeatInterval = kCFCalendarUnitDay;
        
        notification.timeZone = [NSTimeZone defaultTimeZone];
        NSDate *currentDate = [NSDate date];
        
        NSDate *sinceNow = [currentDate dateByAddingTimeInterval:10];
        notification.fireDate = sinceNow;
        
        notification.applicationIconBadgeNumber = 1;
        
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.alertBody = @"测试通知";
        notification.alertAction = @"打开";
        
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }


(2)在进入前台页面时- (void)applicationDidBecomeActive:,设置appicon的applicationIconBadgeNumber的值为0;

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];


三、IOS8(以上)请求授权

原因是因为在ios8中,设置应用的application badge value需要得到用户的许可。使用如下方法咨询用户是否许可应用设置application badge value,在application: didFinishLaunchingWithOptions:中加入以下代码。

//IOS8以上的系统
    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (systemVersion >= 8.0) {
        
        if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
            
            UIUserNotificationSettings *noteSetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound
                                                                                        categories:nil];
            
            [[UIApplication sharedApplication] registerUserNotificationSettings:noteSetting];
        }
    }

备注:在IOS8 -- iphone6调试报错:

2015-12-14 16:40:50.997 Mannyi1[3932:1764085] Attempting to badge the application icon but haven't received permission from the user to badge the application

原因是:没有用户权限,无法设置badge的值,奇怪的是权限已设置(待研究)。


四、其他

触发通知有两种情况:一种是程序没有运行,另一种是程序已经运行;已经在运行又分为两种情况:后台运行触发和前台运行触发。

(1)我先说明一下程序没有运行的情况,可以在application: didFinishLaunchingWithOptions:函数中判断是否存在UIApplicationLaunchOptionsLocalNotificationKey。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{    

    UILocalNotification *loc = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if(loc!=nil){

        NSDictionary *dic= loc.userInfo//就使本地通知中传递过的NSDictionary,我们可以获取其中的参数。

    }

}

(2)程序已运行,在后台运行通知的时间到了,不会主动触发- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification函数,需要用户点击才会触发;程序在前台运行,会出现这样的一种情况就是:通知时间到了会触发函数,用户在通知之中心点击通知也会触发函数。所以需要区别这两种情况,用到的方法就是:当前时间与触发时间比较在很小的范围内则表示时间到了触发,反之表示在用户在通知中心点击通知触发例如:

if([[formatter dateFromString:[formatter stringFromDate:[NSDate date]]] timeIntervalSinceDate:notification.fireDate] > 0.5) {

                    //用户点击了,

                }else{

               //时间到了触发的

 }


以下的例子是判断激活状态的,application: didReceiveLocalNotification函数;

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    //if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {
    //判断应用程序当前的运行状态,如果是激活状态,则进行提醒,否则不提醒
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil];
        [alert show];
    }
    //}
}


转载于:https://my.oschina.net/u/1450995/blog/544151

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值