简体   繁体   English

应用未运行时处理 iOS 推送通知

[英]Handling iOS push notifications when the app is not running

I have an app that receives PN from a backend using Firebase Cloud Messaging.我有一个应用程序使用 Firebase 云消息从后端接收 PN。 Basically, all I what to do is store some data of the PN with CoreData.基本上,我要做的就是用 CoreData 存储 PN 的一些数据。 I'm using application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) to do it.我正在使用application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)来做这件事。 I tested the feature in foreground and background, opening the app with the PN and with the app icon.我在前台和后台测试了该功能,使用 PN 和应用程序图标打开应用程序。 Everything works fine.一切正常。 But the problem is in this two cases:但问题出在这两种情况下:

  • When the app is killed, a PN arrives, and the user taps on the push notification.当应用程序被终止时,PN 到达,用户点击推送通知。
  • When the app is killed, a PN arrives, and the user taps on the app icon.当应用程序被终止时,PN 到达,用户点击应用程序图标。

I saw that sometimes, didReceiveRemoteNotifications is fired.我看到有时会触发didReceiveRemoteNotifications Sometimes, it doesn't.有时,它没有。 I read that this method is not called when the app is killed but I'm experiencing a different behaviour.我读到当应用程序被终止时不会调用此方法,但我遇到了不同的行为。 Also, I read that the way to handle push notifications in killed state is with VoIP apps or with UNNotificationServiceExtension .另外,我读到在 killed state 中处理推送通知的方法是使用 VoIP 应用程序或使用UNNotificationServiceExtension My app is not a VoIP app… Anyone knows if UNNotificationServiceExtension can resolve my problem?我的应用程序不是 VoIP 应用程序……有人知道UNNotificationServiceExtension是否可以解决我的问题吗? This is the payload fired by the backend:这是后端触发的有效载荷:

const message = {
  apns: {
    payload: {
      aps : {
        'content-available' : 1,
        alert: {
          title : "Game Request",
          body : "Bob wants to play poker",
        }
     },
    },
  },
  data: {
    shortText: 'test',
    chasisNumber: '23',
    alertType: 'some alert type',
    alertId: '1234',
    alertTypeId: '1234',
    userId: '1234',
    text: 'some text',
    title: 'some title',
    deleted: 'false',
    creationDate: '2011-11-02T02:50:12.208Z',
    receivedDate: '2011-11-02T02:50:12.208Z',
    actionType: 'some action type',
    ticketId: '1234',
    status: 'some status'
  },
  tokens: [androidToken, iosToken]
};

First of all to answer your question before iOS 13 is that it was possible to handle a push via VoIP.首先在 iOS 13 之前回答您的问题是可以通过 VoIP 处理推送。 Since iOS13 this method is no longer possible.自 iOS13 以来,此方法不再可行。

If your application is killed it is not possible to handle the push notification if the user does not click on the notification.如果您的应用程序被终止,如果用户没有点击通知,则无法处理推送通知。 If you receive a PN and the user opens the application via the phone's home screen you will not be able to access the PN data.如果您收到 PN 并且用户通过手机的主屏幕打开应用程序,您将无法访问 PN 数据。

  • If the application is killed, the user receive PN and he click on the notification to open the app;如果应用被杀死,用户收到PN,点击通知打开应用; you must add the below code in the method didFinishLaunchingWithOptions of the AppDelegate to handle the PN:您必须在AppDelegatedidFinishLaunchingWithOptions方法中添加以下代码来处理 PN:
if let remoteUserInfo = launchOptions?[.remoteNotification] as? [AnyHashable: Any] {
    self.application(application, didReceiveRemoteNotification: remoteUserInfo)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM