Android 8 (Oreo) 以降の通知生成
久々に Google Play へ登録したアプリを更新しようとしたら、targetSdkVersion は 26 以上、2019/11/1以降は 28 以上じゃないとアップできねーとか表示されて、ターゲットをあげた途端に色々動作しなくなったり強制終了するようになったのメモ
Oreo以降の通知登録、Notification.Builder へ setChannelId を追加と NotificationChannel の利用が必須へ
Android 8 (Oreo) 以降の通知生成
(わかりにくいので括弧で包んでます)
Oreo以降の通知登録、Notification.Builder へ setChannelId を追加と NotificationChannel の利用が必須へ
Android 8 (Oreo) 以降の通知生成
(わかりにくいので括弧で包んでます)
// 通知をタップで起動するActivity
Intent intent = new Intent(this, HogeActivity.class); // タップで呼ばれるActivity
intent.putExtra("sendData", true); // Activityへ渡すデータ等(複数追加可)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// 通知生成
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
{
Notification.Builder nbuilder = new Notification.Builder(this);
{
nbuilder.setPriority(Notification.PRIORITY_DEFAULT);
nbuilder.setContentTitle("タイトル");
nbuilder.setContentText("メッセージ");
// nbuilder.setAutoCancel(true); // タップしたら解除
nbuilder.setSmallIcon(R.drawable.ic_hogehoge); // アイコン
nbuilder.setOngoing(true); // 削除不可
nbuilder.setContentIntent(contentIntent);
// 8(Oreo) API26 以降
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
nbuilder.setChannelId("hogehoge_channgel_01");
}
}
// 8(Oreo) API26 以降
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
NotificationChannel channel = new NotificationChannel("hogehoge_channgel_01", "hogehoge channel", NotificationManager.IMPORTANCE_HIGH); //Create Notification Channel
channel.setDescription("Channel Description.");
nManager.createNotificationChannel(channel);
}
nManager.notify(0/* 削除等に利用する番号 */, nbuilder.build()); // NotificationManagerに登録
}
コメント