Swift2.0推送

本文深入探讨了iOS应用中消息处理与通知机制的实现方式,包括如何注册通知、处理不同类型的用户交互动作以及实现消息的本地化显示与响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建消息处理事件

var window: UIWindow?          let NotificationCategoryIdent:NSString = "ACTIONABLE"     let NotificationActionOneIdent:NSString = "ONE"     let NotificationActionTwoIdent:NSString = "TWO"          func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {         // Override point for customization after application launch.         self.registerForNotification()                            return true     }          func registerForNotification(){         var leftAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    //这里需要注意的是你处理消息是在你对应的应用程序前台处理还是后台处理,
    //Foreground会跳进应的应用程序里面,Background则会在当前消息界面处理即苹果的主当前窗口。    
   
leftAction.activationMode = UIUserNotificationActivationMode.Foreground   
   leftAction.title = "left"       
 leftAction.identifier = NotificationActionOneIdent   
    leftAction.destructive = false      
 leftAction.authenticationRequired = false        
   
var rightAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    
   rightAction.activationMode = UIUserNotificationActivationMode.Foreground     
   rightAction.title = "right"       
 rightAction.identifier = NotificationActionTwoIdent        
rightAction.destructive = false   
    
rightAction.authenticationRequired = false  
     
//这个Category很重要很重要,这个是后面进行消息通信联系起来的      
  var notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory() 
  
   notificationCategory.identifier = NotificationCategoryIdent        

var actionArray:NSArray = [rightAction,leftAction]        
notificationCategory.setActions(actionArray, forContext: UIUserNotificationActionContext.Default)                

 var categories:NSSet = NSSet(object:notificationCategory)   
     
let types:UIUserNotificationType = [.Alert,.Badge,.Sound]
  
var notificationSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes:types,categories:categories)        

UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)         
}
 
func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
 forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
           
     if identifier == NotificationActionOneIdent {           

 NSNotificationCenter.defaultCenter().postNotificationName("ONE",object: nil)      
 
}else if identifier == NotificationActionTwoIdent { 
         
  NSNotificationCenter.defaultCenter().postNotificationName("TWO",object: nil)      

  } completionHandler()         
}


2.要处理的事件

override func viewDidLoad() {    
     super.viewDidLoad()         
     
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "TestShape:", name: "ONE", object: nil)        
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "TestMessage:", name: "TWO", object: nil)  
     self.notification()                       
} 
     

func notification() {                  
     var dateNow:NSDate = NSDate(timeIntervalSinceNow: 5)        
     var notification:UILocalNotification = UILocalNotification()         
notification.fireDate = dateNow        
notification.hasAction = true        
notification.category =  "ACTIONABLE"//如果不把这两个category联系起来,新特性根本无法实现        
notification.alertBody = "新消息来了!"        
UIApplication.sharedApplication().scheduleLocalNotification(notification)
            
 }

        
func TestShape(notification: NSNotification) { 

var view:UIView = UIView(frame: CGRectMake(100,100,100,100));        
view.backgroundColor = UIColor.blueColor()        
self.view.addSubview(view)
            
}
        
func TestMessage(notification: NSNotification) {  
             
  var message: UIAlertController = UIAlertController(title: "Hello", message: "推送通知",
 preferredStyle: UIAlertControllerStyle.Alert) 
      
message.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))     
  
self.presentViewController(message, animated: true, completion: nil)            
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值