49
Notification Watch WatchKit勉強会 in 東京 2015/04/25

AppleWatch発売!Apple Watch WatchKit勉強会-Notification

Embed Size (px)

Citation preview

Notification

Watch WatchKit勉強会 in 東京

2015/04/25

自己紹介

• 金田浩明

• 株式会社ブリリアントサービス勤務

• 著書:基礎からのAndroidプログラミング

• www.facebook.com/hiroaki.kaneda

• emboss369

Notificationの出し方

• アプリに通知機能がない場合

• →アプリにNotificationを実装する

• アプリに通知機能がある場合

• →何もしない

以上

ありがとうございました。

というわけにもいかないので

• Notificationの作り方からおさらい

• ついでにToday Extensionの作り方

• Apple WatchでのNotificationのカスタマイズ

サンプルアプリ

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)

}

Notification

Today

• Notificationの作り方からおさらい

• ついでにToday Extensionの作り方

• Apple WatchでのNotificationのカスタマイズ

新規ターゲット作成

Today Extensionを追加

アプリとExtension間のデータ共有

適当なグループ名をつける。group.PM25Extensionとした。

Today Extension側も同じように

アプリ側

// 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でデータ共有ができます。

こんな感じになります

お待たせしました

• Notificationの作り方からおさらい

• ついでにToday Extensionの作り方

• Apple WatchでのNotificationのカスタマイズ

ようやくWatchKit

WatchKit Appを追加

Notification Sceneを含める

Static とDynamic

シミュレータでのNotification実行

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"

}

Dynamic Interface

軽くはまった

何故か空欄になってる。WatchKit Extensinを指定する。

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情報"

},

複数の.apnsでテスト

Notification Interfaceは複数作成可能

使用するNotificationはCategoryで指定

使用するNotificationはCategoryで指定

使用する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

識別子で区別する

アクション可能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番目のボタンが選択された")}

}}

カスタマイズ

アイコン

多すぎ

アイコン1個作って全サイズ作成

サッシの色

イメージ

テキストの折り返し

背景イメージ

今日のまとめ

• コーディングなしでもアプリが通知対応していれば通知は表示される

• 通知のデザインはカテゴリに応じて複数作成可能

• 通知のアクションはユーザーの手間を減らすために使う

実機で試した感想

• JSONを用いたNotificationの実機デバッグ実行はうまくいかなかった

– 実際のPush通知を試す必要あり?

– サーバ用意とかPHPでコーディんとか色々面倒そう)

• Local NotificationでDynamicインタフェースにならない。

– Staticインタフェースになってしまう

• iPhoneがロックされていないとWatchにNotificatonは表示されない

Watchの書籍

• www.facebook.com/hiroaki.kaneda

• emboss369

質疑応答