//进行用户权限的申请
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
//在block中会传入布尔值granted,表示用户是否同意
if (granted) {
//如果用户权限申请成功,设置通知中心的代理
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
}
}];
typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) {
//允许更新app上的通知数字
UNAuthorizationOptionBadge = (1 << 0),
//允许通知声音
UNAuthorizationOptionSound = (1 << 1),
//允许通知弹出警告
UNAuthorizationOptionAlert = (1 << 2),
//允许车载设备接收通知
UNAuthorizationOptionCarPlay = (1 << 3),
};
//通知内容类
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
//设置通知请求发送时 app图标上显示的数字
content.badge = @2;
//设置通知的内容
content.body = @"这是iOS10的新通知内容:普通的iOS通知";
//默认的通知提示音
content.sound = [UNNotificationSound defaultSound];
//设置通知的副标题
content.subtitle = @"这里是副标题";
//设置通知的标题
content.title = @"这里是通知的标题";
//设置从通知激活app时的launchImage图片
content.launchImageName = @"lun";
//设置5S之后执行
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
//添加通知请求
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
//系统默认的音效 + (instancetype)defaultSound; //自定义的音频音效 /* 注意,音频文件必须在bundle中或者在Library/Sounds目录下 */ + (instancetype)soundNamed:(NSString *)name __WATCHOS_PROHIBITED;
//创建触发器 在timeInterval秒后触发 可以设置是否循环触发 + (instancetype)triggerWithTimeInterval:(NSTimeInterval)timeInterval repeats:(BOOL)repeats; //获取下次触发的时间点 - (nullable NSDate *)nextTriggerDate;
//创建触发器 设置触发时间 可以设置是否循环触发 + (instancetype)triggerWithDateMatchingComponents:(NSDateComponents *)dateComponents repeats:(BOOL)repeats; //下一次触发的时间点 - (nullable NSDate *)nextTriggerDate;
//地域信息 @property (NS_NONATOMIC_IOSONLY, readonly, copy) CLRegion *region; //创建触发器 + (instancetype)triggerWithRegion:(CLRegion *)region repeats:(BOOL)repeats __WATCHOS_PROHIBITED;
//创建图片附件
UNNotificationAttachment * attach = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttach" URL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"]] options:nil error:nil];
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
//设置附件数组
content.attachments = @[attach];
content.badge = @1;
content.body = @"这是iOS10的新通知内容:普通的iOS通知";
//默认的通知提示音
content.sound = [UNNotificationSound defaultSound];
content.subtitle = @"这里是副标题";
content.title = @"这里是通知的标题";
//设置5S之后执行
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefaultImage" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
//配置附件的类型的键 需要设置为NSString类型的值,如果不设置 则默认从扩展名中推断 extern NSString * const UNNotificationAttachmentOptionsTypeHintKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0); //配置是否隐藏缩略图的键 需要配置为NSNumber 0或者1 extern NSString * const UNNotificationAttachmentOptionsThumbnailHiddenKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0); //配置使用一个标准的矩形来对缩略图进行裁剪,需要配置为CGRectCreateDictionaryRepresentation(CGRect)创建的矩形引用 extern NSString * const UNNotificationAttachmentOptionsThumbnailClippingRectKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0); //使用视频中的某一帧作为缩略图 配置为NSNumber时间 extern NSString * const UNNotificationAttachmentOptionsThumbnailTimeKey __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
//创建用户活动
/*
options参数可选如下:
//需要在解开锁屏下使用
UNNotificationActionOptionAuthenticationRequired
//是否指示有破坏性
UNNotificationActionOptionDestructive
//是否允许活动在后台启动app
UNNotificationActionOptionForeground
//无设置
UNNotificationActionOptionNone
*/
UNTextInputNotificationAction * action = [UNTextInputNotificationAction actionWithIdentifier:@"action" title:@"回复" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"活动" textInputPlaceholder:@"请输入回复内容"];
//创建通知模板
UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryText" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
content.badge = @1;
content.body = @"这是iOS10的新通知内容:普通的iOS通知";
//默认的通知提示音
content.sound = [UNNotificationSound defaultSound];
content.subtitle = @"这里是副标题";
content.title = @"这里是通知的标题";
//设置通知内容对应的模板 需要注意 这里的值要与对应模板id一致
content.categoryIdentifier = @"myNotificationCategoryText";
//设置5S之后执行
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefaultText" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题1" options:UNNotificationActionOptionNone];
UNNotificationAction * action2 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题2" options:UNNotificationActionOptionNone];
UNNotificationAction * action3 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题3" options:UNNotificationActionOptionNone];
UNNotificationAction * action4 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题4" options:UNNotificationActionOptionNone];
UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryBtn" actions:@[action,action2,action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
content.badge = @1;
content.body = @"这是iOS10的新通知内容:普通的iOS通知";
//默认的通知提示音
content.sound = [UNNotificationSound defaultSound];
content.subtitle = @"这里是副标题";
content.title = @"这里是通知的标题";
content.categoryIdentifier = @"myNotificationCategoryBtn";
//设置5S之后执行
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
//接收到通知时会被调用
/*
开发者可以从notification对象中拿到附件等内容进行UI刷新
*/
- (void)didReceiveNotification:(UNNotification *)notification;
//当用户点击了通知中的用户交互按钮时会被调用
/*
response对象中有通知内容相关信息
在回调block块completion中,开发者可以传入一个UNNotificationContentExtensionResponseOption参数来告诉系统如何处理这次用户活动
UNNotificationContentExtensionResponseOption枚举中可选值如下:
typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionResponseOption) {
//不关闭当前通知界面
UNNotificationContentExtensionResponseOptionDoNotDismiss,
//关闭当前通知界面
UNNotificationContentExtensionResponseOptionDismiss,
//关闭当前通知界面并将用户活动传递给宿主app处理
UNNotificationContentExtensionResponseOptionDismissAndForwardAction,
} __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
*/
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion;
/*
这个属性作为get方法进行实现 这个方法用来返回一个通知界面要显示的媒体按钮
typedef NS_ENUM(NSUInteger, UNNotificationContentExtensionMediaPlayPauseButtonType) {
//不显示媒体按钮
UNNotificationContentExtensionMediaPlayPauseButtonTypeNone,
//默认的媒体按钮 当点击按钮后 进行播放与暂停的切换 按钮始终显示
UNNotificationContentExtensionMediaPlayPauseButtonTypeDefault,
//Overlay风格 当点击按钮后,媒体播放,按钮隐藏 点击媒体后,播放暂停,按钮显示。
UNNotificationContentExtensionMediaPlayPauseButtonTypeOverlay,
} __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
*/
@property (nonatomic, readonly, assign) UNNotificationContentExtensionMediaPlayPauseButtonType mediaPlayPauseButtonType;
//返回媒体按钮的位置
@property (nonatomic, readonly, assign) CGRect mediaPlayPauseButtonFrame;
//返回媒体按钮的颜色
@property (nonatomic, readonly, copy) UIColor *mediaPlayPauseButtonTintColor;
//点击播放按钮的回调
- (void)mediaPlay;
//点击暂停按钮的回调
- (void)mediaPause;
//媒体开始播放的回调
- (void)mediaPlayingStarted __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
//媒体开始暂停的回调
- (void)mediaPlayingPaused __IOS_AVAILABLE(10_0) __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __OSX_UNAVAILABLE;
UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题1" options:UNNotificationActionOptionNone];
//根据id拿到自定义UI的模板
UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryH" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
content.badge = @1;
content.body = @"这是iOS10的新通知内容:普通的iOS通知";
//默认的通知提示音
content.sound = [UNNotificationSound defaultSound];
content.subtitle = @"这里是副标题";
content.title = @"这里是通知的标题";
content.categoryIdentifier = @"myNotificationCategoryH";
//设置5S之后执行
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefaultCustomUIH" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
/*
这个方法在应用在前台,并且将要弹出通知时被调用,后台状态下弹通知不会调用这个方法
这个方法中的block块completionHandler()可以传入一个UNNotificationPresentationOptions类型的枚举
有个这个参数,开发者可以设置在前台状态下,依然可以弹出通知消息,枚举如下:
typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
//只修改app图标的消息数
UNNotificationPresentationOptionBadge = (1 << 0),
//只提示通知音效
UNNotificationPresentationOptionSound = (1 << 1),
//只弹出通知框
UNNotificationPresentationOptionAlert = (1 << 2),
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
//什么都不做
static const UNNotificationPresentationOptions UNNotificationPresentationOptionNone
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
/*
这个方法当接收到通知后,用户点击通知激活app时被调用,无论前台还是后台
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
typedef NS_ENUM(NSInteger, UNErrorCode) {
//通知不被允许
UNErrorCodeNotificationsNotAllowed = 1,
//附件无效url
UNErrorCodeAttachmentInvalidURL = 100,
//附件类型错误
UNErrorCodeAttachmentUnrecognizedType,
//附件大小错误
UNErrorCodeAttachmentInvalidFileSize,
//附件数据错误
UNErrorCodeAttachmentNotInDataStore,
UNErrorCodeAttachmentMoveIntoDataStoreFailed,
UNErrorCodeAttachmentCorrupt,
//时间无效
UNErrorCodeNotificationInvalidNoDate = 1400,
//无内容
UNErrorCodeNotificationInvalidNoContent,
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
@interface UNNotification : NSObject <NSCopying, NSSecureCoding> //触发的时间 @property (nonatomic, readonly, copy) NSDate *date; //内置的通知请求对象 @property (nonatomic, readonly, copy) UNNotificationRequest *request; - (instancetype)init NS_UNAVAILABLE; @end
@interface UNNotificationSettings : NSObject <NSCopying, NSSecureCoding> //用户权限状态 @property (NS_NONATOMIC_IOSONLY, readonly) UNAuthorizationStatus authorizationStatus; //音效设置 @property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting soundSetting __TVOS_PROHIBITED; //图标提醒设置 @property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting badgeSetting __WATCHOS_PROHIBITED; //提醒框设置 @property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting alertSetting __TVOS_PROHIBITED; //通知中心设置 @property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting notificationCenterSetting __TVOS_PROHIBITED; //锁屏设置 @property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting lockScreenSetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED; //车载设备设置 @property (NS_NONATOMIC_IOSONLY, readonly) UNNotificationSetting carPlaySetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED; //提醒框风格 @property (NS_NONATOMIC_IOSONLY, readonly) UNAlertStyle alertStyle __TVOS_PROHIBITED __WATCHOS_PROHIBITED; @end
typedef NS_ENUM(NSInteger, UNNotificationSetting) {
//不支持
UNNotificationSettingNotSupported = 0,
//不可用
UNNotificationSettingDisabled,
//可用
UNNotificationSettingEnabled,
}
typedef NS_ENUM(NSInteger, UNAuthorizationStatus) {
//为做选择
UNAuthorizationStatusNotDetermined = 0,
// 用户拒绝
UNAuthorizationStatusDenied,
// 用户允许
UNAuthorizationStatusAuthorized
}
typedef NS_ENUM(NSInteger, UNAlertStyle) {
//无
UNAlertStyleNone = 0,
//顶部Banner样式
UNAlertStyleBanner,
//警告框样式
UNAlertStyleAlert,
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有