我在iOS中使用通知Feed进行通知。我希望用户将邮件标记为已读取,以便徽章计数器重置为0.但是,当他们关闭应用程序时,徽章就会消失。一旦他们收到另一个通知,徽章计数器就会达到应该的数量。例如,如果我有10个通知并且我将其标记为已读,那么我的徽章应该保留为9.但它会完全消失。如果我收到另一个通知,那么它将返回到10.有没有办法可以让我的应用程序在申请结束时不清除徽章?
我将所有通知存储在sql数据库中,因此徽章编号在服务器端计算并随通知有效负载一起发送。 这是我的app委托文件。
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Request notifications
// iOS 10 support
if #available(iOS 10, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
application.registerForRemoteNotifications()
}/*else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}*/
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
print("Became active")
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// Called when APNs has assigned the device a unique token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Convert token to string
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
// Print it to console
print("APNs device token: \(deviceTokenString)")
print(Keychain.set(deviceTokenString, forKey: "device_token"))
if(Keychain.value(forKey: "user_id") != nil && Keychain.value(forKey: "device_token") != nil){
// send data to the server
let requestUrl = NSURL(string: "https://api.website.com/insert_ios_notification_token.php?user_id=\(Keychain.value(forKey: "user_id")!)&email=\(Keychain.value(forKey: "email")!)&password=\(Keychain.value(forKey: "password")!)&token=\(Keychain.value(forKey: "device_token")!)")
let request = NSMutableURLRequest(url: requestUrl! as URL)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
guard let _ = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response!)")
}
}
task.resume()
}
// Persist it in your backend in case it's new
}
// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Print the error to console (you should alert the user that registration failed)
print("APNs registration failed: \(error)")
}
// Push notification received
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
// Print notification payload data
print("Push notification received: \(data)")
}
}