• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift本地通知、远程通知

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 无论是远程推送、本地推送 都需要注册通知代码

        iOS 8 把原先一步到位的 RemoteNotification 的注册分成两部分,一部分是注册新引入的那个「UIUserNotificationSettings」,另一部分是 RemoteNotifications

        事实上,Apple  iOS 8  RemoteNotification  LocalNotification 统一了起来。两种 Notifications 将统一由 UIUserNotificationSettings 来管理用户界面相关的东西:标记、声音和提醒。除了统一用户界面的通知外,UIUserNotificationSettings 还引入了 UIUserNotificationCategory,可以让用户方便的直接在 Notification 上进行一些快捷的操作(Action)。

        if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){

            application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

        }else{

            application.registerForRemoteNotificationTypes(.Alert | .Sound | .Badge)

        }

        

然后,在Appdelegate.swift 写相关远程推送、本地通知等代码

    // 收到本地通知

    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

        var alertView = UIAlertView(title: "系统本地通知", message: notification.alertBody, delegate: nil, cancelButtonTitle: "返回")

        alertView.show()

    }

     // 远程推送通知 注册成功

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

        println(deviceToken.description)

    }

    // 远程推送通知 注册失败

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {

        if error.code == 3010 {

            println("Push notifications are not supported in the iOS Simulator.")

        }else{

            println("application:didFailToRegisterForRemoteNotificationsWithError: \(error)")

        }

    }

    //   收到远程推送通知

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

        let notif    = userInfo as NSDictionary

        let apsDic   = notif.objectForKey("aps") as NSDictionary

        let alertDic = apsDic.objectForKey("alert") as String

        var alertView = UIAlertView(title: "系统本地通知", message: alertDic, delegate: nil, cancelButtonTitle: "返回")

        alertView.show()

    }

   

    // 注册通知 alertsoundbadge 8.0之后,必须要添加下面这段代码,否则注册失败)

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

        application.registerForRemoteNotifications()

    }

 

 

******************************本地通知方法****************************** 

    

//

//  TimeViewController.swift

//  UIControlDemo

//

//  Created by  on 14/12/10.

//  Copyright (c) 2014 马大哈. All rights reserved.

//

 

import UIKit

 

class TimeViewController: BaseViewController {

    var wordTextField:UITextField? //文字

    var dateTextField:UITextField? //时间

    var datePicker: UIDatePicker?

 

    override func viewDidLoad() {

        super.viewDidLoad()

        self.title = "时间/日期/本地通知"        

        let leftBarButton:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Trash, target: self, action: "locaNotifcationDeleteAll")

        self.navigationItem.rightBarButtonItem = leftBarButton;

        

        wordTextField = UITextField(frame: CGRectMake(50, 80, 200, 40))

        wordTextField?.backgroundColor = .clearColor()

        wordTextField!.tag = 100

        wordTextField?.borderStyle = UITextBorderStyle.RoundedRect

        wordTextField?.keyboardType = UIKeyboardType.Default

        wordTextField?.returnKeyType = UIReturnKeyType.Done

        wordTextField?.contentVerticalAlignment = UIControlContentVerticalAlignment.Center

        wordTextField?.clearButtonMode = UITextFieldViewMode.WhileEditing

        wordTextField?.secureTextEntry = false

        wordTextField?.textColor = .blackColor()

        wordTextField?.textAlignment = .Left

        wordTextField?.placeholder = "键盘"

        wordTextField?.font = UIFont.systemFontOfSize(15)

        self.view.addSubview(wordTextField!)

 

        dateTextField = UITextField(frame: CGRectMake(50, 140, 200, 40))

        dateTextField?.backgroundColor = .clearColor()

        dateTextField!.tag = 101

        dateTextField?.borderStyle = UITextBorderStyle.RoundedRect

        dateTextField?.keyboardType = UIKeyboardType.Default

        dateTextField?.returnKeyType = UIReturnKeyType.Done

        dateTextField?.contentVerticalAlignment = UIControlContentVerticalAlignment.Center

        dateTextField?.textColor = .blackColor()

        dateTextField?.textAlignment = .Left

        dateTextField?.placeholder = "日期picker"

        dateTextField?.font = UIFont.systemFontOfSize(15)

        self.view.addSubview(dateTextField!)

         

        datePicker = UIDatePicker()

        datePicker?.datePickerMode = .DateAndTime

        datePicker?.timeZone = NSTimeZone.systemTimeZone()

        datePicker?.addTarget(self, action: "datePickerSelected:", forControlEvents: UIControlEvents.ValueChanged)

        dateTextField!.inputView = datePicker

               

        var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

        button.frame = CGRectMake(200, 200, 50, 30)

        button.backgroundColor = UIColor.redColor()

        button.setTitleColor(UIColor.blackColor(), forState:.Normal)

        button.setTitle("保存", forState: UIControlState.Normal)

        button.titleLabel!.font = UIFont.boldSystemFontOfSize(CGFloat(20))

        button.showsTouchWhenHighlighted = true

        button.addTarget(self, action: "saveLocalNotificationButton", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

      

    }

 

    // 保存按钮方法

    func saveLocalNotificationButton(){

        var contentDic = ["KEY":"VALUE"]

        locaNotifcationSchedule(chedulDate: datePicker!.date, alertBody: "通知看到 : \(wordTextField!.text)", content: contentDic)

        var alertView = UIAlertView(title: "保存成功", message: nil, delegate: nil, cancelButtonTitle: "返回")

        alertView.show()

        wordTextField?.resignFirstResponder()

        dateTextField?.resignFirstResponder()

        

    }

     

    // 注册本地通知

    func locaNotifcationSchedule(#chedulDate: NSDate ,alertBody: String ,content: NSDictionary) {

        var localNotif = UILocalNotification()

        localNotif.fireDate = chedulDate

        localNotif.timeZone = NSTimeZone.defaultTimeZone()

        //        localNotif.repeatInterval = repeatInterval 0代表不重复

        localNotif.soundName"iPhone.caf"//此属性可以不写(默认系统声音UILocalNotificationDefaultSoundName

//        localNotif.hasAction = true;

//        localNotif.alertAction = "查看";

        localNotif.alertBody = alertBody

        localNotif.userInfo  = content

        UIApplication.sharedApplication().scheduleLocalNotification(localNotif) 

    }

    

    // 删除所有注册的本地通知

    func locaNotifcationDeleteAll() {

        let application = UIApplication.sharedApplication()

        application.cancelAllLocalNotifications()

        var alertView = UIAlertView(title: "所有本地通知都已移除", message: nil, delegate: nil, cancelButtonTitle: "返回")

        alertView.show()

    }

 

    // 动态改变textfield内容

    func datePickerSelected(datePicker:UIDatePicker){

        let dateString = timeDateFormatter().stringFromDate(datePicker.date)

        dateTextField!.text = dateString

    }

 

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

        wordTextField?.resignFirstResponder()

        dateTextField?.resignFirstResponder()

        

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        

    }

    

}

 

       

 

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
2.2 Swift的(Int)整形类型 [Swift原创教程]发布时间:2022-07-13
下一篇:
Swift搭建服务端发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap