繁体   English   中英

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

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

我有一个应用程序使用 Firebase 云消息从后端接收 PN。 基本上,我要做的就是用 CoreData 存储 PN 的一些数据。 我正在使用application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)来做这件事。 我在前台和后台测试了该功能,使用 PN 和应用程序图标打开应用程序。 一切正常。 但问题出在这两种情况下:

  • 当应用程序被终止时,PN 到达,用户点击推送通知。
  • 当应用程序被终止时,PN 到达,用户点击应用程序图标。

我看到有时会触发didReceiveRemoteNotifications 有时,它没有。 我读到当应用程序被终止时不会调用此方法,但我遇到了不同的行为。 另外,我读到在 killed state 中处理推送通知的方法是使用 VoIP 应用程序或使用UNNotificationServiceExtension 我的应用程序不是 VoIP 应用程序……有人知道UNNotificationServiceExtension是否可以解决我的问题吗? 这是后端触发的有效载荷:

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]
};

首先在 iOS 13 之前回答您的问题是可以通过 VoIP 处理推送。 自 iOS13 以来,此方法不再可行。

如果您的应用程序被终止,如果用户没有点击通知,则无法处理推送通知。 如果您收到 PN 并且用户通过手机的主屏幕打开应用程序,您将无法访问 PN 数据。

  • 如果应用被杀死,用户收到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