簡體   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