Quantcast
Channel: kenji matsuokaのニッキ
Viewing all articles
Browse latest Browse all 342

Notificationを作る

$
0
0
ウェアラブル向けアプリを作る 目次
原文
ウェアラブルにも送られる携帯端末Notificationを作るためにNotificationCompat.Builderを使用します。このクラスを使ってNotificationを作ると、システムはNotificationが携帯端末もウェアラブルのどちらにも正しく表示されるように配慮します。

メモ:Notificationはカスタムレイアウトが使用できないRemoteViewsを使用し、ウェアラブル上にはテキストとアイコンのみが表示されます。しかしながら、ウェアラブルデバイス上で実行されるウェアラブルアプリを作ることでカスタムカードレイアウトを使った カスタムレイアウトの作成(翻訳中) が可能です。

必要なクラスのインポート

必要なパッケージをインポートするためにbuild.gradefileにこの行を追加します。

compile "com.android.support:support-v4:20.0.+"

これにより、プロジェクトは必要なパッケージにアクセスできるようになります。サポートライブラリーから必要なクラスをインポートします。

import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;

Notification Builderを使ってNotificationを作る

v4 support libraryを使うことでAndroid1.6(API livel4)以上と互換性を保ったまま、アクションボタンやラージアイコンなどの最新機能を使ったNotificationを作ることが出来ます。

サポートライブラリを使用してNotificationを作るには、NotificationCompat.Builderのインスタンスを生成し、notify()に渡すことでNotificationを発行します。
例:

int notificationId = 001;
// notificationの内容のためのIntentを作る
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(this, 0, viewIntent, 0);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent);

// NotificationManager serviceのインスタンスを取得する
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);

// notification managerでNotificationを作って発行する。
notificationManager.notify(notificationId, notificationBuilder.build());

このNotificationが携帯デバイスで表示されると、ユーザーはNotificationをタッチすることでsetContentIntent()メソッドにて指定されたPendingIntentを発行できます。
Androidウェアラブル上で表示されているときはNotificationを左にスワイプして「端末で開く」アクションを表示し、携帯端末のintentを発行できます。

アクションボタンを追加する

setContentIntent()で宣言したメインのコンテントアクションに加えて、addAction()メソッドにPendingIntentを渡すことで他のアクションを加える事が出来ます。

例えば以下のコードは上記コードのNotificationタイプと同じですが、マップ上でイベントの場所を見るためのアクションが追加されています。

// マップビューアクションのためのIntentを作る
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode(location));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent =
PendingIntent.getActivity(this, 0, mapIntent, 0);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent);

携帯端末ではNotificationに追加のボタンが付いた状態でアクションが表示されます。ウェアラブル上ではユーザーが左方向にスワイプした時に大きなボタンとして表示されます。ユーザーがアクションをタップすると関連したIntentが携帯端末上で発行されます。
チップス:もしNotificationに「返信」アクションが(メッセージアプリなど)含まれているならAndroidウェアラブルから直接音声入力で返信できるようにすることで振る舞いを改善できます。より詳細はNotificationで音声入力を受け取るを読んでください。

ウェアラブルのみに特別なアクションを追加する

もし、ウェアラブル上のみで利用可能で、携帯端末とは違うアクションを追加したいのであれば、WearableExtender.addAction()を使用します。このメソッドにより一度アクションが追加されると、NotificationCompat.Builder.addAction()で追加された他のアクションはウェアラブル上に表示されなくなります。WearableExtender.addAction()で指定されたアクションはウェアラブル上のみに追加され携帯端末上には表示されません。

// 返信アクションのためのIntentを作る
Intent actionIntent = new Intent(this, ActionActivity.class);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

// アクションを作る
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_action,
getString(R.string.label, actionPendingIntent))
.build();

// notificationを作り、WearableExtenderを通じてアクションを追加する。
Notification notification =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_message)
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.content))
.extend(new WearableExtender().addAction(action))
.build();

Big Viewを追加する

Notificationに"big view"スタイルの1つを加えることで拡張テキストコンテンツを挿入できます。ユーザーは携帯デバイス上においてNotificationを拡張することでbig viewコンテンツを見ることが出来ます。ウェアラブルデバイス上では標準で表示されます。

Notificationに拡張コンテンツを追加するにはNotificationCompat.Builder setStyle()を呼んで、BigTextStyleInboxStyleを渡します。

例えば、以下のコードはNotificationCompat.BigTextStyleインスタンスをイベントNotificationに追加しイベントの詳細全てを含めています。(setContentText()よりも多くのスペースを与えられるため、より多くのテキストを含めることが出来ます。)



// "big_view" コンテンツを指定して通常のコンテンツテキストには入らない長いイベント詳細を表示する。
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);

Notificationには他のNotificationでも使用されているsetLargeIcon()メソッドを使って大きな背景画像を追加できます。大きな画像を使ったNotificationのデザインについての詳しい情報はAndroid Wearのデザイン原則(翻訳中)を御覧ください。

Notificationにウェアラブルの機能を追加する

コンテンツの追加ページや音声入力でテキストの返信を喋らせるなど、ウェアラブル特有のオプション機能を追加する時はNotificationCompat.WearableExtenderクラスを使用してオプションを指定します。
このAPIを使用するには:
1.WearableExtenderのインスタンスを生成し、Notification向けのウェアラブル特有のオプションを指定する。
2.NotificationCompat.Builderのインスタンスを生成し、このレッスンの最初を参照して、希望するプロパティを設定する。
3.Notificationでextend()メソッドを呼びWearableExtenderを渡す。これはウェアラブルオプションをNotificationに適用します。
4.Notificationを作るためにbuild()を呼ぶ。

例えば、以下のコードはNotirificationカードからアプリアイコンを削除するためにsetHintHideIcon()メソッドを呼んでいます。

// ウェアラブル向けの機能を追加するためにWearableExtenderを作る
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(true);

// 標準的なNotificationを作るためにNotificationCompat.Builderを作る
// その後WearableExtenderで拡張する
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender)
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail);
.extend(wearableExtender)
.build();


setHintHideIcon()メソッドはNotificationCompat.WearableExtenderで使用できるようになったNotification新機能の一例です。
もし後でウェアラブル特有のオプションを読む必要があるならオプションに対応したゲットメソッドを使用します。以下の例はgetHintHideIcon()メソッドを呼んでNotificationのアイコンが隠されているかどうかを調べています。

NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();

Notificationを届ける

Notificationを届けたいと思うときは常にNotificationManagerの代わりにNotificationManagerCompatAPIを使用してください。

// NotificationManagerサービスのインスタンスを取得
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);

// notification manager.を使ってnotificationを発行する
notificationManager.notify(notificationId, notif);

もし、フレームワークのNotificationManager,を使用していると、NotificationCompat.WearableExtenderのいくつかの新機能は動作しません。そのため確実にNotificationCompat.を使用するようにしてください。

NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();

NotificationCompat.WearableExtenderAPI群はNotificationの追加ページ、スタックNotificationsなどの新機能を追加可能にします。次のレッスンではこれらの機能について学びます。


Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content License.

Viewing all articles
Browse latest Browse all 342

Trending Articles