Today, we’re blasting off into uncharted territory: getting your push notifications to roar with a custom sound on your Android app. Strap in, because the custom sound plane is about to break the sound barrier! 🛩️💥
Your secret weapon? A .ogg or .mp3 sound file. Place it in the heart of your Android app:
nativephp/android/app/src/main/res/raw/notification.ogg
⚡ Pro tip: keep your filename simple—lowercase letters and numbers only. In our mission, we’ll call it notification.ogg.
When launching your push notification, here’s how you arm your payload:
'androidConfig' => [
'priority' => 'high',
'notification' => [
'channel_id' => 'fcm_default_channel',
'sound' => 'notification'
]
],
NativePHP’s default channel_id is fcm_default_channel. And remember—the sound key is just your filename without the extension. Simple, elegant, powerful.
But the adventure doesn’t stop there. To truly make your notifications sing, we need to tweak the PushNotificationService.kt:
/nativephp/android/app/src/main/java/com/youraweseomeappname/app/v1/services/PushNotificationService.kt
Inside, you’ll inject some extra syntax to unleash your custom sound. Think of it as strapping a jet engine onto your notification system. 🚀
Once you’ve done this, every push notification becomes a sonic boom of attention—a custom sound that’s unmistakably yours.
Always make sure you remove the app completely from your device after making these tweaks en rerun this with php artisan native:run
package com.yourawesomeappname.app.v1.services
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import com.yourawesomeappname.app.v1.R
// Added by 599 Tech
import android.media.AudioAttributes
import android.net.Uri
// Added by 599 Tech
class PushNotificationService: FirebaseMessagingService() {
companion object {
private const val TAG = "PushNotificationService"
private const val CHANNEL_ID = "fcm_default_channel"
private const val NOTIFICATION_ID = 1001
}
// Added by 599 Tech
private val soundUri: Uri? by lazy {
try {
Uri.parse("android.resource://${packageName}/${R.raw.notification}")
} catch (e: Exception) {
Log.e(TAG, "Sound file missing, will use default sound")
null
}
}
// Added by 599 Tech
override fun onCreate() {
super.onCreate()
createNotificationChannel()
Log.d(TAG, "📱 PushNotificationService created and notification channel set up")
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "FCM Notifications"
val descriptionText = "Notifications from Firebase Cloud Messaging"
val importance = NotificationManager.IMPORTANCE_HIGH
// Added by 599 Tech
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
// Added by 599 Tech
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
enableVibration(true)
enableLights(true)
// Added by 599 Tech
setSound(soundUri, audioAttributes)
// Added by 599 Tech
}
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
Log.d(TAG, "🔔 Notification channel created for Android O+")
}
}
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d(TAG, "🔄 Token refreshed: $token")
//update remote server with new token
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Log.d(TAG, "📨 FCM message received")
Log.d(TAG, "📊 Message data: ${message.data}")
Log.d(TAG, "🔔 Message notification title: ${message.notification?.title}")
Log.d(TAG, "📝 Message notification body: ${message.notification?.body}")
// Extract title and body from message
val title = message.notification?.title ?: message.data["title"] ?: "Notification"
val body = message.notification?.body ?: message.data["body"] ?: "You have a new message"
Log.d(TAG, "🎯 Using title: '$title', body: '$body'")
// Show notification for foreground messages
showNotification(title, body)
}
private fun showNotification(title: String, body: String) {
try {
Log.d(TAG, "🔨 Building notification with title='$title', body='$body'")
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher) // Using launcher icon as small icon
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.setStyle(NotificationCompat.BigTextStyle().bigText(body)) // Allow multiline body text
// Added by 599 Tech
.setSound(soundUri)
// Added by 599 Tech
.build()
notificationManager.notify(NOTIFICATION_ID, notification)
Log.d(TAG, "✅ Notification displayed successfully")
} catch (e: Exception) {
Log.e(TAG, "❌ Error showing notification: ${e.message}", e)
}
}
}