3

这里是我已经习惯了接收推送通知的代码时,应用前景iOS版雨燕接收推送通知当应用程序在前台

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) 
{ 
    completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge]) 
} 

但我的问题是,我的通知包含[NSObject的:AnyObject]值。如何收到这样的背景通知

+0

是否要通过通知对象访问用户信息? – Darshana

+0

@iOS_devloper是的,先生 –

+0

我使用“notification.request.content.userInfo”来获得数据。你可以用上面的方法打印这个。 – Amanpreet

回答

4

添加该代码在AppDelegate.swift文件。

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{ 
    completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge]) 
} 
+0

这个怎么写在iOS 8.0中@Kavin Kumar Arumugam –

+0

@DilipTiwari请在上面的回答更新 –

+0

@DilipTiwari上面的代码没有变化,它已经支持iOS 8.0 –

1
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 

    if (application.applicationState == 0) // active --- forground 
    { 
     // publish your notification message 
    } 

} 
0

UNNOtification有财产要求(UNNotificationRequest),你可以使用它来获取用户信息。

使用后从未通知用户的访问信息:

let userinfo = notification.request.content.userInfo 
相关问题