iOS 紧急通知

iOS 紧急通知

一般通知

关于通知的各种配置和开发,可以参考推送通知教程:入门 – Kodeco,具有详细步骤。

紧急通知表现

  1. 紧急通知不受免打扰模式和静音模式约束。
  2. 当紧急通知到达时,会有短暂提示音量和抖动(约2s)。
  3. 未锁屏状态,通知banner会持续展示,锁边状态,紧急通知不会被收集到组内。
  4. 可以自定义声音,且声音可以超过30s,播放最长时长限制未知(已测试4分钟自定义音频可以完整播放)。

紧急通知在 iOS16.2 上的效果图如下:

iOS 紧急通知

紧急通知申请和配置

紧急通知并不是面向所有应用,如果 APP 需要发送紧急通知,需要向苹果申请,申请通过才能是使用。

Request Critical Alert Notifications Entitlement

申请界面如下:

iOS 紧急通知

一般理由合适,在两三个工作日即可申请下来,这时候在苹果开发者证书配置中可以勾选Critical Alerts,如下图

iOS 紧急通知

Xcode 端更新证书,可以通过查看证书的 Entitlements查看

iOS 紧急通知

最后,还需要在项目的.entitlements 文件添加com.apple.developer.usernotifications.critical-alerts字段,并将值设为YES

iOS 紧急通知

通知设置的紧急通知开关如下图

iOS 紧急通知

紧急通知开发

var authOptions: UNAuthorizationOptions?
if #available(iOS 12.0, *) {
    authOptions = [.alert, .badge, .sound, .criticalAlert]
} else {
    authOptions = [.alert, .badge, .sound]
}
UNUserNotificationCenter.current().requestAuthorization(options:   
  authOptions!) { (granted, error) in
    if !granted {
        print(“紧急通知权限被拒绝”)
    }
}

请求紧急通知后,会弹出如下弹窗:

iOS 紧急通知

如何测试紧急通知

可以借助onmyway133/PushNotifications: 🐉 A macOS, Linux, Windows app to test push notifications on iOS and Android (github.com)来发送通知,进行测试。

apns json如下:

{
   "aps":{  
       "alert":"This is a Critical Alert!",
       "badge": 1,
       "sound": {  
         "critical": 1,
         "name":"your_custom_sound.aiff",
         "volume": 1.0
        }
     }
}

其中

  • critical:用于触发紧急通知
  • name:自定义声音
  • volume:声音大小,如果不传,默认为1.0

其他参数参考苹果对于APNs字典定义。Generating a remote notification

或者你也可以使用命令行发送通知Sending push notifications using command-line tools | Apple Developer Documentation

自定义紧急通知声音

由于自定义通知声音还是由 iOS 系统来播放的,所以对音频数据格式有限制,可以是如下四种之一:

  1. Linear PCM
  2. MA4 (IMA/ADPCM)
  3. µLaw
  4. aLaw

对应音频文件格式是 aiffwavcaf 文件,文件也必须放到 app 的 mainBundle 目录中。

自定义普通通知声音的播放时间必须在 30s 内,如果超过这个限制,则将用系统默认通知声音替代。

可以使用 afconvert 工具来处理音频文件格式,转换命令如下:

afconvert xxx.mp3 criticalAlert.caf -d ima4 -f caff -v

转换完成后就可以将 criticalAlert.caf 这个文件拖入 Xcode 工程中,注意一定要选择 Add to targets 。

普通通知 apns json

{
  "xxx":"xxx"
  "sound":"xxx.caf"
}

紧急通知 apns json

{
   "aps":{  
       "alert":"This is a Critical Alert!",
       "badge": 1,
       "sound": {  
         "critical": 1,
         "name":"criticalAlert.caf",
         "volume": 1.0
        }
     }
}

参考

What’s New in User Notifications - WWDC18 - Videos - Apple Developer

推送通知教程:入门 – Kodeco

如何实施 iOS 关键警报 – medium.com

如何实施关键警报 –- tapcode.co

[Implementing iOS Critical Alerts. iOS 12 has added the critical alerts… | by Shashidhar Yamsani | Medium](https://medium.com/@shashidharyamsani/implementing-ios-critical-alerts-7d82b4bb5026#:~:text=iOS 12 has the critical,allowed to send critical alerts)

声明:本站所有文章,如无特殊说明或标注,均为爬虫抓取以及网友投稿,版权归原作者所有。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧