iOS- 实现APP前台、后台、甚至杀死进程下收到通知后进行语音播报(金额)
https://www.jianshu.com/p/601426a247e6
iOS10通知(三)--通知的取消和修改
https://blog.csdn.net/xttxqjfg/article/details/68926335
在创建通知时,我们可以指定标识符。这个标识符可以用来管理通知。
在 iOS 10 之前,我们很难取消掉某一个特定的通知,也不能主动移除或者更新已经展示的通知。
iOS 10 中,UserNotifications 框架提供了一系列管理通知的 API,你可以做到
1、取消还未展示的通知
2、修改还未展示的通知
3、删除已经展示过的通知
4、修改已经展示过的通知
其中关键就在于在创建请求时使用同样的标识符。
取消和修改目前还不能用于远程推送,我还没有查找到相关可以实现的资料,如有了解的可以留言告诉我
详细的代码实现如下
-
-(void)btnClicked:(UIButton *)sender
-
{
-
//创建两个用于测试的消息体
-
UNMutableNotificationContent *content1 = [[UNMutableNotificationContent alloc]init];
-
content1.title = @"1";
-
content1.body = @"通知1";
-
-
UNMutableNotificationContent *content2 = [[UNMutableNotificationContent alloc]init];
-
content2.title = @"2";
-
content2.body = @"通知2";
-
-
switch (sender.tag) {
-
case 1001:
-
{
-
//发送 取消
-
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
-
NSString *identifier = @"SendAndCancle";
-
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
-
-
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-
//
-
}];
-
-
//延迟2秒之后执行
-
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
-
-
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
-
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[identifier]];
-
});
-
break;
-
}
-
case 1002:
-
{
-
//发送 更新
-
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
-
NSString *identifier = @"SendAndModify";
-
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
-
-
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-
//
-
}];
-
-
//延迟2秒之后执行
-
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
-
-
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
-
-
//用相同的标识再次发送即可覆盖
-
UNTimeIntervalNotificationTrigger *triggerNew = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
-
-
UNNotificationRequest *requestNew = [UNNotificationRequest requestWithIdentifier:identifier content:content2 trigger:triggerNew];
-
-
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requestNew withCompletionHandler:^(NSError * _Nullable error) {
-
//
-
}];
-
});
-
-
break;
-
}
-
case 1003:
-
{
-
//发送 删除
-
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
-
NSString *identifier = @"deliveredSendAndRemove";
-
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
-
-
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-
//
-
}];
-
-
//延迟4秒之后执行
-
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
-
-
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
-
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
-
});
-
-
break;
-
}
-
case 1004:
-
{
-
//发送 修改
-
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
-
NSString *identifier = @"deliveredSendAndModify";
-
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content1 trigger:trigger];
-
-
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-
//
-
}];
-
-
//延迟4秒之后执行
-
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0/*延迟执行时间*/ * NSEC_PER_SEC));
-
-
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
-
-
//用相同的标识再次发送即可覆盖
-
UNTimeIntervalNotificationTrigger *triggerNew = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
-
-
UNNotificationRequest *requestNew = [UNNotificationRequest requestWithIdentifier:identifier content:content2 trigger:triggerNew];
-
-
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:requestNew withCompletionHandler:^(NSError * _Nullable error) {
-
//
-
}];
-
-
});
-
break;
-
}
-
-
default:
-
break;
-
}
-
}
最终的效果图可以在实现之后自行查看,这里就不上传介绍了