これまでのNotificationは、Notificationクラスをインスタンス化して表示していました。しかし、現在ではこの方法は非推奨となっています。
そのため、Notification.Builderを使用する方法が現在は推奨されています。
ただしNotification.BuilderはAPI11からのAPIのため、それ以前のバージョンに対応させるにはNotificationCompat.Builderを使用します。
基本的な使い方は同じですが、今回はNotificationCompat.Builderのコードで解説します。
NotificationCompatを使ってYoutubeを起動するコードは以下の通りです。
private void startYoutubeNotification(){ final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://hq0GjaBMhhM")); final PendingIntent contentIntent = PendingIntent.getActivity(this, PENDING_INTENT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Notificationの設定 final Notification notification = new NotificationCompat.Builder(this) // 通知タイミング .setWhen(System.currentTimeMillis()) // YoutubeIntent .setContentIntent(contentIntent) // アイコン .setSmallIcon(android.R.drawable.stat_notify_chat) // Tickerテキスト(ステータスバーに表示される文字列) .setTicker(getString(R.string.notification_ticker)) // タイトル .setContentTitle(getString(R.string.notification_title)) // 音とバイブとライト .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS) // タップすれば消える設定 .setAutoCancel(true) // Notification生成 .build(); // NotificationManagerの生成 final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID_YOUTUBE, notification); }3行目ではYoutubeを起動するIntentはvnd.youtube://[動画ID]として記述し、IntentのURIに含めています。
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://hq0GjaBMhhM"));
このIntentをNotificationで利用する場合はPendingIntentに設定します。
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://hq0GjaBMhhM")); final PendingIntent contentIntent = PendingIntent.getActivity(this, PENDING_INTENT_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);PendingIntentの引数に関してはこちらを参考にしてください。
Notification.Builderのインスタンスは次のように行います。
new NotificationCompat.Builder(this)ここで取得したNotification.BuiderはBuilderパターンで作られているので、次のように設定を記述していきます。
// Notificationの設定 final Notification notification = new NotificationCompat.Builder(this) // 通知タイミング .setWhen(System.currentTimeMillis()) // YoutubeIntent .setContentIntent(contentIntent) // アイコン .setSmallIcon(android.R.drawable.stat_notify_chat) // Tickerテキスト(ステータスバーに表示される文字列) .setTicker(getString(R.string.notification_ticker)) // タイトル .setContentTitle(getString(R.string.notification_title)) // 音とバイブ .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS) // タップすれば消える設定 .setAutoCancel(true) // Notification生成 .getNotification();最後にNotificationBuilderをNotificationManagerに設定し、Notificationを表示します。
// NotificationManagerの生成 final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID_YOUTUBE, notification);前回書きましたが、、Notification.DEFAULT_VIBRATEを指定する場合は、VIBRATEのパーミッションを指定してください。
※追記:NotificationCompat$BuilderのgetNotification()が非推奨となったので、build()に修正しました。
0 件のコメント:
コメントを投稿