iOS 10本地推送
来源:互联网 发布:儿童编程软件下载 编辑:程序博客网 时间:2024/06/05 20:36
iOS 10本地推送
import
import “AppDelegate.h”
@interface AppDelegate ()
@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 10本地推送
- iOS 10本地推送
- iOS 10 本地推送UNUserNotificationCenter
- iOS-本地推送之UserNotifications(iOS 10)
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- ios (推送)之本地推送
- iOS推送:实现本地推送
- ios (推送)之本地推送
- iOS推送之本地推送
- ios推送之本地推送
- ios本地推送
- iOS本地推送
- iOS 本地推送
- ios本地推送方法
- IOS本地推送处理
- 第28个python程序:布尔表达式练习
- Android开发——Android手机屏幕适配方案总结
- Android WebView开发问题汇总
- ListView和GridView列表滑动卡顿问题的优化方法汇总
- JavaScript趣题:质数之差
- iOS 10本地推送
- jquery总结知识点
- 前言---我最喜欢的宣老师语录
- 一个简单的单点登录构思
- redis加入到Windows 服务
- JVM垃圾回收机制
- Linux查看并释放占用端口
- 触发Full GC执行的情况
- 数据结构 -- 链栈java实现