iOS8拥有了全新的通知中心,有全新的通知机制。当屏幕顶部收到推送时只需要往下拉,就能看到快速操作界面,并不需要进入该应用才能操作。在锁屏界面,对于推送项目也可以快速处理。基本上就是让用户尽量在不离开当前页面的前提下处理推送信息,再次提高处理效率。
能够进行直接互动的短信、邮件、日历、提醒,第三方应用,可以让你不用进入程序就能进行快捷操作,并专注于手中正在做的事情。
- 在通知横幅快速回复信息,不用进入短信程序;
- 可直接拒绝或接受邮件邀请;
- 可对提醒进行标记为完成或推迟;
- 当第三方应用更新接口后便可直接对应用进行快速操作。
用户推送:
//1.创建消息上面要添加的动作(按钮的形式显示出来) let firstAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction() firstAction.identifier = "action";//按钮的标示 firstAction.title = "Accept";//按钮的标题 firstAction.activationMode = UIUserNotificationActivationMode.Foreground;//当点击的时候启动程序 // firstAction.authenticationRequired = true; // firstAction.destructive = true; let secondAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction() secondAction.identifier = "action2"; secondAction.title = "Reject"; secondAction.activationMode = UIUserNotificationActivationMode.Background;//当点击的时候不启动程序,在后台处理 secondAction.authenticationRequired = true;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略; secondAction.destructive = true; //2.创建动作(按钮)的类别集合 let category: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory() category.identifier = "alert" category.setActions([firstAction, secondAction], forContext: UIUserNotificationActionContext.Minimal) //3.创建UIUserNotificationSettings,并设置消息的显示类类型 let uns: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: (UIUserNotificationType.Alert|UIUserNotificationType.Badge|UIUserNotificationType.Sound), categories: NSSet(object: category)) //4.注册推送 application.registerUserNotificationSettings(uns) /////////////////////////////////////////////////////////////////// // //5.发起本地推送消息 UIApplication.sharedApplication().cancelAllLocalNotifications() let notification = UILocalNotification() let timeIntervalSinceNow = Double(seconds) notification.fireDate = NSDate(timeIntervalSinceNow:timeIntervalSinceNow); notification.soundName = UILocalNotificationDefaultSoundName; notification.timeZone = NSTimeZone.systemTimeZone(); notification.alertBody = "计时完成!!!"; notification.category = "alert"; notification.userInfo = ["info":"开饭啦"] UIApplication.sharedApplication().scheduleLocalNotification(notification);
远程推送:
UserRequires call to registerUserNotificationSettings:• SilentInfo.plist UIBackgroundModes array contains remote-notification• Can use both(一、用户需要调用注册用户推送registerUserNotificationSettings;二、在info.plist文件中UIBackgroundModes必须包含远程通知)
离线push数据包带上特定Category字段(字段内容需要前后台一起定义,必须要保持一致),手机端收到时,就能展示上述代码对应Category设置的按钮,和响应按钮事件。
// payload example: {"aps":{"alert":"Incoming call", "sound":"default", "badge": 1, "category":"incomingCall"}}
重大修改: 离线push数据包之前能带的数据最多为256字节,现在APPLE将该数值放大到2KB。 这个应该是只针对IOS8的。
[[UIApplication sharedApplication] registerForRemoteNotifications];//注册远程通知
地理位置推送:
这个推送是新的API才有的特性,必须配合CLLocation定位一起使用。(导入CoreLocation.framework)
//注册地理位置推送 let locationMan:CLLocationManager = CLLocationManager() locationMan.delegate = self locationMan.requestWhenInUseAuthorization()
//地理位置推送通知
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
let canUseLocationNotifications = (status == CLAuthorizationStatus.AuthorizedWhenInUse);
if (canUseLocationNotifications) {
startShowLocationNotification();
}
}
func startShowLocationNotification()
{
var local2D: CLLocationCoordinate2D = CLLocationCoordinate2DMake( 123.0, 123.0)
var locNotification: UILocalNotification = UILocalNotification();
locNotification.alertBody = "你接收到了";
locNotification.regionTriggersOnce = true;
locNotification.region = CLCircularRegion(center: local2D, radius: 45, identifier: "local-identity")
UIApplication.sharedApplication().scheduleLocalNotification(locNotification);
}
如果没有开启Core Location 那么上面的didReceiveLocalNotification不会被调用
AppDelegate.m里面对通知结果进行处理:
//本地推送通知 func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings){ //成功注册registerUserNotificationSettings:后,回调的方法 println("%@",notificationSettings); } func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification){ //收到本地推送消息后调用的方法 println("%@",notification); let dict:[NSObject : AnyObject]? = notification.userInfo!; if ((dict) != nil) { let alert = UIAlertView() alert.title = "开饭啦"//String(dict!["info"]) // alert.message = "开饭啦" alert.addButtonWithTitle("OK") alert.show() } let region: CLRegion? = notification.region; if (region != nil) { let alert = UIAlertView() alert.title = "你的位置"//String(dict!["info"]) // alert.message = "开饭啦" alert.addButtonWithTitle("OK") alert.show() } } func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void){ //在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容 println("%@----%@",identifier,notification); completionHandler();//处理完消息,最后一定要调用这个代码块 } //远程推送通知 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData){ //向APNS注册成功,收到返回的deviceToken var token: String = NSString(format: "%@", [deviceToken]); token = token.stringByReplacingOccurrencesOfString( "<", withString: "", options: nil, range: nil) token = token.stringByReplacingOccurrencesOfString( ">", withString: "", options: nil, range: nil) token = token.stringByReplacingOccurrencesOfString( " ", withString: "", options: nil, range: nil) println(token) } func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError){ //向APNS注册失败,返回错误信息error } func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){ //收到远程推送通知消息 } func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void){ //在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮 }
当app被kill后启动接收通知:
let localNotify: UILocalNotification? = launchOptions![UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification; if(localNotify != nil) { } let userInfo: AnyObject? = launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey]; if(userInfo != nil) { }
感谢:http://blog.csdn.net/yujianxiang666/article/details/35260135