iOS本地推送

来源:互联网 发布:蘑菇云刷机软件 编辑:程序博客网 时间:2024/06/05 02:54

本地推送介绍

本地推送和远程推送的功能是一样的,都是要提醒用户去做某些事情。但是和远程推送不同的就是本地推送是不需要设备联网的,而远程推送是必需要设备联网的,因为只有联网状态下,才能和苹果的APNs服务器建立长连接,从而推送消息。本地推送是由App自己设定的,并且发送给安装此App的这台设备,属于一对一的对应关系。
本地推送适合日历 to-do list等类型的App,注意:一个App最多只能设置64个本地推送,当超过此限制的时候,系统会自动忽略多余的本地推送,而保留能最快触发的64个。循环的本地推送会被系统认为是同一个本地推送。

本地推送应用

iOS8本地推送注册

iOS8之后推送要求必须注册App支持的用户交互类型,注册代码和远程推送注册代码相同如下
// iOS8注册本地通知类型
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

基本应用

UILocalNotification的基本属性

fireDate:启动时间
timeZone:启动时间参考的时区
repeatInterval:重复推送时间(NSCalendarUnit类型),0代表不重复
repeatCalendar:重复推送时间(NSCalendar类型)
alertBody:通知内容
alertAction:解锁滑动时的事件
alertLaunchImage:启动图片,设置此字段点击通知时会显示该图片
alertTitle:通知标题,适用iOS8.2之后
applicationIconBadgeNumber:收到通知时App icon的角标
soundName:推送是带的声音提醒,设置默认的字段为UILocalNotificationDefaultSoundName
userInfo:发送通知时附加的内容
category:此属性和注册通知类型时有关联,(有兴趣的同学自己了解,不详细叙述)适用iOS8.0之后

region:带有定位的推送相关属性,具体使用见下面【带有定位的本地推送】适用iOS8.0之后
regionTriggersOnce:带有定位的推送相关属性,具体使用见下面【带有定位的本地推送】适用iOS8.0之后

  • (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:item.day];
    [dateComps setMonth:item.month];
    [dateComps setYear:item.year];
    [dateComps setHour:item.hour];
    [dateComps setMinute:item.minute];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil) return;

    localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@”%@ in %i minutes.”, nil),
    item.eventName, minutesBefore];
    localNotif.alertAction = NSLocalizedString(@”View Details”, nil);
    localNotif.alertTitle = NSLocalizedString(@”Item Due”, nil);
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
    localNotif.userInfo = infoDict;

    // 设置好本地推送后必须调用此方法启动此推送
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }

取消本地推送的方法
// 取消某一个本地推送
[[UIApplication sharedApplication] cancelLocalNotification:notification];
// 取消所有的本地推送
[[UIApplication sharedApplication] cancelAllLocalNotifications];

收到本地通知时的回调方法

application: didFinishLaunchingWithOptions:
此方法在程序第一次启动是调用,也就是说App从Terminate状态进入Foreground状态的时候,根据方法内代码判断是否有推送消息。

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    // userInfo为收到远程通知的内容
    NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if (userInfo) {
    // 有推送的消息,处理推送的消息
    }
    return YES;
    }

application: didReceiveLocalNotification:
如果App处于Background状态时,只用用户点击了通知消息时才会调用该方法;如果App处于Foreground状态,会直接调用该方法。
- (void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification {

}

带有定位的本地推送
iOS8之后,本地推送可以创建带有定位的本地推送,当用户进入这一指定的区域的时候就会发送此推送。并且可以指定此消息是只发送一次,还是每次用户进入此区域的时候都发送此推送。设置此属性对应的属性字段为:regionTriggersOnce。

开启定位服务

注册带有定位的推送必要要求用户开启定位功能,授权使用定位服务
// 获得授权去追踪用户的位置
CLLocationManager *locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
[locManager requestWhenInUseAuthorization];

当你第一次请求使用授权定位服务的时候,系统会询问用户是同意还是拒绝。为了提醒用户,系统会显示一些在Info.plist中NSLocationWhenInUseUsageDescription此key值对应的额外信息。使用定位服务此key值一定要配置。只有用户同意了此App访问他的位置信息,此App才会接收到回到方法。
处理定位回调

首先要检查用户对定位服务的授权状态,只有用户授权了可以访问用户的位置信息,才会回调以下方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
//检查状态判断App是否授权
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// 设置通知
[self startShowingLocationNotifications];
}
}
// 设置通知
- (void)startShowingLocationNotifications {

UILocalNotification *locNotification = [[UILocalNotification alloc] init];locNotification.alertBody = @"You have arrived!";locNotification.regionTriggersOnce = YES;locNotification.region = [[CLCircularRegion alloc] initWithCenter:LOC_COORDINATE radius:LOC_RADIUS identifier:LOC_IDENTIFIER];[[UIApplication sharedApplication] scheduleLocalNotification:locNotification];

}
// 收到本地通知的时候,回调此方法,处理通知
- (void)application:(UIApplication )application didReceiveLocalNotification: (UILocalNotification )notification {

if (notification.region) {       [self tellUserArrivedAtRegion:region];}

}

0 0
原创粉丝点击