iOS 发送本地推送通知

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
(注:不要忘记空格)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 许多集成的步骤个推官网都有了,这里只写关于推送的远程推送和本地通知的步骤和代码。APP在后台时:走苹果的APNS通...
    AllureJM阅读 7,760评论 1 9
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,714评论 10 16
  • 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知...
    莫离_焱阅读 11,592评论 1 8
  • //发送通知 UILocalNotification *notification=[[UILocalNotific...
    韩七夏阅读 5,532评论 1 0
  • 在简单项目中,有使用到apns推送服务,许多文章有涉及到却没有讲清楚。最近做福路通项目,有使用到,做一个总结。 推...
    天空的守望者阅读 4,390评论 0 3