iOS10通知及通知拓展Extension使用详解
来源:互联网 发布:新东方英语软件下载 编辑:程序博客网 时间:2024/04/28 12:02
1.1-iOS10拓展简介
1.2-iOS10通知使用
#import "AppDelegate.h"#import <UserNotifications/UserNotifications.h>#import <UserNotificationsUI/UserNotificationsUI.h>@interface AppDelegate ()<UNUserNotificationCenterDelegate>@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center setNotificationCategories:[NSSet setWithObjects:[self createCatrgory], nil]]; [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { NSLog(@"授权成功"); } }]; return YES;}- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 500)]; webview.center = self.window.center; [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.itheima.com"]]]; [self.window addSubview:webview]; webview.alpha = 0; [UIView animateWithDuration:1 animations:^{ webview.alpha = 1; }];}#pragma mark - 创建通知分类(交互按钮)- (UNNotificationCategory *)createCatrgory{ UNTextInputNotificationAction *textInputAction = [UNTextInputNotificationAction actionWithIdentifier:@"textInputAction" title:@"请输入信息" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"输入" textInputPlaceholder:@"还有多少话要说……"]; UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"foreGround" title:@"打开" options:UNNotificationActionOptionForeground]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"backGround" title:@"关闭" options:UNNotificationActionOptionDestructive]; UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[textInputAction,action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; return category;}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ if ([response.actionIdentifier isEqualToString:@"textInputAction"]) { UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse *)response; NSLog(@"输入的内容为:%@",textResponse.userText); } NSLog(@"%@",response.actionIdentifier);}- (void)applicationWillResignActive:(UIApplication *)application { }- (void)applicationDidEnterBackground:(UIApplication *)application { }- (void)applicationWillEnterForeground:(UIApplication *)application { }- (void)applicationDidBecomeActive:(UIApplication *)application { }- (void)applicationWillTerminate:(UIApplication *)application { }@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
#pragma mark - 发送本地通知- (IBAction)sendLocalNotification:(id)sender { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"当前授权状态:%zd",[settings authorizationStatus]); UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init]; notification.title = [NSString localizedUserNotificationStringForKey:@"传智播客" arguments:nil]; notification.subtitle = @"hellow world"; notification.body = @"欢迎来到黑马程序员"; notification.sound = [UNNotificationSound defaultSound]; notification.badge = @2; /**通知触发器主要有三种 UNTimeIntervalNotificationTrigger 指定时间触发 UNCalendarNotificationTrigger 指定日历时间触发 UNLocationNotificationTrigger 指定区域触发 */ UNTimeIntervalNotificationTrigger * timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; notification.categoryIdentifier = @"category"; NSString *path = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]; UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:[NSURL fileURLWithPath:path] options:nil error:nil]; notification.attachments = @[attachment]; /** Identifier:通知请求标识符,用于删除或者查找通知 content:通知的内容 trigger:通知触发器 */ UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"localNotification" content:notification trigger:timeTrigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error == nil) { NSLog(@"通知发送成功"); } else { NSLog(@"%@",error); } }]; }];}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
#pragma mark - 移除所有通知- (IBAction)removeAllNotification:(id)sender { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllDeliveredNotifications]; [center removeAllPendingNotificationRequests];}#pragma mark - 移除指定通知- (IBAction)removeSingleNotification:(id)sender { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]]; [center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]];}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
1.3-iOS10通知拓展Extension使用
- 2.通知的拓展Extension实际上相当于在当前的应用程序重新添加一个应用程序,工程会添加对应的代码文件夹和target
- 3.默认拓展控制器只有一个Label,我们可以在这里自定义我们的控制器界面
- 5.配置plist文件
- 默认情况下应用程序是不会加载拓展界面的,需要配置plist文件,关闭系统默认通知界面
- 6.运行
- 运行的话不需要选择
Extension
的target,直接选择应用程序的target即可
1.4-效果演示
0 0