推送通知未在 iOS 10 设备中显示
Posted
技术标签:
【中文标题】推送通知未在 iOS 10 设备中显示【英文标题】:Push notification doesn't showing in iOS 10 devices 【发布时间】:2017-07-20 04:40:09 【问题描述】:我是 Swift 新手,ios 10 中没有弹出推送通知,我做了所有配置。通知在 iOS 8 和 9 设备中显示(相同代码),但在 iOS 10 设备中不显示。代码就像(只添加了通知部分代码)。
private let brandFullery : String = "some token key"
var pushNotify : NSDictionary!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
UIApplication.sharedApplication().cancelAllLocalNotifications()
MyCacheManager.sharedInstance.deviceId = UIDevice.currentDevice().identifierForVendor!.UUIDString
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
application.registerForRemoteNotifications()
MyCacheManager.sharedInstance.deleteAllFileInDocument("LiveUserProfile")
if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, metData = payload["data"] as? NSDictionary
pushNotify = metData
let aps = pushNotify["eventType"] as! NSString
if aps != "INVITE_POINTS" && aps != "UPDATE_BEACON" && aps != "CREATE_REWARD"
MyCacheManager.sharedInstance.isFromNotification = aps as String
else
if aps == "UPDATE_BEACON"
MyCacheManager.sharedInstance.isBeaconUpdate = true
MyCacheManager.sharedInstance.isFromNotification = ""
self.createMenuView()
else
MyCacheManager.sharedInstance.isFromNotification = ""
self.createMenuView()
#if BRANDAPP
Twitter.sharedInstance().startWithConsumerKey(brandTwitterAuthKey, consumerSecret: brandSecuriteKey)
Flurry.startSession(brandFullery)
#else
Twitter.sharedInstance().startWithConsumerKey(userTwitterAuthKey, consumerSecret: userSecuriteKey)
Flurry.startSession(userFullery)
#endif
Fabric.with([Twitter.self])
UIApplication.sharedApplication().statusBarStyle = .LightContent
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
//MARK: Push notification
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
var deviceTokenString : NSString = deviceToken.description;
deviceTokenString = deviceTokenString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ", withString: "")
MyCacheManager.sharedInstance.deviceToken = deviceTokenString as String
MyCacheManager.sharedInstance.putDeviceToken(deviceTokenString)
NSLog("device %@", deviceTokenString)
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
MyCacheManager.sharedInstance.isFromNotification = "MSG"
print(userInfo)
请任何人帮助我完成这项工作。非常感谢。
【问题讨论】:
您必须为 iOS 10 使用更新的 API,即UNUserNotificationCenter
。请参考***.com/questions/37807302/…
Registering for Push Notifications in Xcode 8/Swift 3.0?的可能重复
【参考方案1】:
你必须注册 ios 10。试试这个
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
// iOS 10+ support
if #available(iOS 10.0, *)
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) (granted, error) in
if error == nil
UIApplication.shared.registerForRemoteNotifications()
// iOS 9 support or less
else
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
let deviceTokenString = deviceToken.reduce("", $0 + String(format: "%02X", $1))
print("APNs device token: \(deviceTokenString)")
然后你会收到通知
// Receive displayed notifications for iOS 10 devices.
//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
let userInfo = notification.request.content.userInfo
print("User Info = ",userInfo)
completionHandler([.alert, .badge, .sound])
//MARK:- Handle push notifications
//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
let userInfo = response.notification.request.content.userInfo
print("User Info = ",userInfo)
//handle the notification form here when cliked.
completionHandler()
【讨论】:
显示未知属性'转义'错误。而在 didRegisterForRemoteNotificationsWithDeviceToken 类型的“数据”方法值中没有成员“减少” 使用 Xcode 8.0 我还添加了 UserNotifications.framework 注册推送通知后是否还出现Showing Unknown attribute'escaping'错误? 您在哪个部分遇到了错误?你能发布更新的代码吗? //通知传递到前台应用时调用。 @available(iOS 10.0, *) func userNotificationCenter(_center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) let userInfo = notification.request.content.userInfo print("User Info = " ,userInfo) completionHandler([.alert, .badge, .sound]) 继续......【参考方案2】:简单的形式是
if #available(iOS 10, *)
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) (granted, error) in
application.registerForRemoteNotifications()
else
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
然后 IOS 10 提供了新的代理
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings)
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
//here we will get the device token
【讨论】:
以上是关于推送通知未在 iOS 10 设备中显示的主要内容,如果未能解决你的问题,请参考以下文章