gpt4 book ai didi

ios - iOS 10 设备中不显示推送通知

转载 作者:行者123 更新时间:2023-11-28 06:15:40 25 4
gpt4 key购买 nike

我是 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。试试这个

   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()
}

关于ios - iOS 10 设备中不显示推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45205308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com