iOS 本地通知推送 iOS10.0 新API使用總結,iosios10.0

來源:互聯網
上載者:User

iOS 本地通知推送 iOS10.0 新API使用總結,iosios10.0
1. 本地通知 iOS10.01.1. 發送推播通知

////  ViewController.m//  iOS10.0本地推播通知////  Created by zhouyu on 2017/12/28.//  Copyright ? 2017年 zhouyu. All rights reserved.//#import "ViewController.h"#import @interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.title = @"ios10.0之後本地通知";    [self setUpUI];}//- (void)touchesBegan:(NSSet *)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
1.2. 註冊和接收推播通知,並進行邏輯處理
////  AppDelegate.m//  iOS10.0本地推播通知////  Created by zhouyu on 2017/12/28.//  Copyright ? 2017年 zhouyu. All rights reserved.//#import "AppDelegate.h"#import #import "ViewController.h"#import "RedController.h"#import "BlueController.h"@interface AppDelegate ()@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);    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"APP在前台" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];    [alert show];    // 處理完成後條用 completionHandler ,用於指示在前台顯示通知的形式    completionHandler(UNNotificationPresentationOptionSound);}//在後台或者程式被殺死的時候,點擊通知欄調用的,在前台的時候不會被調用- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{    NSLog(@"%@",response);//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"APP在後台或者被殺死後喚醒至前台" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];//    [alert show];    [self jumpToControllerWithUNNotificationResponse:response];    completionHandler();}//判斷跳轉- (void)jumpToControllerWithUNNotificationResponse:(UNNotificationResponse *)response{    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;    if ([response.notification.request.identifier isEqualToString:@"red"]) {        [nav pushViewController:[[RedController alloc] init] animated:YES];    } else {        [nav pushViewController:[[BlueController alloc] init] animated:YES];    }}@end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.