[英]Push notification in-app badge icon not updating when clicking on app icon
我在獲取應用內徽章圖標標簽正確更新時遇到了一些麻煩。 我想要這樣做的東西:
每當收到推送通知時,都會顯示此紅色圖標。 我正確地在iPhone上的應用程序圖標上獲得了推送通知徽章; 但是,僅當我按橫幅上的推送通知時,或者如果我已經在應用程序中時,該應用程序中的紅色圖標才會出現。
我的問題是,如果我按實際的應用程序圖標,它不會出現。 我希望即使在應用程序處於后台的情況下,也要更新應用程序內的標簽,就像facebook應用程序在Notifications Globe圖標上方具有圖標的方式一樣。
我將在AppDelegate中顯示相關方法(省略令牌等):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let userInfo: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey]
if userInfo != nil {
handleRemoteNotifications(application, userInfo: userInfo! as! NSDictionary)
return true
}
if application.applicationState != UIApplicationState.Background {
let oldPushHandlerOnly = !self.respondsToSelector(Selector("application:didReceiveRemoteNotification:fetchCompletionHandler:"))
let noPushPayload: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey]
if oldPushHandlerOnly || noPushPayload != nil {
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
}
}
return true
}
func handleRemoteNotifications(application: UIApplication, userInfo: NSDictionary) {
if let type: String = userInfo["type"] as? String {
switch (type) {
case "follow":
NSNotificationCenter.defaultCenter().postNotificationName("commentNotification", object: self)
case "comment":
NSNotificationCenter.defaultCenter().postNotificationName("commentNotification", object: self)
default:
return
}
}
}
func applicationDidBecomeActive(application: UIApplication) {
if (application.applicationIconBadgeNumber != 0) {
application.applicationIconBadgeNumber = 0
}
let installation = PFInstallation.currentInstallation()
if installation.badge != 0 {
installation.badge = 0
installation.saveEventually()
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
if let badgeNumber: Int = userInfo["badge"] as? Int {
application.applicationIconBadgeNumber = badgeNumber
}
handleRemoteNotifications(application, userInfo: userInfo)
if application.applicationState == .Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayloadInBackground(userInfo, block: nil)
}
handler(.NewData)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if application.applicationState == .Inactive {
// The application was just brought from the background to the foreground,
// so we consider the app as having been "opened by a push notification."
PFAnalytics.trackAppOpenedWithRemoteNotificationPayloadInBackground(userInfo, block: nil)
handleRemoteNotifications(application, userInfo: userInfo)
}
}
在ViewController中 ,我在viewDidAppear中調用方法,並使其在每次接收到推送通知時更新標簽並將數字增加1:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
followLabelNumber = NSUserDefaults.standardUserDefaults().integerForKey("followNumberKey")
commentLabelNumber = NSUserDefaults.standardUserDefaults().integerForKey("commentNumberKey")
NSNotificationCenter.defaultCenter().removeObserver(self, name: "followNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"followNotificationReceived:", name:"followNotification", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "commentNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"commentNotificationReceived:", name:"commentNotification", object: nil)
self.navigationController?.navigationBarHidden = true
}
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementer() -> Int {
runningTotal += amount
return runningTotal
}
return incrementer
}
func followNotificationReceived(notification: NSNotification) {
if let number = followLabelNumber {
let aNumber = makeIncrementer(forIncrement: 1)
followLabelNumber = number + aNumber()
NSUserDefaults.standardUserDefaults().setInteger(followLabelNumber!, forKey: "followNumberKey")
NSUserDefaults.standardUserDefaults().synchronize()
profileNotificationLabel.hidden = false
profileNotificationLabel.text = String(followLabelNumber!)
hasReceivedFollowNotification = true
}
}
func commentNotificationReceived(notification: NSNotification) {
if let number = commentLabelNumber {
let aNumber = makeIncrementer(forIncrement: 1)
commentLabelNumber = number + aNumber()
NSUserDefaults.standardUserDefaults().setInteger(commentLabelNumber!, forKey: "commentNumberKey")
NSUserDefaults.standardUserDefaults().synchronize()
commentsNotificationLabel.hidden = false
commentsNotificationLabel.text = String(commentLabelNumber!)
hasReceivedCommentNotification = true
}
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
非常感謝任何人的幫助,因為我已經堅持了幾天。
編輯:更改標題
首先,由於您正在使用Parse,因此您不需要自己處理application.applicationIconBadgeNumber
:
//You don't need these...
if (application.applicationIconBadgeNumber != 0) {
application.applicationIconBadgeNumber = 0
}
//Because these lines will set the application's icon badge to zero
let installation = PFInstallation.currentInstallation()
if installation.badge != 0 {
installation.badge = 0
installation.saveEventually()
}
您也不需要:
//Because Parse handles that for you
if let badgeNumber: Int = userInfo["badge"] as? Int {
application.applicationIconBadgeNumber = badgeNumber
}
另外,問題似乎是您在加載View Controller時沒有更新按鈕的標志。 您只有在收到新通知並且View Controller可見時才更新它們。 簡而言之,在您的View Controller上嘗試以下操作:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
followLabelNumber = NSUserDefaults.standardUserDefaults().integerForKey("followNumberKey")
commentLabelNumber = NSUserDefaults.standardUserDefaults().integerForKey("commentNumberKey")
//BEGIN SUGGESTED CODE
profileNotificationLabel.hidden = followLabelNumber > 0
profileNotificationLabel.text = String(followLabelNumber!)
commentsNotificationLabel.hidden = commentLabelNumber > 0
commentsNotificationLabel.text = String(commentLabelNumber!)
//END SUGGESTED CODE
NSNotificationCenter.defaultCenter().removeObserver(self, name: "followNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"followNotificationReceived:", name:"followNotification", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "commentNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"commentNotificationReceived:", name:"commentNotification", object: nil)
self.navigationController?.navigationBarHidden = true
}
最后但並非最不重要的一點是,每當收到遠程通知時,就將其傳遞到AppDelegate中的 func handleRemoteNotifications(application: UIApplication, userInfo: NSDictionary)
。 反過來,這會將NSNotification
到正在監聽的對象。 但是,可能存在或可能沒有ViewController,因為在應用程序處於后台時可能已將其釋放。 因此,當您收到遠程通知時,這些代碼行將永遠不會被調用:
NSUserDefaults.standardUserDefaults().setInteger(followLabelNumber!, forKey: "followNumberKey")
NSUserDefaults.standardUserDefaults().synchronize()
嘗試將以上各行移至AppDelegate的func handleRemoteNotifications(application: UIApplication, userInfo: NSDictionary)
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.