Last active
May 13, 2019 10:07
-
-
Save m-saeki0926/26d88ac08afd94a1d9588fc93da863bb to your computer and use it in GitHub Desktop.
Android push通知のサウンド切り替え
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyService : FirebaseMessagingService() { | |
// グループ名 | |
private val channelGroupId = "channel_group_id" | |
private val channelGroupName = "channel_group_name" | |
// チャンネル名 | |
private val channelId1 = "channel_id_sound_1" | |
private val channelName1 = "channel_name_sound_1" | |
private val channelSound = Uri.parse("android.resource://" + packageName + "/" + R.raw.sound1) | |
override fun onMessageReceived(remoteMessage: RemoteMessage?) { | |
super.onMessageReceived(remoteMessage) | |
remoteMessage?.data?.let { | |
sendData(it) | |
} | |
} | |
private fun sendData(data: Map<String, String>) { | |
// push通信から受信した値をセット | |
val channel = data["channel_name"] ?: "" | |
val title = data["title"] ?: "" | |
val body = data["body"] ?: "" | |
createChannel() | |
createNotification(title, body, channel) | |
} | |
/** | |
* チャンネル作成 | |
*/ | |
@SuppressLint("WrongConstant") | |
private fun createChannel() { | |
// Android O以上なら対応する | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
// 既にChannel Groupが作成されている場合は省く | |
if (NotificationManagerCompat.from(this).getNotificationChannelGroup(channelGroupId) != null) return | |
// チャンネルグループのインスタンス作成 | |
val group = NotificationChannelGroup(channelGroupId, channelGroupName) | |
NotificationManagerCompat.from(this).createNotificationChannelGroup(group) | |
// AudioAttributesの定義 | |
val audioAttributes = AudioAttributes.Builder() | |
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) | |
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION) | |
.build() | |
// チャンネルのインスタンス作成 | |
val channel = NotificationChannel(channelId1, channelName1, NotificationManagerCompat.IMPORTANCE_DEFAULT) | |
.apply { setSound(channelSound, audioAttributes) } | |
val default = NotificationChannel(channelId1, channelName1, NotificationManagerCompat.IMPORTANCE_DEFAULT) | |
.apply { setSound(Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes) } | |
NotificationManagerCompat.from(this).createNotificationChannels(listOf(channel, default)) | |
} | |
} | |
private fun createNotification(title: String, body: String, channelId: String) { | |
// Notificationのインスタンスを作成 | |
val notification = NotificationCompat.Builder(this, channelId).apply { | |
setAutoCancel(true) | |
setSmallIcon(R.drawable.ic_push) // 各自セットして下さい | |
setContentTitle(title) | |
setStyle(NotificationCompat.BigTextStyle() | |
.bigText(body)) | |
// O以下の場合のハンドリング | |
// チャンネルの概念がないのでここでサウンドをセットしている | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { | |
setSound( when (channelId) { | |
channelId1 -> { channelSound } | |
else -> { Settings.System.DEFAULT_NOTIFICATION_URI } | |
}) | |
} | |
}.build() | |
// Notificationの表示 | |
NotificationManagerCompat.from(this) | |
.notify(0 /* ID of notification */, notification) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment