通知服务扩展程序无法工作

7
当我发送带有推送负载mutable-content: 1的通知时,通知未显示,也没有命中通知服务扩展中的断点,尽管没有可变内容推送被显示,但通知内容扩展正常工作。我没有修改通知服务扩展内部的代码,它是由Xcode默认生成的。在创建通知服务扩展时,我是否遗漏了某些东西,或者可能是设备设置的问题?几天前,我在同一设备上进行了测试,通知服务扩展正常工作。 下面是我的服务扩展代码:
import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
    
           contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

编辑1:以下是我的推送负载

{  
   "aps":{  
      "sound":"default",
      "mutable-content": 1,
      "category": "myCategory",
      "alert":{  
         "body":"this is a custom push",
         "subtitle":"subtitle of the push",
         "title":"Push Test"
      }
      
   }
}

编辑1:问题似乎出现在使用10.2.1系统的特定设备上,我在另一台运行10.2系统的设备上进行了检查,它触发了通知服务扩展。

你是如何发送包含“mutable-content:1”的Json的?如果使用FCM,请将“-”更改为“_”。 - SeikoTheWiz
嗨@SeikoTheWiz,我没有使用FCM。 - rahulk9
3
嗨@princerk,我也正在尝试实现通知服务扩展,但即使我使用默认的苹果代码,它也不会修改我的内容。 我需要为通知服务扩展设置的 App Id 启用推送通知吗? 请帮助我了解所需的步骤以使其正常工作。 - Milan Agarwal
@Milan 不需要为服务扩展启用推送通知。Xcode生成的默认代码应该可以完成工作。确保在您的推送有效负载中传递“mutable-content”:1。尝试使用其他设备。 - rahulk9
就你修改有效载荷的方式,你并没有展示出来。 - mfaani
7个回答

24

最终我解决了这个问题。虽然我不确定问题的原因,但是经过一些实验我发现并不仅是我的服务扩展,所有其他已安装应用的服务扩展都无法工作,当发送带有 "mutable-content:1" 的推送有效载荷时,通知没有显示。在重新启动设备后,它开始正常工作。我使用的是iPhone 6s。


最近我的一些客户也遇到了类似的问题,重启后一切正常。你找到根本原因了吗? - Tiến Nguyễn Hữu

19

你的NotificationServiceExtension的部署目标也可能是原因之一。

在我的情况下,我在运行iOS 12.4的设备上进行测试,而我的NotificationServiceExtension的目标版本为14.1。我只需将NotificationServiceExtension的目标版本更改为12.4即可解决问题。


2
天啊,那正是导致我的问题的原因。 - brutella
我花了几周的时间试图弄清楚为什么它在我们的QA流程中不起作用,这对我来说就是答案。谢谢! - Alejandro Iván

2
在我的情况下,我先移除了扩展名再重新添加。然后重启设备。 enter image description here

这是唯一在2022年6月22日对我有效的解决方案。在我的应用程序转移后,我不得不删除此文件并重新添加它。然后通知扩展再次开始工作。 - coolcool1994

0
在我的情况下,问题最终是由于我的扩展程序使用了太多的内存。而Console.app只显示Springboard用错误信息杀死了该应用程序:

CMSessionMgr- CMSessionMgrHandleApplicationStateChange: CMSession: Sending stop command to _ with pid '_' because client is not allowed to play in the background AND does not continue AirPlaying video when device locks

我以为这是权限或类似的问题,但一旦我进行了调试,我就发现这是iOS因为占用了太多内存而杀死了该进程。一旦我修复了这个bug,扩展程序就一直保持活动状态,直到我调用回调函数。

0

在真实设备(iPhone / iPad)上尝试。模拟器不起作用。


0
在我的情况下,当我在调试时,通知服务扩展没有在系统接收到带有可变内容负载的通知时启动。我尝试了很多次,然后发现在运行脚本中,勾选了“仅在安装时复制”。我取消勾选它,然后就能够调试扩展程序了。

enter image description here


0
可能与一般问题无关,但我花了整整一天的时间试图弄清楚为什么在使用Xcode运行时,AppMetrica Push SDK推送活动无法在TestFlight中接收到,但是在"push environment"设置为"production"时却能成功接收到。对我来说,问题出在"development"环境下:func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)这个方法需要做如下更改:
let pushEnvironment = YMPYandexMetricaPushEnvironment.production
YMPYandexMetricaPush.setDeviceTokenFrom(deviceToken, pushEnvironment: pushEnvironment)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接