凌雪
2018-10-10
来源 :网络
阅读 1487
评论 0
摘要:本文将带你了解IOS开发入门iOS 本地通知推送 iOS10.0 新API使用总结,希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门iOS 本地通知推送 iOS10.0 新API使用总结,希望本文对大家学IOS有所帮助。
1. 本地通知 iOS10.0
1.1. 发送推送通知
//
// ViewController.m
// iOS10.0本地推送通知
//
// Created by zhouyu on 2017/12/28.
// Copyright ? 2017年 zhouyu. All rights reserved.
//
#import "ViewController.h"
#import <usernotifications usernotifications.h="">
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"ios10.0之后本地通知";
[self setUpUI];
}
//- (void)touchesBegan:(NSSet<uitouch> *)touches withEvent:(UIEvent *)event{
// // 使用 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) {
// NSLog(@"推送成功了");
// }];
//}
- (void)setUpUI{
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 150, 60)];
[btn1 setTitle:@"跳转红色控制器" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(red) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 150, 60)];
[btn2 setTitle:@"跳转蓝色控制器" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(blue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
}
- (void)red{
[self pushUNUserNotificationCenterWithRequestIdentifier:@"red" contentTitle:@"红色控制器" contentBody:@"跳转到红色控制器"];
}
- (void)blue{
[self pushUNUserNotificationCenterWithRequestIdentifier:@"blue" contentTitle:@"蓝色控制器" contentBody:@"跳转到蓝色控制器"];
}
- (void)pushUNUserNotificationCenterWithRequestIdentifier:(NSString *)identifier contentTitle:(NSString *)title contentBody:(NSString *)body{
// 使用 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:identifier
content:content trigger:trigger];
//添加推送成功后的处理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"推送成功了");
}];
}
@end
</uitouch></usernotifications>
1.2. 注册和接收推送通知,并进行逻辑处理
//
// AppDelegate.m
// iOS10.0本地推送通知
//
// Created by zhouyu on 2017/12/28.
// Copyright ? 2017年 zhouyu. All rights reserved.
//
#import "AppDelegate.h"
#import <usernotifications usernotifications.h="">
#import "ViewController.h"
#import "RedController.h"
#import "BlueController.h"
@interface AppDelegate ()<unusernotificationcenterdelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
[self.window makeKeyAndVisible];
self.window.backgroundColor = [UIColor whiteColor];
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//监听回调事件
center.delegate = self;
//iOS 10 使用以下方法注册,才能得到授权
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"error = %@",error);
}];
return YES;
}
/*
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
UNNotificationPresentationOptionBadge = (1 << 0),
UNNotificationPresentationOptionSound = (1 << 1),
UNNotificationPresentationOptionAlert = (1 << 2),
}
*/
//当应用在前台的时候,收到本地通知,是用什么方式来展现。系统给了三种形式:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSLog(@"%@",notification.request);
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号