Upload
hiroaki-kaneda
View
609
Download
5
Embed Size (px)
Citation preview
自己紹介
• 金田浩明
• 株式会社ブリリアントサービス勤務
• 著書:基礎からのAndroidプログラミング
• www.facebook.com/hiroaki.kaneda
• emboss369
Notificationには2種類ある
• Local Notification(ローカル通知)
–アプリがバックグラウンド時にユーザーに情報を伝える仕組み。
–アプリ自身が発信し、iOSが配信する
• Remote Notification(リモート通知)
–いわゆるプッシュ通知。
–リモートサーバーで発信し、iOSが配信する
• 見た目としてはどちらも一緒
最小フェッチ間隔設定
// AppDelegate.swift
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
〜
let interval:NSTimeInterval = 60 * 60 // 1時間UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(interval)
〜
return true}
バックグラウンド処理を書くfunc application(application: UIApplication,
performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {〜なんらかの処理〜// 処理結果を通知するNotification生成let myNotification: UILocalNotification = UILocalNotification()// タイトルを設定myNotification.alertTitle = message// メッセージを設定myNotification.alertBody = detailStr// Timezoneを設定をする.myNotification.timeZone = NSTimeZone.defaultTimeZone()// Notificationを表示する.UIApplication.sharedApplication().scheduleLocalNotification(myNotification)// バックグラウンドフェッチ結果を通知completionHandler(UIBackgroundFetchResult.NewData)
}
アプリ側
// PM2.5の値をウィジェットと共有let sharedDefaults:NSUserDefaults = NSUserDefaults(suiteName: "group.PM25Extension")!sharedDefaults.setObject("【東京都】PM2.5観測情報 : "
+ self.statusLabel.text!, forKey: "pm25info")sharedDefaults.synchronize()
Extension側
class TodayViewController: UIViewController, NCWidgetProviding {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// 初期化時にNSUserDefaultsDidChangeNotificationで変更を検知して// userDefaultsDidChangeメソッドを呼び出すNSNotificationCenter.defaultCenter().addObserver(self, selector: "userDefaultsDidChange:",
name: NSUserDefaultsDidChangeNotification, object: nil)
}
/// アプリ側でテキストが変更されたらウィジェットに反映する関数func userDefaultsDidChange(notification: NSNotification) {
updateLabel()
}
/// ラベルを更新する関数func updateLabel() {
let defaults:NSUserDefaults = NSUserDefaults(suiteName: "group.PM25Extension")!
self.pm25StatusLabel.text = defaults.stringForKey("pm25info”)
}
}WatchKitもExtensionだから同じようにNSUserDefaultsでデータ共有ができます。
Remote Notificationしかテストできない?
PushNotificationPayload.apnsファイルの内容(JSONファイル)
{
"aps": {
"alert": {
"body": "【東京都】PM2.5観測情報:非常に多い",
"title": "PM2.5情報"
},
"badge" : 1,
"category": "very_high"
},
"WatchKit Simulator Actions": [
{
"title": "最初のボタン",
"identifier": "firstButtonAction"
},
{
"title": "2番目のボタン",
"identifier": "secondButtonAction"
}
],
"pm2.5status": "very high"
}
NotificationControllerclass NotificationController: WKUserNotificationInterfaceController {
override func didReceiveRemoteNotification(
remoteNotification: [NSObject : AnyObject],
withCompletion completionHandler: (WKUserNotificationInterfaceType) -> Void) {
var aps = remoteNotification["aps"] as! [NSObject : AnyObject]
var alert = aps["alert"] as! [NSObject : AnyObject]
var body = alert["body"] as! String
self.label.setText(body)
completionHandler(.Custom)
}
}
.Customで Dynamic Interfaceが呼ばれる。
.Defaultとすると、Static Interfaceが呼ばれる。
PushNotificationPayload.apnsファイルの内容(JSONファイル)
{
"aps": {
"alert": {
"body": "【東京都】PM2.5観測情報:非常に多い",
"title": "PM2.5情報"
},
使用するNotificationはCategoryで指定
// 通知を表示する// Notification生成let myNotification: UILocalNotification = UILocalNotification()
// タイトルを設定myNotification.alertTitle = "PM2.5情報"
// メッセージを設定myNotification.alertBody = "【東京都】PM2.5観測情報:多い"
// Timezoneを設定をする.
myNotification.timeZone = NSTimeZone.defaultTimeZone()
myNotification.soundName = UILocalNotificationDefaultSoundName;
// カテゴリー名を指定するmyNotification.category = "high"
// Notificationを表示する.
UIApplication.sharedApplication().scheduleLocalNotification(myNotification)
アクション可能Notification
• 通知を見て、1タップでアクションできる便利機能
• メールソフトなら「既読」「削除」「アーカイブ」を1タップで
• TODO管理ソフトなら「完了」「1時間後に再通知」を1タップで
• 通知を見てその場でアクションを選択できる
アクション可能Notification
InterfaceController.swift
class InterfaceController: WKInterfaceController {〜override func handleActionWithIdentifier(identifier: String?,
forRemoteNotification remoteNotification: [NSObject : AnyObject]) {
if identifier == "firstButtonAction" {self.label.setText("1番目のボタンが選択された")
}else if identifier == "secondButtonAction" {
self.label.setText("2番目のボタンが選択された")}
}}
実機で試した感想
• JSONを用いたNotificationの実機デバッグ実行はうまくいかなかった
– 実際のPush通知を試す必要あり?
– サーバ用意とかPHPでコーディんとか色々面倒そう)
• Local NotificationでDynamicインタフェースにならない。
– Staticインタフェースになってしまう
• iPhoneがロックされていないとWatchにNotificatonは表示されない