为什么我的 iPhone 没有收到任何通知? [英] Why I did not receive any notification on my iPhone?

查看:36
本文介绍了为什么我的 iPhone 没有收到任何通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xcode8.3 上使用 swift 3.1 运行这个

Running this on Xcode8.3 with swift 3.1

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

Below is my code in 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 的调试区域收到消息.

I use node-apns to push notification to my app and I can have message from my Debug area in Xcode.

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

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

But, I did not receive any notification on my iPhone.

我是不是遗漏了什么?

推荐答案

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

if you want to handle notifications, when your app is active, you should do something like this, because the push view won't appear

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

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

you might be also interested in creating new build schema, so your app will wait until it would be launched, so you can debug your app behaviour when receiving push.

这篇关于为什么我的 iPhone 没有收到任何通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆