iOS 本地通知

以前一直迷惑,本地推送到底有什么用,后来仔细想了想,确实有用(废话,不然苹果爸爸干嘛开发这个?手动鄙视自己!)

本地通知服务 主要处理基于时间行为的通知。比如定时通知用户该起床撒尿了。

就是这么个用处!嗯,是这样吧?是的!

在iOS10苹果废弃了之前的UILocalNotification,而采用了新的UserNotifications Framework来推送通知。现在先说一下iOS10之前的本地推送流程!

iOS 10之前

注册通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:settings];
    }

    return YES;
}

发送通知

// 1.创建一个本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];

    // 1.1.设置通知发出的时间
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    // 1.2.设置通知内容
    localNote.alertBody = @"这是一个本地推送";

    // 1.3.设置锁屏时,字体下方显示的一个文字
    localNote.alertAction = @"看我";
    localNote.hasAction = YES;

    // 1.4.设置启动图片(通过通知打开的)
    localNote.alertLaunchImage = @"../Documents/1.jpg";

    // 1.5.设置通过到来的声音
    localNote.soundName = UILocalNotificationDefaultSoundName;

    // 1.6.设置应用图标左上角显示的数字
    localNote.applicationIconBadgeNumber = 1;

    // 1.7.设置一些额外的信息
    localNote.userInfo = @{@"hello" : @"how are you", @"msg" : @"success"};

    // 2.执行通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];

这里要说一点,就是iOS系统限制了注册本地推送的数量,最大的注册量为64条。

接收推送

  • 应用在前台或后台,未被杀死时。
//程序处于前台或后台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    NSLog(@"333这里被调用");
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"程序在前台或后台,未被杀死,点击通知栏调用" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    [alert show];
}
  • 程序已被杀死时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
        //添加处理代码

        NSLog(@"666这里被调用");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"程序已被杀死,点击通知栏调用" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
        [alert show];
    }

    return YES;
}

iOS 10之后

先导入这个东西#import <UserNotifications/UserNotifications.h>

注册通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // 使用 UNUserNotificationCenter 来管理通知
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    //监听回调事件
    center.delegate = self;

    //iOS 10 使用以下方法注册,才能得到授权
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              // Enable or disable features based on authorization.
                          }];


    return YES;
}

发送通知

// 使用 UNUserNotificationCenter 来管理通知
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

    //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"本地推送Title" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"本地推送Body"     arguments:nil];
    content.sound = [UNNotificationSound defaultSound];

    // 在 设定时间 后推送本地推送
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:5 repeats:NO];

    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                          content:content trigger:trigger];

    //添加推送成功后的处理!
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

    }];

通知处理

实现UNUserNotificationCenterDelegate代理方法:

  • 第一个方法:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{


    // 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
    completionHandler(UNNotificationPresentationOptionSound);
}

这个方法中的那句话就是,当应用在前台的时候,收到本地通知,是用什么方式来展现。系统给了三种形式:

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
    UNNotificationPresentationOptionBadge   = (1 << 0),
    UNNotificationPresentationOptionSound   = (1 << 1),
    UNNotificationPresentationOptionAlert   = (1 << 2),
} 
  • 第二个代理方法:

这个方法是在后台或者程序被杀死的时候,点击通知栏调用的,在前台的时候不会被调用

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    [alert show];

    completionHandler();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值