Swift:推送通知计数器清除了哪种方法?

时间:2016-03-30 09:54:48

标签: ios swift apple-push-notifications

我的iOS应用处理推送通知,如下所示:

func showNotificationDetails(userInfo: [NSObject : AnyObject], application:UIApplication) {
    print("showNotificationDetails")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let notificationController = storyboard.instantiateViewControllerWithIdentifier("DynamicEventsViewController") as! DynamicEventsViewController
    notificationController.isLoadedFromNotification = true
    notificationController.eventTitle = userInfo["aps"]!["alert"] as! String
    notificationController.eventDescription = userInfo["aps"]!["message"] as! String
    notificationController.isLoadedFromNotification = true

    if let tabBarController = self.window?.rootViewController {
        tabBarController.presentViewController(notificationController, animated: true, completion: nil)
    }

    application.applicationIconBadgeNumber = 0
    application.cancelAllLocalNotifications()

}


func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    let state:UIApplicationState = application.applicationState
    if (state == UIApplicationState.Inactive || state == UIApplicationState.Background) {
        self.showNotificationDetails(userInfo, application: application)
    }
    else if (state == UIApplicationState.Active) {
        let alertNewNotification = UIAlertController(title: "New notification received!", message: "Would you like to see details?", preferredStyle: .Alert)
        alertNewNotification.addAction(UIAlertAction(title: "Show", style: .Default, handler: { (action) -> Void in
            self.showNotificationDetails(userInfo, application: application)
        }))
        alertNewNotification.addAction(UIAlertAction(title: "Later", style: .Default, handler: { (action) -> Void in
            alertNewNotification.dismissViewControllerAnimated(true, completion: nil)
        }))
        if let tabBarController = self.window?.rootViewController {
            tabBarController.presentViewController(alertNewNotification, animated: true, completion: nil)
        }
    }
}

即使用户点击Later,通知计数器仍会设置为0,并且不再有通知横幅,因此永远丢失。为什么呢?

2 个答案:

答案 0 :(得分:1)

通知横幅仅在应用未激活时显示

所以你应该自己处理这个通知

答案 1 :(得分:0)

您可以尝试使用此TWMessageBarManager

当您同时显示来自didReceiveRemoteNotification方法的提醒时,您可以调用此库的消息以显示通知横幅

您展示的示例代码可以是这样的:

TWMessageBarManager.sharedInstance().showMessageWithTitle("Title", description: "Description", type: TWMessageBarMessageType.Info)

如果您想显示showNotificationDetails的数据,您也可以在该方法中调用