美文网首页推送程序员iOS 开发
iOS --- 本地推送通知 UILocalNotificati

iOS --- 本地推送通知 UILocalNotificati

作者: icetime17 | 来源:发表于2016-05-24 07:17 被阅读763次

本地推送UILocalNotification常用于定期提醒用户使用该APP,如AirBrush的定期提醒用户拍照,运动锻炼工具的每天锻炼提醒。
不同于远程推送RemoteNotification,本地推送一般较固定,通常事先设置好推送周期。而推送内容往往也是固定的,可存放于plist文件中。本地推送并不依赖于网络连接,可简单将其视为一个定时装置即可。

申请通知权限

若要通知生效,则先要为该APP申请通知权限。一般可在AppDelegate的application:didFinishLaunchingWithOptions方法中添加如下代码:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { 
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}

UILocalNotification对象

添加本地推送的步骤如下:

UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) { 
    NSLog(@">> 10s' local notification"); 
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    notification.timeZone = [NSTimeZone defaultTimeZone]; 
    notification.alertTitle = @"AlertTitle 1"; 
    notification.alertBody = @"AlertBody 1"; 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"notification_1", @"id", nil]; 
    notification.userInfo = infoDict; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

UILocalNotification对象的参数比较多,但作用都很明确。
fireDate设置触发通知接收方法的时间,
alertTitle和alertBody可设置相应的通知内容。
注意userInfo可用于在该UILocalNotification对象中传递一些参数,userInfo必须是NSDictionary类型。

application:didReceiveLocalNotification:方法

该方法用于接收到本地通知时的回调方法:

// APP运行中收到notification时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    // 可根据notification对象的userInfo等属性进行相应判断和处理
}

多个本地通知

一般的工具型APP会包含多个本地通知,分别设置不同的fireDate。如三天,7天,一个月分别推送一次,以唤醒用户。若一个月之内打开APP,则所有本地通知重置。
这里以设置1Min和3Min的本地通知为例:
可在AppDelegate的applicationDidBecomeActive:方法中设置如下,则每次APP启动即执行:

-(void)applicationDidBecomeActive:(UIApplication *)application { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.  
    // appIcon上的消息提示个数 
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;  
    // 取消所有的本地通知 
    [[UIApplication sharedApplication] cancelAllLocalNotifications];  
    // 添加1Mins和3Mins的localNotification

#define kLocalNotificationTimeInterval_1Mins (60*1)
#define kLocalNotificationTimeInterval_3Mins (60*3) 

    [self setLocalNotification:kLocalNotificationTimeInterval_1Mins]; 
    [self setLocalNotification:kLocalNotificationTimeInterval_3Mins];
}

setLocalNotification:方法接收本地通知的时间参数,如下:

-(void)setLocalNotification:(NSTimeInterval)timeInterval { 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    if (notification) { 
        // 设置提醒时间为20:00 
        //NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
        //NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]]; 
        //dateComponents.hour = 20; 
        //dateComponents.minute = 0; 
        //NSDate *fireDate = [calendar dateFromComponents:dateComponents];  
        NSDate *fireDate = [NSDate date];  
        notification.fireDate = [fireDate dateByAddingTimeInterval:timeInterval]; 
        notification.timeZone = [NSTimeZone defaultTimeZone]; 
        notification.alertBody = [NSString stringWithFormat:@"Local Notification %f", timeInterval]; 
        notification.applicationIconBadgeNumber = 1; 

#define LocalNotificationPeriod_1Mins @"LocalNotificationPeriod_1Mins"
#define LocalNotificationPeriod_3Mins @"LocalNotificationPeriod_3Mins"  
    
        NSString *period; 
        if (timeInterval == kLocalNotificationTimeInterval_1Mins) { 
            period = LocalNotificationPeriod_1Mins; 
        } else { 
            period = LocalNotificationPeriod_3Mins; 
        }  
        notification.userInfo = @{ @"id": period, }; 
        [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    }
}

这里,简单介绍了UILocalNotification的常见使用方式,往往用于固定周期内唤醒用户。

DemoDemo地址: DemoLocalNotification

相关文章

  • iOS --- 本地推送通知 UILocalNotificati

    本地推送UILocalNotification常用于定期提醒用户使用该APP,如AirBrush的定期提醒用户拍照...

  • iOS 通知机制总结

    iOS中提供了2种推送通知 本地推送通知(Local Notification) 远程推送通知(Remote No...

  • iOS-消息推送

    iOS 消息推送包括远程推送通知(Remote Notification)和本地推送通知(Local Notifi...

  • iOS 推送通知及通知扩展

    级别: ★★☆☆☆标签:「iOS 本地推送」「iOS 远程推送」「iOS通知扩展」作者: dac_1033审校: ...

  • 26 - 推送

    iOS中提供了2种推送通知: 本地推送通知(Local Notification) 远程推送通知(Remote N...

  • iOS推送通知概览

    iOS推送通知概览 一、响应推送(本地通知 和 远程通知 都合适) iOS 10 以前 1. UIUserNoti...

  • iOS本地通知和远程推送

    iOS 本地通知和远程推送 推送通知的应用,可以推送最新的消息给用户,获得更多的关注。推送分为本地推送和远程推送两...

  • iOS 本地推送通知

    本地推送通知 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。 a...

  • UILocalNotification

    iOS提供两种推送机制: 本地推送通知(Local Notification)与远程推送通知(Remote Not...

  • 技术贴:5.iOS中的推送(本地推送、极光推送)

    iOS中分为本地推送和远程推送两种; 本地推送: 1.创建一个本地通知(UILocalNotification)(...

网友评论

    本文标题:iOS --- 本地推送通知 UILocalNotificati

    本文链接:https://www.haomeiwen.com/subject/ecxzrttx.html