`
啸笑天
  • 浏览: 3437975 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

iOS8 notification

 
阅读更多

  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

 

 

 

  • 大小: 179.8 KB
分享到:
评论
4 楼 啸笑天 2016-07-29  
How to handle remote notification with background mode enabled  http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/


Will iOS launch my app into the background if it was force-quit by the user?  http://stackoverflow.com/questions/19068762/will-ios-launch-my-app-into-the-background-if-it-was-force-quit-by-the-user
3 楼 啸笑天 2016-07-20  
https://onevcat.com/2013/08/ios7-background-multitask/  WWDC 2013 Session笔记 - iOS7中的多任务
2 楼 啸笑天 2016-07-20  
{
     "aps" : { 
         "alert"              :              {   // string or dictionary
            "title"          :   "string"
            "body"           :   "string",
            "title-loc-key"  :   "string or null"
            "title-loc-args" :   "array of strings or null"
            "action-loc-key" :   "string or null"
            "loc-key"        :   "string"
            "loc-args"       :   "array of strings"
            "launch-image"   :   "string"
         },
          "badge"             :    number,
          "sound"             :    "string"
          "content-available" :    number;
          "category"          :    "string"
     },
}

aps:推送消息必须有的key

alert:推送消息包含此key值,系统就会根据用户的设置展示标准的推送信息
badge:在app图标上显示消息数量,缺少此key值,消息数量就不会改变,消除标记时把此key对应的value设置为0
sound:设置推送声音的key值,系统默认提示声音对应的value值为default
content-available:此key值设置为1,系统接收到推送消息时就会调用不同的回调方法,iOS7之后配置后台模式
category:UIMutableUserNotificationCategory's identifier 可操作通知类型的key值

title:简短描述此调推送消息的目的,适用系统iOS8.2之后版本
body:推送的内容
title-loc-key:功能类似title,附加功能是国际化,适用系统iOS8.2之后版本
title-loc-args:配合title-loc-key字段使用,适用系统iOS8.2之后版本
action-loc-key:可操作通知类型key值,不详细叙述
loc-key:参考title-loc-key
loc-args:参考title-loc-args
launch-image:点击推送消息或者移动事件滑块时,显示的图片。如果缺少此key值,会加载app默认的启动图片。
1 楼 啸笑天 2016-07-20  
http://ios.jobbole.com/83952/ iOS推送之远程推送
http://ios.jobbole.com/83949/   iOS推送之本地推送

相关推荐

    iOS8 Notification

    iOS8系统推送通知在上方显示的时候,下拉就可以立即快出处理,iMessage就是最好例子,当有人给你发送短信的时候就不用跳转到短信页面可以直接下拉回复,本Demo就是展示了iOS8的新的推送通知。

    iOS push notification 文档

    完整的介绍了如何ios push notification的使用过程。包括工程的编写,和server的端的介绍。希望对大家会有帮助。

    ios5 Notification示例

    用于传参,个人觉得比delegate方便得多,也比较容易理解,用于不同页面间的传递参数很简易

    PHP代码发送IOS推送消息 PUSH IOS NOTIFICATION

    PHP代码发送IOS推送消息 PUSH IOS NOTIFICATION

    iOS开发 notification在2个页面之间传值

    页面a点击 控件 打开页面 b,在页面b中输入内容,把值传回a中显示,传值机制使用 notification,初学者比看,学习xcode的好例子

    iOS Notification正向传值

    iOS传值,用通知Notifaction正向传值。

    ios kvo和Notification的使用demo

    Notification使用的关键是利用ios的消息中心,发送消息通知,观察者接收到消息通知,执行对应的方法。NSNotificationCenter 较之于 kvo 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。

    ios-强大的iOS Notification和AlertView框架.zip

    FFToast是一个非常强大的iOS message notifications和AlertView扩展。它可以很容易实现从屏幕顶部、屏幕底部和屏幕中间弹出一个通知。你可以很容易的自定义弹出的View. 由于详细描述有文字要求没法发。详细使用教程...

    iOS Notification反向传值

    iOS传值,用Notifaction反向传值,将B界面的值传到A界面。

    iOS Remote Notification远程消息推送处理

    主要为大家详细介绍了iOS Remote Notification远程消息推送处理,感兴趣的小伙伴们可以参考一下

    iOS 自带pushnotification 也就是apns

    NULL 博文链接:https://wang-peng1.iteye.com/blog/1741757

    AJNotificationView, iOS通知组件.zip

    AJNotificationView, iOS通知组件 AJNotificationViewiOS的通知组件不需要图像,所有CoreGraphics代码适用于iPhone和 iPad ( 分辨率独立)动画 background 类型一次只显示一个通知。 创建通知时,该通知将添加到队

    iOS 10 App Development Essentials

    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 ...

    Notification

    通过使用NSNotification通知类,可以实现一对多的传递关系,同时也能很好的避免类与类之间层层递进的关系。但是必须记得在使用过程中可能出现通知会被多次接收,并执行相关的方法,避免这种情况出现必须在接收通知前...

    iOS10NotificationSample:关于iOS 10通知的示例代码

    iOS10NotificationSample 关于iOS 10通知的示例代码注意:除非应用程序在主NotificationSample目标上将开发团队更新为自己的开发团队,然后将适当的应用程序组添加到您帐户的列表中,否则允许应用程序使用共享容器的...

    notification Demo

    一个小熊啊的通知机制 用起来方便 怎么用呢 注册,调用等 看代码 就知道了 在一个类里 通过通知可以调用另一个类里的方法

    iOS10添加本地推送(Local Notification)实例

    主要为大家详细介绍了iOS10添加本地推送(Local Notification)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    IOS 打包出现Missing Push Notification Entitlement 问题解决方案

    主要介绍了IOS 打包出现Missing Push Notification Entitlement 问题解决方案的相关资料,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics