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(){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) }vardateNow:NSDate=NSDate(timeIntervalSinceNow:5) varnotification:UIlocalnotification=UIlocalnotification()
以上是内存溢出为你收集整理的Swift2.0推送全部内容,希望文章能够帮你解决Swift2.0推送所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)