2017-04-14 52 views
1

与SWIFT 3.1为什么我没有收到我的iPhone的任何通知?

下面就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) 
} 

代码我使用节点APNS推送通知我的应用程序,我可以从我的调试领域有消息在Xcode中。

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

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

难道我失去了一些东西?

+0

可能重复[苹果推送通知不生产](http://stackoverflow.com/questions/15595720/apple-push-notification-not-working-in-production) – iphonic

+0

你不需要代码将设备标识符上传到您的服务位置? – Alistra

+1

也许是因为你的应用程序是'foreground'?推仅表现如果是'off'或'background' – JuicyFruit

回答

1

,如果你要处理的通知,当你的应用程序被激活,你应该做这样的事情,因为推视图不会出现

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

你可能也有兴趣在创造新的建设模式,所以你的应用程序会一直等到它启动,所以你可以在接收推送时调试你的应用程序行为。 enter image description hereenter image description here

+0

我不明白你的意思是什么:“你的应用会一直等到它启动,所以你可以在接收推送时调试你的应用行为”你能否详细说明与“自动“ – Honey

+0

@Honey它会在您的设备上编译您的应用程序,但不会自动启动它,因此您可以通过它自己发送推送并自行打开,以查看与之相关的一些控制台日志。 – JuicyFruit

+0

我明白您的意思了...说等待可执行文件翻译为'等待你手动点击应用程序'?错误的命名! – Honey

1

随着iOS的10,你可以做以下看到推送通知时,应用程序是在前台。你必须在AppDelegate中实现下面的函数。它的UNUserNotificationCenterDelegate的委托方法。

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

遵循以下步骤:

  1. 进口UserNotifications在AppDelegate中延长UNUserNotificationCenterDelegate

enter image description here

  • 搭建UNUserNotificationCenter代表在didFinishLaunchingWithOptions
  • let center = UNUserNotificationCenter.current()
    center.delegate = self
  • 实现的AppDelegate意愿本方法和调用与所述选项中完成处理程序。现在
  • func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
         completionHandler([.alert,.badge,.sound]) 
    }

    ,如果你得到一推,就可以看到该通知提醒,即使你的应用程序是在前台。 More Info in Apple Doc

    相关问题