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
相关推荐
iOS8系统推送通知在上方显示的时候,下拉就可以立即快出处理,iMessage就是最好例子,当有人给你发送短信的时候就不用跳转到短信页面可以直接下拉回复,本Demo就是展示了iOS8的新的推送通知。
完整的介绍了如何ios push notification的使用过程。包括工程的编写,和server的端的介绍。希望对大家会有帮助。
用于传参,个人觉得比delegate方便得多,也比较容易理解,用于不同页面间的传递参数很简易
PHP代码发送IOS推送消息 PUSH IOS NOTIFICATION
页面a点击 控件 打开页面 b,在页面b中输入内容,把值传回a中显示,传值机制使用 notification,初学者比看,学习xcode的好例子
iOS传值,用通知Notifaction正向传值。
Notification使用的关键是利用ios的消息中心,发送消息通知,观察者接收到消息通知,执行对应的方法。NSNotificationCenter 较之于 kvo 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。
FFToast是一个非常强大的iOS message notifications和AlertView扩展。它可以很容易实现从屏幕顶部、屏幕底部和屏幕中间弹出一个通知。你可以很容易的自定义弹出的View. 由于详细描述有文字要求没法发。详细使用教程...
iOS传值,用Notifaction反向传值,将B界面的值传到A界面。
主要为大家详细介绍了iOS Remote Notification远程消息推送处理,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://wang-peng1.iteye.com/blog/1741757
AJNotificationView, iOS通知组件 AJNotificationViewiOS的通知组件不需要图像,所有CoreGraphics代码适用于iPhone和 iPad ( 分辨率独立)动画 background 类型一次只显示一个通知。 创建通知时,该通知将添加到队
72. An iOS 10 Local Notification Tutorial 73. An Overview of iOS 10 Application State Preservation and Restoration 74. An iOS 10 State Preservation and Restoration Tutorial 75. Integrating Maps into ...
通过使用NSNotification通知类,可以实现一对多的传递关系,同时也能很好的避免类与类之间层层递进的关系。但是必须记得在使用过程中可能出现通知会被多次接收,并执行相关的方法,避免这种情况出现必须在接收通知前...
iOS10NotificationSample 关于iOS 10通知的示例代码注意:除非应用程序在主NotificationSample目标上将开发团队更新为自己的开发团队,然后将适当的应用程序组添加到您帐户的列表中,否则允许应用程序使用共享容器的...
一个小熊啊的通知机制 用起来方便 怎么用呢 注册,调用等 看代码 就知道了 在一个类里 通过通知可以调用另一个类里的方法
主要为大家详细介绍了iOS10添加本地推送(Local Notification)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了IOS 打包出现Missing Push Notification Entitlement 问题解决方案的相关资料,需要的朋友可以参考下