如何实现IOS APP被杀掉后依然可以接收到个推消息通知

背景

项目已经集成了个推SDK,但是在离线场景下无法收到推送消息,离线场景主要分2种情况,一种是用户将APP切换到了后台,一种是用户将APP杀掉了。

针对场景一:我们可以将APP支持后台运行,比如项目中使用到了后台持续定位功能,后台音频等(Xcode中可以查看支持哪些后台模式),可以增加接收到离线通知的概率。

针对场景二:我们需要集成NotificationServiceExtension,下面就主要介绍一下如何集成

新增NotificationServiceExtension

在XCode中,按照以下步骤操作

新增一个target


配置好NotificationService的包名和签名

Bundle一般就用自己APP的包名加上NotificationService,如 com.edenwang.www.NotificationService

将下载的个推SDK中的NotificationService代码复制到项目中

个推SDK下载地址

https://docs.getui.com/download.html

解压开找到NotificationService

将压缩包中NotificationService.swift文件内容替换项目中的NotificationService.swift

Info.plist和NotificationService.entitlements

记得将NotificationService.entitlements中包名更新成自己的

新建一个空的OC文件

Empty.m是新建的一个空的Objective-C文件,这个文件不能少。这个是咨询个推的技术人员后才得知的,说是混编的问题,如果是Swift项目需要加一个OC文件,如果是OC项目需要加一个空的Swift文件。可以查看下载的个推SDK中其他方式的实现作对比。

其他文件复制

复制NotificationService-Bridging-Header.h

打包测试

为了快速测试,需要将个推的后台运行临时关掉 GeTuiSdk.runBackgroundEnable(false)

安装到手机上后,将APP退出到后台后,稍等片刻,在个推控制台上查询当前设备的CID,检查CID的状态是否为离线,如果已经为离线了,可以通过个推的控制台推送测试消息,然后观察手机上收到的消息是否包含[Success]字符,如果有,说明大功告成了。

Swift 复制代码
  override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    guard let bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent else { return }
   
    // [ 测试代码 ] TODO:语音播报
    let cnt: Double = 123 //读取apns中播报信息
    let name = ApnsHelper.makeMp3FromExt(cnt)
    let sound = UNNotificationSound(named: UNNotificationSoundName(name))
    bestAttemptContent.sound = sound
    
    // [ 测试代码 ] TODO: 用户可以在这里处理通知样式的修改,eg:修改标题,开发阶段可以用于判断是否运行通知扩展
//    bestAttemptContent.title = "\(bestAttemptContent.title) [WillIn]"
    
    // [ GTSDK ] 统计APNs到达情况和多媒体推送支持接口, 建议使用该接口
    GeTuiExtSdk.handelNotificationServiceRequest(request, withAttachmentsComplete: { [weak self] (attachments: Array?, errors: Array?) in
      guard let handler = self?.contentHandler else { return }
      // [ 测试代码 ] TODO:日志打印,如果APNs处理有错误,可以在这里查看相关错误详情
      // print("处理个推APNs展示遇到错误:\(String(describing: errors))")
      
      // [ 测试代码 ] TODO:用户可以在这里处理通知样式的修改,eg:修改标题,开发阶段可以用于判断是否运行通知扩展
      bestAttemptContent.title = "\(bestAttemptContent.title) [Success]"
      if let attachment = attachments as? [UNNotificationAttachment], !attachment.isEmpty {
        // 设置通知中的多媒体附件
        bestAttemptContent.attachments = attachment
      }
      // 展示推送的回调处理需要放到个推回执完成的回调中
      handler(bestAttemptContent)
    })
  }

然后你可以在这个代码基础上定制自己的业务逻辑。

相关推荐
游戏开发爱好者81 小时前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
HH思️️无邪2 小时前
Flutter 项目 -从 0 到 1 实现 iOS WatchApp
flutter·ios
TheNextByte13 小时前
如何将 iPhone 上的联系人备份到 iCloud?
ios·iphone·icloud
牛马1114 小时前
iOS swift 自定义View
ios·cocoa·swift
2501_915106324 小时前
HBuilderX 项目上架 iOS app上架 App Store 的关键流程
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
iOS 文件管理,在不越狱的前提下管理 iPhone / iPad 文件
android·ios·小程序·uni-app·iphone·webview·ipad
牛马1114 小时前
ios swift处理json数据
ios·json·swift
黑码哥4 小时前
iOS开屏广告多机型屏幕适配最佳实践
macos·ios·cocoa·广告·商业·开屏广告
Swift社区4 小时前
LeetCode 473 火柴拼正方形 - Swift 题解
算法·leetcode·swift
前端不太难6 小时前
Flutter / RN / iOS,在长期维护下谁更容易“止损”?
flutter·ios·状态模式