【发布时间】:2015-10-25 02:12:29
【问题描述】:
我在正确更新应用内徽章图标标签时遇到了一些问题。我想要这样的东西:
只要收到推送通知,此红色图标就会出现。我在 iPhone 上的应用程序图标上正确获得了推送通知徽章;但是,只有当我按下推送通知的横幅,或者我已经在应用程序内时,应用程序内的这个红色图标才会出现。
我的问题是,如果我按下实际的应用程序图标,它不会出现。即使应用程序在后台,我也希望更新应用程序中的标签,就像 facebook 应用程序在通知地球图标顶部的图标一样。
我将在 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)
}
我非常感谢任何人的帮助,因为我已经坚持了几天了。
编辑:更改标题
【问题讨论】:
标签: ios swift parse-platform push-notification badge