分类 推送 下的文章

本地推送

发送通知

//
//  ViewController.swift
//  01-通知
//
//  Created by admin on 16/5/4.
//  Copyright © 2016年 snsnb. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    @IBAction func addLocalNotification(sender: UIButton) {

        /**
         // 消息发出的时间
         @NSCopying public var fireDate: NSDate?
         // 时区
         @NSCopying public var timeZone: NSTimeZone?
         // 通知周期
         public var repeatInterval: NSCalendarUnit // 0 means don't repeat
         @NSCopying public var repeatCalendar: NSCalendar?

         // location-based scheduling

         // 区域
         @available(iOS 8.0, *)
         @NSCopying public var region: CLRegion?

         // when YES, the notification will only fire one time. when NO, the notification will fire every time the region is entered or exited (depending upon the CLRegion object's configuration). default is YES.
         @available(iOS 8.0, *)
         public var regionTriggersOnce: Bool

         // 通知内容
         public var alertBody: String? // defaults to nil. pass a string or localized string key to show an alert
         // 是否显示 alertAction 设置的内容
         public var hasAction: Bool // defaults to YES. pass NO to hide launching button/slider
         // 设置提醒文字
         public var alertAction: String? // used in UIAlert button or 'slide to unlock...' slider in place of unlock
         // 设置从提醒进入app时的启动图片
         public var alertLaunchImage: String? // used as the launch image (UILaunchImageFile) when launch button is tapped
         @available(iOS 8.2, *)
         // 设置标题
         public var alertTitle: String? // defaults to nil. pass a string or localized string key

         // 声效
         public var soundName: String? // name of resource in app's bundle to play or UILocalNotificationDefaultSoundName

         // 数字
         public var applicationIconBadgeNumber: Int // 0 means no change. defaults to 0

         //
         public var userInfo: [NSObject : AnyObject]? // throws if contains non-property list types

         // category identifer of the local notification, as set on a UIUserNotificationCategory and passed to +[UIUserNotificationSettings settingsForTypes:categories:]
         @available(iOS 8.0, *)
         public var category: String?
        */
        // 创建通知
        var localNotification = UILocalNotification()

        // 设置时间
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)

        // 设置消息内容
        localNotification.alertBody = "哈哈哈,我是内容"
        // 设置消息标题
        if #available(iOS 8.2, *) {
            localNotification.alertTitle = "我是标题"
        } else {
            // Fallback on earlier versions
        }
        // 设置 alertAction
        localNotification.alertAction = "快点打开"
        // 设置 alertAction 是否显示,默认为显示
        localNotification.hasAction = false
        // 声效
        localNotification.soundName = UILocalNotificationDefaultSoundName
        // 图标数字
        localNotification.applicationIconBadgeNumber = 888
        // 传递自定义的信息
        localNotification.userInfo = ["body":localNotification.alertBody!,"badgeNumber":888]
        // 推送通知
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }
}

获取通知传递的值

//
//  AppDelegate.swift
//  01-通知
//
//  Created by admin on 16/5/4.
//  Copyright © 2016年 snsnb. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        /**
         public static var None: UIUserNotificationType { get } // the application may not present any UI upon a notification being received
         public static var Badge: UIUserNotificationType { get } // the application may badge its icon upon a notification being received
         public static var Sound: UIUserNotificationType { get } // the application may play a sound upon a notification being received
         public static var Alert: UIUserNotificationType { get } // the application may display an alert upon a notification being received

        */
        // ios8 之后需要授权
        if #available(iOS 8.0, *) {
            let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert,UIUserNotificationType.Sound,UIUserNotificationType.Badge], categories: nil)
            application.registerUserNotificationSettings(notificationSettings)
        }
        application.applicationIconBadgeNumber = 0
        // 只要是不是正常从图标点击进入的就会进入这个if
        if launchOptions != nil
        {
            // 注意,
            // print("通知进入")
            let view = UILabel(frame: CGRectMake(0, 120, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height - 120))
            view.backgroundColor = UIColor.darkGrayColor()
            view.numberOfLines = 0
            // 因为不能打印,所以在控件里面显示来查看效果
            // view.text = "\(launchOptions)"
            // view.text = "\(launchOptions![UIApplicationLaunchOptionsLocalNotificationKey])"
            // 可以取到传递的内容了,需要先转换下
            let notification = launchOptions![UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification;
            view.text = "\(notification.userInfo)"
            self.window?.rootViewController?.view.addSubview(view)
        }
        return true
    }
    // 从通知进入 app 的时候做相应的处理,在这里面进行处理的话,当程序被杀死的之后,再从通知进入应用,是无效的
    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        // 当应用处于活动状态时,直接 return ,后面就可以做相关的操作
        if application.applicationState == UIApplicationState.Active {
            return
        }
        // 当应用处于正在进入活动状态时,执行相应的操作
        if application.applicationState == UIApplicationState.Inactive {
            // print("跳转")
            // print(notification.userInfo)
        }
    }
}

远程推送

//
//  AppDelegate.swift
//  远程推送
//
//  Created by admin on 16/5/5.
//  Copyright © 2016年 snsnb. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        // 8.0 之后需要授权
        if #available(iOS 8.0, *) {
            // 初始化设置对象
            let setting = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge,UIUserNotificationType.Sound,UIUserNotificationType.Alert], categories: nil)
            // 注册
            application.registerUserNotificationSettings(setting)
            // 注册远程通知
            application.registerForRemoteNotifications()
        } else {
            UIApplication.sharedApplication().registerForRemoteNotificationTypes([UIRemoteNotificationType.Badge,UIRemoteNotificationType.Sound,UIRemoteNotificationType.Alert])
        }
        // 和本地消息一样,在程序被杀死的状态下,也需要在这里获取下传递来的消息
        if launchOptions != nil {
            let lableRect = UIScreen.mainScreen().bounds
            let lable = UILabel(frame: lableRect)
            lable.numberOfLines = 0
            lable.backgroundColor = UIColor.grayColor()
            // 全部信息
            // lable.text = "\(launchOptions)"
            // 传递来的消息
            lable.text = "\(launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey])"
            self.window?.rootViewController?.view.addSubview(lable)
        }
        return true
    }
    // 获得设备标识,把标识纪录下,在给apple服务器发送的时候需要用到
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
    {
        print(deviceToken.description)
    }
    // 获得传递的消息
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
    {
        // 可以打印消息
        print(userInfo)
    }
}