美文网首页iOS学习笔记
IOS实现本地推送

IOS实现本地推送

作者: Lucky灬Candy | 来源:发表于2016-08-24 15:11 被阅读175次

本地推送(UILocalNotification)和远程推送其目的都是要提醒用户去做某件事情,其本质区别有两点:(1)是否需要联网?(2)触发者是谁?简单来说,本地推送是由用户(App使用者)触发,不需要联网,类似的比如ToDoList,一对一推送;而远程推送触发者是App运营者,为了宣传产品提高用户粘合度,推送形式单对多,需要联网。

Step1:iOS8之后推送要求必须注册App支持的用户交互类型,注册代码和远程推送注册代码相同如下

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge,UIUserNotificationType.Alert,UIUserNotificationType.Sound]categories: [category]))

如果没有特殊需求(比如使用到UIMutableUserNotificationAction)可以将后面的categories设置为nil
在介绍下一步之前有必要说一下UILocalNotification的基本属性便于大家理解使用

属性名 类型 备注 英文原版
fireDate NSDate? 代表推送的启动时间 timer-based scheduling
timeZone NSTimeZone? 说明设置的fireDate在哪个时区 the time zone to interpret fireDate in
repeatInterval NSCalendarUnit 重复次数 nil
repeatCalendar NSCalendar? 重复推送时间 nil
alertBody String? 通知内容 pass a string or localized string key to show an alert
alertAction String? 解锁滑动时的事件 used in UIAlert button or 'slide to unlock...' slider in place of unlock
soundName String 消息推送提示声音 name of resource in app's bundle to play or UILocalNotificationDefaultSoundName
alertLaunchImage String? 启动图片,设置此字段点击通知时会显示该图片 used as the launch image (UILaunchImageFile) when launch button is
alertTitle String? 消息标题,默认无 defaults to nil. pass a string or localized string key
applicationIconBadgeNumber Int App icon的角标 0 means no change. defaults to 0
userInfo [NSObject : AnyObject]? 自定义参数 nil

Step2:添加推送通知

@IBAction func sendLocalNotification(sender: UIButton) {
        
        let localNotification = UILocalNotification();
        //触发通知时间
        localNotification.fireDate = NSDate(timeIntervalSinceNow:5);
        //重复间隔
        //    localNotification.repeatInterval = kCFCalendarUnitMinute;
        localNotification.timeZone = NSTimeZone.defaultTimeZone();
        //通知内容
        localNotification.alertBody = "本地消息推送测试";
        localNotification.applicationIconBadgeNumber = 1;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        //通知参数
        localNotification.userInfo = ["message": "本地消息"];
        
        localNotification.category = "categoryIdentifier";
        
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification);
    }

至此,本地推送已经完成,但需要说明的是,如果推送发生时,App处于Foreground会直接调用

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

如果App处于Background状态时,此时触发推送会在通知栏产生一条消息,当你点击推送消息后才会触发上面的方法。

如果你想实现特殊的消息推送,比如快速点赞、回复等,可使用UIMutableUserNotificationAction定义不同的action并指定其唯一的identifier,因为无需打开App可直接将action的activationMode设置为Background,处理函数为:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void){
        if identifier == "yourIdentifier"{
            //do something..
        }else{
            //else do...
        }
        completionHandler()
    }

相关文章

  • 本地推送闹钟功能实现

    本地推送闹钟功能实现 在ios10下使用UserNotifications用本地推送实现闹钟功能,只是实现了简单的...

  • iOS8-推送(本地和远程)的简单使用

    参考文章:本地推送:一、iOS推送之本地推送(iOS Notification Of Local Notifica...

  • IOS实现本地推送

    本地推送(UILocalNotification)和远程推送其目的都是要提醒用户去做某件事情,其本质区别有两点:(...

  • iOS 本地推送

    iOS 本地推送 ------------------------------------------------...

  • APNS

    在iOS开发中,消息推送有两种形式,一种是本地推送,一种是远程推送。 本地推送相对来说比较简单,不要要联网就可实现...

  • iOS本地推送

    iOS本地推送 第一步:创建本地推送 // 创建一个本地推送 UILocalNotifica...

  • iOS10 本地推送你玩过了吗?

    首先来看一下iOS10的推送的基本的实现,和之前的推送有啥差别。 权限申请iOS8之前,远程推送和本地推送是区分对...

  • iOS远程推送之(一):APNs原理和基本配置

    一、前言 iOS中消息推送有两种方式,本地推送和远程推送。本地推送在iOS中使用本地通知为你的APP添加提示用户功...

  • iOS 推送通知及通知扩展

    级别: ★★☆☆☆标签:「iOS 本地推送」「iOS 远程推送」「iOS通知扩展」作者: dac_1033审校: ...

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

网友评论

    本文标题:IOS实现本地推送

    本文链接:https://www.haomeiwen.com/subject/pbgnsttx.html