呼如林
要在应用程序位于前台时显示横幅消息,请使用以下方法。iOS 10,Swift 3/4:// This method will be called when app received push notifications in foregroundfunc userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])}iOS 10,Swift 2.3:@available(iOS 10.0, *)func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void){
//Handle the notification
completionHandler(
[UNNotificationPresentationOptions.Alert,
UNNotificationPresentationOptions.Sound,
UNNotificationPresentationOptions.Badge])}您还必须将您的应用代表注册为通知中心的代表:import UserNotifications// snip!class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate// snip!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// set the delegate in didFinishLaunchingWithOptions
UNUserNotificationCenter.current().delegate = self ...
}