如何在swift中管理本地通知

时间:2016-07-29 10:15:21

标签: ios iphone swift2 xcode7 uilocalnotification

当应用程序收到推送通知时,我正在创建本地通知。这些本地通知在应用程序处于前台时生成,当我同时创建本地通知时,didReceiveLocalNotification方法正在调用并且难以管理本地通知并点击/点击位置同一事件调用两次。我还需要避免重复的本地通知。

请帮我解决这个问题。

 //MARK: - Delegate Method For APNS Notification
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        print("Notification Message \(userInfo)")

        let aps = userInfo["aps"] as! [String: AnyObject]

        // 1
        if  userInfo["postData"] != nil {

            // Refresh Promotions 
            print("Got it...")

        // Clear Previous Value Data
        postData.removeAll()

        //Adding New Post Data here
        if (userInfo["postData"] != nil){
            self.postData = userInfo["postData"]! as! [String : AnyObject]
            print(self.postData)
        }

        //Condition here for Notification
        if appInForeground == false {

            //Goto Promo List
            //Set Boolean for View
            notiDetails = true

            //Navigation
            gotoPromoListView()
        }else{

            let systemSoundID: SystemSoundID = 1016
            // to play sound
            AudioServicesPlaySystemSound (systemSoundID)
            AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

            let notification = UILocalNotification()
            notification.alertBody = (aps["alert"] as? String)!
            notification.soundName = UILocalNotificationDefaultSoundName
            notification.userInfo = userInfo["postData"]! as! [String : AnyObject]
       UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

如何管理此方法以进行本地通知

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        // Do something serious in a real app.
        print("Received Local Notification:")
        print(notification.userInfo)
        self.postData = notification.userInfo as! [String : AnyObject]
        didTapNotification()
    }

1 个答案:

答案 0 :(得分:0)

您可以在注册远程通知的位置显示您的代码吗?

如果您想显示提醒和徽章,在收到推送通知时,您可以这样做:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let notificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
        let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

        return true
    }

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        UIApplication.sharedApplication().registerForRemoteNotifications()
    }

之后,您可以向方法添加自定义操作:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : 
}

如果您需要本地通知,则应在安排通知时设置fire属性:

let localNotification = UILocalNotification()
      localNotification.fireDate = NSDate()
      localNotification.timeZone = NSTimeZone.defaultTimeZone()
      localNotification.alertBody = text
      localNotification.soundName = UILocalNotificationDefaultSoundName
      UIApplication.sharedApplication().scheduleLocalNotification(localNotification)