【问题标题】:Local notification not calling didReceiveRemoteNotifications in swift 4 [duplicate]本地通知未在 swift 4 中调用 didReceiveRemoteNotifications [重复]
【发布时间】:2018-08-03 06:04:36
【问题描述】:

我有以下 appDelegate:

import UIKit
import CoreData
import UserNotifications

@available(iOS 11.0, *)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
    var window: UIWindow?

    ...

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
    {
        print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }
}

我正在使用 ios11 和 swift 4.1,但通知被正确触发。但是当我点击它时,我无法在应用程序中抓取它。为什么?我正在使用 xcode 9.4.1。谢谢。

应用程序在后台,当点击通知时应用程序打开。

【问题讨论】:

  • 应用的状态是什么?背景/关闭/打开?
  • 什么是本地推送通知?是推送通知还是本地通知?
  • 更新问题
  • 如果你只是想获得本地通知响应,试试 userNotificationCenter。我认为您的代码用于远程通知(didReceiveRemoteNotification)
  • 我想在有人点击时获取本地通知。这是怎么做的?我有这些答案:stackoverflow.com/questions/51626479/…

标签: ios swift apple-push-notifications swift4 ios11


【解决方案1】:

你好@cdub 你应该使用 usernotificatioCenter

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    // do code here to show notification alert in for-ground mode 
}




func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //your notification handling code here 
    }

【讨论】:

  • 这也包含在 appDelegate 中吗?只是好奇
  • 是的,当然你必须把它放在 appdelegate 中
  • 它现在可以工作了,谢谢
  • 我的荣幸@cdub 请也投赞成票
  • notification.request.content.userInfo ==== willPresent 委托和 response.notification.request.content.userInfo ==== 在 didReceive 中这样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多