App在后台的情况希望通知用户看到信息时使用本地推送通知实现。
1.导入系统框架
#import <UserNotifications/UserNotifications.h>
2.注册本地通知样式
UIApplication* application = [UIApplication sharedApplication];
if ([application currentUserNotificationSettings]) {
UIUserNotificationSettings* setting = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert
categories:nil];
[application registerUserNotificationSettings:setting];
}
3.清除之前注册通知
[application cancelAllLocalNotifications];
如果不清除历史通知,可能会收到之前的全部通知。
4.设置本次通知内容
UILocalNotification* localNotif = [[UILocalNotification alloc] init];
// 发送时间
localNotif.fireDate =[NSDate dateWithTimeIntervalSinceNow:0];
// 在通知中心要展示的通知内容
localNotif.alertBody = @"本地推送通知";
// 通知携带的内容信息,开发者自行处理
localNotif.userInfo = @{@"userid":@(123), @"message":@"通知"};
// 自定义同事提示声音,必须是.caf格式音频文件
localNotif.soundName = @"voip_call.caf";
// 通知唯一标识,可以是msgId,用户删除、查找通知
localNotif.alertAction = @"notificationId";
[application scheduleLocalNotification:localNotif];
以上方法在 iOS10之后被弃用,iOS 开始使用UNUserNotificationCenter
具体逻辑大概类似。UNUserNotificationCenter的内容更加丰富,可以通过它自行定义通知显示样式,功能更加强大。
UNUserNotificationCenter* center =
[UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content =
[[UNMutableNotificationContent alloc] init];
content.body = @"本地推送通知";
UNNotificationSound* sound =
[UNNotificationSound soundNamed:@"voip_call.caf"];
content.sound = sound;
content.userInfo = @{@"userid":@(123), @"message":@"通知"};
UNTimeIntervalNotificationTrigger* tirgger =
[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1
repeats:NO];
/// Identifier 通知唯一标识,用于删除、查找通知
UNNotificationRequest* request =
[UNNotificationRequest requestWithIdentifier:@"notificationId"
content:content
trigger:tirgger];
[center addNotificationRequest:request
withCompletionHandler:^(NSError* _Nullable error) {
NSLog(@"%@本地推送 :( 报错 %@", @"notificationKey", error);
}];
最后接收本地推送通知
- (void)application:(UIApplication*)application
didReceiveLocalNotification:(UILocalNotification*)notification {
}
iOS10之后
// 点击推送消息后回调
- (void)userNotificationCenter:(UNUserNotificationCenter*)center
didReceiveNotificationResponse:(UNNotificationResponse*)response
withCompletionHandler:(void (^)(void))completionHandler {
NSLog(@"center content : %@", response.notification.request.content.userInfo);
}
有时候需要删除通知中心的通知,类似微信撤回消息时会撤回通知
- (void)deleteAVChatLocalNotificationId:(NSString *)notificationId {
__block BOOL has = NO;
UIApplication* application = [UIApplication sharedApplication];
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter* center =
[UNUserNotificationCenter currentNotificationCenter];
[center getPendingNotificationRequestsWithCompletionHandler:^(
NSArray<UNNotificationRequest*>* _Nonnull requests) {
for (UNNotificationRequest* request in requests) {
if ([request.identifier isEqualToString:notificationId]) {
has = YES;
break;
}
}
}];
if (has) {
[center
removeDeliveredNotificationsWithIdentifiers:@[notificationId]];
}
} else {
NSArray* localNotifications = [application scheduledLocalNotifications];
for (UILocalNotification* local in localNotifications) {
if ([local.alertAction isEqualToString:notificationId]) {
[application cancelLocalNotification:local];
has = YES;
break;
}
}
}
if (has) {
NSInteger num = [application applicationIconBadgeNumber];
if (num > 1) {
[application setApplicationIconBadgeNumber:num - 1];
} else {
[application setApplicationIconBadgeNumber:0];
}
}
}
mp3转 caf
第一步:获取MP3文件路径,为了方便测试,这里把文件放到了桌面上,地址为 :/Users/Mina/Desktop/1.mp3
第二步:设置CAF文件的输出路径,这里也放到了桌面上,路径为:/Users/Mina/Desktop/2.caf
第三步:打开终端输入命令:
afconvert /Users/Mina/Desktop/1.mp3 /Users/Mina/Desktop/2.caf -d ima4 -f caff -v
(注:不要忘记空格)