最近在看ios10的通知,也在写一些demo。这篇文章是写的本地通知,先看下效果图:
在ios10中开发者可以使用UserNotifications.framework和UserNotificationsUI.framework来管理使用系统中的通知功能,也可以自定义通知的UI。通知中可以包含音频、图片、视频,更多相关信息查看苹果官方文档
1、先倒入相关框架UserNotifications.framework和UserNotificationsUI.framework
2、在AppDelegate.m导入头文件
#import <UserNotifications/UserNotifications.h>
“`
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
ifdef __IPHONE_10_0
UNAuthorizationOptions options = UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError *error) {
//取得授权
if(granted)
{
//获取权限信息
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings)
{
NSLog(@"权限%d",(int)settings.authorizationStatus);
}];
//创建通知
[self createNotification];
}
//未取得授权
else
{
}
}];
endif
[[UIApplication sharedApplication] registerForRemoteNotifications];
//app未启动的情况下收到通知
id obj;
if((obj = launchOptions[UIApplicationLaunchOptionsLocationKey]))
{
NSLog(@"本地通知 %@", obj);
}
else if((obj = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]))
{
NSLog(@"远程通知 %@", obj);
}
return YES;
}
//创建带有图片的推送
- (void)createNotification
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//创建通知内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.body = [NSString localizedUserNotificationStringForKey:@"帅哥美女云集的偶像剧,大家有木有追着看呢~~" arguments:nil];
content.title = [NSString localizedUserNotificationStringForKey:@"这部超