Swift2.0推送

1.创建消息处理事件

varwindow:UIWindow? letNotificationCategoryIdent:Nsstring="ACTIONABLE" letNotificationActionOneIdent:Nsstring="ONE" letNotificationActionTwoIdent:Nsstring="TWO" funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{ //Overridepointforcustomizationafterapplicationlaunch. self.registerForNotification() returntrue } funcregisterForNotification(){ varleftAction:UIMutableuserNotificationAction=UIMutableuserNotificationAction()
    //这里需要注意的是你处理消息是在你对应的应用程序前台处理还是后台处理,
    //Foreground会跳进应的应用程序里面,Background则会在当前消息界面处理即苹果的主当前窗口。 

leftAction.activationMode=UIUserNotificationActivationMode.Foreground
leftAction.title="left"
leftAction.identifier=NotificationActionOneIdent
leftAction.destructive=false
leftAction.authenticationrequired=false

varrightAction:UIMutableuserNotificationAction=UIMutableuserNotificationAction()

rightAction.activationMode=UIUserNotificationActivationMode.Foreground
rightAction.title="right"
rightAction.identifier=NotificationActionTwoIdent
rightAction.destructive=false

rightAction.authenticationrequired=false

//这个Category很重要很重要,这个是后面进行消息通信联系起来的
varnotificationCategory:UIMutableuserNotificationCategory=UIMutableuserNotificationCategory()

notificationCategory.identifier=NotificationCategoryIdent

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

varcategories:NSSet=NSSet(object:notificationCategory)

lettypes:UIUserNotificationType= [.Alert,.Badge,.sound]

varnotificationSettings:UIUserNotificationSettings=UIUserNotificationSettings(forTypes:types,categories:categories)

UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
}

funcapplication(application:UIApplication,handleActionWithIdentifieridentifier:String?,
forlocalnotificationnotification:UIlocalnotification,completionHandler:()->Void){

ifidentifier==NotificationActionOneIdent{

NSNotificationCenter.defaultCenter().postNotificationName("ONE",object:nil)

}elseifidentifier==NotificationActionTwoIdent{

NSNotificationCenter.defaultCenter().postNotificationName("TWO",object:nil)

} completionHandler()
}


2.要处理的事件

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

funcnotification(){ 
     vardateNow:NSDate=NSDate(timeIntervalSinceNow:5) 
    varnotification:UIlocalnotification=UIlocalnotification() 
notification.fireDate=dateNow notification.hasAction=true notification.category="ACTIONABLE"//如果不把这两个category联系起来,新特性根本无法实现 notification.alertBody="新消息来了!" UIApplication.sharedApplication().schedulelocalnotification(notification) } funcTestShape(notification:NSNotification){ varview:UIView=UIView(frame:CGRectMake(100,100,100)); view.backgroundColor=UIColor.blueColor() self.view.addSubview(view) } funcTestMessage(notification:NSNotification){ varmessage: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) }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...