【问题标题】:Why I did not receive any notification on my iPhone?为什么我的 iPhone 没有收到任何通知?
【发布时间】:2017-04-14 07:48:34
【问题描述】:

使用 swift 3.1 在 Xcode8.3 上运行它

下面是我在 AppDelegate.swift 中的代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let center = UNUserNotificationCenter.current()

    center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
        if granted {
            print("OK!")
        }
        else {
            print("No!")
        }
    })

    application.registerForRemoteNotifications()

    return true
}


func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("==== didReceiveRemoteNotification ====")
    print(userInfo)
}

我使用 node-apns 将通知推送到我的应用程序,我可以从 Xcode 中的调试区域收到消息。

==== didReceiveRemoteNotification ====
[AnyHashable("id"): 123, AnyHashable("aps"): {
    alert = "Hello World \U270c";
    badge = 3;
}]

但是,我的 iPhone 没有收到任何通知。

我错过了什么吗?

【问题讨论】:

  • 您不需要代码来将设备标识符上传到您的任何地方的服务吗?
  • 可能是因为您的应用位于foreground 中?只有在offbackground 时才会显示推送
  • @JuicyFruit 谢谢。之后,我在手机的后台运行我的应用程序。我可以收到通知。如何设置为前台和后台都可以接收通知。
  • @Dreams 检查我的答案

标签: ios iphone swift xcode


【解决方案1】:

使用 iOS 10,您可以执行以下操作以在应用处于前台时查看推送通知。您必须在 AppDelegate 中实现以下功能。它是UNUserNotificationCenterDelegate的委托方法。

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

按照以下步骤操作:

  1. 在 AppDelegate 中导入 UserNotifications 并扩展 UNUserNotificationCenterDelegate

  1. didFinishLaunchingWithOptions 中设置 UNUserNotificationCenter 委托。
让 center = UNUserNotificationCenter.current()
center.delegate = self
  1. 在 AppDelegate 中实现 will present 方法并使用选项调用完成处理程序。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent 通知: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert,.badge,.sound])
}

现在,如果您收到推送,即使您的应用处于前台,您也可以看到通知警报。 More Info in Apple Doc.

【讨论】:

    【解决方案2】:

    如果你想处理通知,当你的应用程序处于活动状态时,你应该这样做,因为推送视图不会出现

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        if application.applicationState == .active {
            //create custom view or something
        } else {
            //it was background/off
        }
    }
    

    您可能还对创建新的构建模式感兴趣,因此您的应用将等待它启动,这样您就可以在接收推送时调试您的应用行为。

    【讨论】:

    • 我不明白你的意思:“你的应用会等到它启动,所以你可以在收到推送时调试你的应用行为”你能详细说明这与“有什么不同吗?自动”
    • @Honey 它会在你的设备上编译你的应用程序,但不会自动启动它,所以你可以通过它发送推送并自己打开它以查看与它相关的一些控制台日志
    • 我明白你在说什么......说等待可执行文件转换为“等待你手动点击应用程序”??错误的命名!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多