2017-07-17 80 views
0

因此,当我的应用程序当前未打开并且收到推送通知时,当横幅出现并按下时,“didrecieveremotenotification”完美无缺。但是当应用程序打开时,根本没有横幅出现。我希望推送通知在应用程序打开时工作。推送通知不工作迅速

我试过使用UNNotificationCenter,但一般都丢失了推送通知。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in 
     if (granted) { 
      UIApplication.shared.registerForRemoteNotifications() 
     } else{ 
      print("Notification permissions not granted") 
     } 
    }) 
} 


//Called when a notification is delivered to a foreground app. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
    completionHandler([.sound, .alert, .badge]) 
} 

//Called to let your app know which action was selected by the user for a given notification. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 
    completionHandler() 
} 

更新:UNUserNotificationCenter.current().requestAuthorization运行

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) 

时授予许可。

但是,当我不使用UNNotificationCenter并使用旧方法时,它不会失败。

为什么一个方法无法注册远程通知,但另一个不是?

更新2:在模拟器工作大声笑,这就是为什么它失败。但是现在,当我运行 UNUserNotificationCenterDelegate 方法只有

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive方法的工作,而不是

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent

更新3:我已经跑出去的可能的事情去尝试。什么会阻止代码发射willPresent而不是didReceive。这是残酷的。有没有人曾经有过这个问题?

更新4:已解决。我用于推送通知的服务要求用户与服务断开连接以发送推送通知。当你在后台时它会自动断开你的连接,这就是为什么这个工作正常,并且它不在前台工作。

奇怪的方式为他们配置,我可能会发一封电子邮件。

+0

一切看起来不错..尝试删除该应用。 – Bilal

+0

试过已经 – NickPali

+0

可以提供'didFailToRegisterForRemoteNotificationsWithError'错误吗? – Imotep

回答

1

实施UNUserNotificationCenterDelegate委托方法获取通知(托盘顶部),而应用程序在前台。但它只会与IOS 10一起工作。

在你的didFinishLaunchingWithOptions方法集UNUserNotificationCenterDelegate委托像这样。

UNUserNotificationCenter.current().delegate = self 

实现委托方法...

//Called when a notification is delivered to a foreground app. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
    completionHandler([.sound, .alert, .badge]) 
} 

//Called to let your app know which action was selected by the user for a given notification. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 
    completionHandler() 
} 
+0

我试着用这个确切的代码之前问这个问题,我的推送通知只是完全停止工作。 – NickPali

+0

请用代码更新您的问题。 – Bilal

+0

我更新了问题 – NickPali