Last active
June 30, 2016 03:49
-
-
Save chuck0523/50d9f810fb58b46d32f162b0f428b72b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// 008 | |
// | |
// Created by chuck on 6/30/16. | |
// Copyright © 2016 chuck. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// アラート表示の許可をもらう | |
let setting = UIUserNotificationSettings(forTypes: [.Sound, .Alert], categories: nil) | |
UIApplication.sharedApplication().registerUserNotificationSettings(setting) | |
// すぐにNotificationが発火するボタンを作成する。 | |
let myNotificationButton: UIButton = UIButton(frame: CGRectMake(0,0,200,80)) | |
myNotificationButton.backgroundColor = UIColor.orangeColor() | |
myNotificationButton.layer.masksToBounds = true | |
myNotificationButton.setTitle("Notification", forState: .Normal) | |
myNotificationButton.layer.cornerRadius = 20.0 | |
myNotificationButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: 200) | |
myNotificationButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) | |
myNotificationButton.tag = 1 | |
// 時間をおいてNotificationを発火するボタンを作成する。 | |
let myNotificationFireButton: UIButton = UIButton(frame: CGRectMake(0,0,200,80)) | |
myNotificationFireButton.backgroundColor = UIColor.blueColor() | |
myNotificationFireButton.layer.masksToBounds = true | |
myNotificationFireButton.setTitle("Notification(Fire)", forState: .Normal) | |
myNotificationFireButton.layer.cornerRadius = 20.0 | |
myNotificationFireButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: 400) | |
myNotificationFireButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) | |
myNotificationFireButton.tag = 2 | |
// ボタンの追加 | |
view.addSubview(myNotificationButton) | |
view.addSubview(myNotificationFireButton) | |
} | |
// ボタンイベント | |
internal func onClickMyButton(sender: UIButton) { | |
print("onClickMyButton") | |
if sender.tag == 1 { | |
showNotification() | |
} else if sender.tag == 2 { | |
showNotificationFire() | |
} | |
} | |
private func showNotification() { | |
print("showNotification") | |
// Notification生成 | |
let myNotification: UILocalNotification = UILocalNotification() | |
// メッセージ代入 | |
myNotification.alertBody = "Test" | |
// Timezone設定 | |
myNotification.timeZone = NSTimeZone.defaultTimeZone() | |
// Notificationを表示 | |
UIApplication.sharedApplication().scheduleLocalNotification(myNotification) | |
} | |
private func showNotificationFire() { | |
let myNotification: UILocalNotification = UILocalNotification() | |
myNotification.alertBody = "Test(Fire)" | |
// 再生サウンドを設定 | |
myNotification.soundName = UILocalNotificationDefaultSoundName | |
myNotification.timeZone = NSTimeZone.defaultTimeZone() | |
// 10秒後に表示する | |
myNotification.fireDate = NSDate(timeIntervalSinceNow: 10) | |
UIApplication.sharedApplication().scheduleLocalNotification(myNotification) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
// 参考:https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/uikit/008-uilocalnotificationno-biao-shi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment