iOS10添加本地推送(Local Notification)实例

 更新时间:2016年09月21日 14:44:22   作者:Daizi  
这篇文章主要为大家详细介绍了iOS10添加本地推送(Local Notification)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

iOS 10 中废弃了 UILocalNotification ( UIKit Framework ) 这个类,采用了全新的 UserNotifications Framework 来推送通知,从此推送通知也有了自己的标签 UN (这待遇真是没别人了),以及对推送功能的一系列增强改进(两个 extension 和 界面的体验优化),简直是苹果的亲儿子,因此推送这部分功能也成为开发中的重点。

本文主要查看了 iOS 10 的相关文档,整理出了在 iOS 10 下的本地推送通知,由于都是代码,就不多做讲解,直接看代码及注释,有问题留言讨论哦。

新的推送注册机制

注册通知( Appdelegate.m ):

#import <UserNotifications/UserNotifications.h>
#import "AppDelegate.h"
@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // 使用 UNUserNotificationCenter 来管理通知
 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
 //监听回调事件
 center.delegate = self;
 
 //iOS 10 使用以下方法注册,才能得到授权
 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
       completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Enable or disable features based on authorization.
       }];
 
 //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  
 }];
 return YES;
}

#pragma mark - UNUserNotificationCenterDelegate
//在展示通知前进行处理,即有机会在展示通知前再修改通知内容。
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
 //1. 处理通知
 
 //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
 completionHandler(UNNotificationPresentationOptionAlert);
}
@end

推送本地通知

//使用 UNNotification 本地通知
+(void)registerNotification:(NSInteger )alerTime{
 
 // 使用 UNUserNotificationCenter 来管理通知
 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
 
 //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
 UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
 content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
 content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
 arguments:nil];
 content.sound = [UNNotificationSound defaultSound];
 
 // 在 alertTime 后推送本地推送
 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
 triggerWithTimeInterval:alerTime repeats:NO];

 UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
 content:content trigger:trigger];
 
 //添加推送成功后的处理!
 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  [alert addAction:cancelAction];
  [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
 }];
}

iOS 10 以前本地推送通知:

+ (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {
 // ios8后,需要添加这个注册,才能得到授权
 // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
 // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
 // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
 // categories:nil];
 // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 // // 通知重复提示的单位,可以是天、周、月
 // }
 
 UILocalNotification *notification = [[UILocalNotification alloc] init];
 // 设置触发通知的时间
 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
 NSLog(@"fireDate=%@",fireDate);
 
 notification.fireDate = fireDate;
 // 时区
 notification.timeZone = [NSTimeZone defaultTimeZone];
 // 设置重复的间隔
 notification.repeatInterval = kCFCalendarUnitSecond;
 
 // 通知内容
 notification.alertBody = @"该起床了...";
 notification.applicationIconBadgeNumber = 1;
 // 通知被触发时播放的声音
 notification.soundName = UILocalNotificationDefaultSoundName;
 // 通知参数
 NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];
 notification.userInfo = userDict;
 
 // ios8后,需要添加这个注册,才能得到授权
 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
  UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                     categories:nil];
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  // 通知重复提示的单位,可以是天、周、月
  notification.repeatInterval = NSCalendarUnitDay;
 } else {
  // 通知重复提示的单位,可以是天、周、月
  notification.repeatInterval = NSDayCalendarUnit;
 }
 
 // 执行通知注册
 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

本文已被整理到了《iOS推送教程》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • IOS开发中键盘输入屏幕上移的解决方法

    IOS开发中键盘输入屏幕上移的解决方法

    在IOS开法中经常会遇到键盘遮挡屏幕的事情,经常档住下面的按钮,下面小编给大家分享IOS开发中键盘输入屏幕上移的解决方法,感兴趣的朋友一起看看吧
    2016-10-10
  • iOS中金额字符串格式化显示的方法示例

    iOS中金额字符串格式化显示的方法示例

    这篇文章主要给大家介绍了关于iOS中金额字符串格式化显示的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • iOS自定义控件开发梳理总结

    iOS自定义控件开发梳理总结

    这篇文章主要介绍了iOS自定义控件开发梳理总结,自定义控件能让我们完全控制视图的展示内容以及交互操作。具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • ios 服务器端推送证书生成的方法

    ios 服务器端推送证书生成的方法

    这篇文章主要介绍了ios 服务器端推送证书生成的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • IOS 获取已连接的wifi信息的实现代码

    IOS 获取已连接的wifi信息的实现代码

    这篇文章主要介绍了IOS 获取已连接的wifi信息的实现代码的相关资料,这里提供实现代码帮助大家学习理解这部分内容,需要的朋友可以参考下
    2017-08-08
  • 详解iOS应用开发中使用设计模式中的抽象工厂模式

    详解iOS应用开发中使用设计模式中的抽象工厂模式

    这篇文章主要介绍了iOS应用开发中使用设计模式中的抽象工厂模式,示例代码为传统的Objective-C,需要的朋友可以参考下
    2016-03-03
  • iOS开发中实现hook消息机制的方法探究

    iOS开发中实现hook消息机制的方法探究

    这篇文章主要介绍了iOS开发中实现hook消息机制的方法探究,这里用到了一个Method Swizzling原理,需要的朋友可以参考下
    2015-10-10
  • 判断iOS应用是否开放HTTP权限的方法

    判断iOS应用是否开放HTTP权限的方法

    这篇文章主要为大家详细介绍了判断iOS应用是否开放HTTP权限的方法,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • iOS中CPU线程调试的高级技巧分享

    iOS中CPU线程调试的高级技巧分享

    这篇文章主要给大家介绍了关于iOS中CPU线程调试的高级技巧,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • iOS开发教程之XLForm的基本使用方法

    iOS开发教程之XLForm的基本使用方法

    XLForm 是最灵活且最强大的创建动态表单的iOS库,下面这篇文章主要给大家介绍了关于iOS开发教程之XLForm的基本使用方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2018-04-04

最新评论