Firebase push notification with custom sound (Flutter)

后端 未结 3 1642
失恋的感觉
失恋的感觉 2021-01-12 05:18

I\'m currently using firebase messaging cloud to push notification for my app. I\'m trying to make a custom notification sound for the push notification. I believe that it c

3条回答
  •  生来不讨喜
    2021-01-12 05:49

    You must create notification channel first with following code
    
    Application.kt
    
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.graphics.Color;
    import android.os.Build;
    
    import io.flutter.app.FlutterApplication
    import io.flutter.plugin.common.PluginRegistry
    import io.flutter.plugins.GeneratedPluginRegistrant
    import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
    import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
    import io.flutter.plugins.pathprovider.PathProviderPlugin;
    
    import io.flutter.plugin.common.MethodChannel
    import android.content.ContextWrapper
    import android.content.Intent
    import android.content.IntentFilter
    import android.os.Build.VERSION
    import android.os.Build.VERSION_CODES
    import android.net.Uri;
    import android.media.AudioAttributes;
    import android.content.ContentResolver;
    
    class Application : FlutterApplication(), PluginRegistrantCallback {
        override fun onCreate() {
            super.onCreate()
            this.createChannel()
            FlutterFirebaseMessagingService.setPluginRegistrant(this)
        }
    
        override fun registerWith(registry: PluginRegistry?) {
    //        createChannel();
            FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
        }
    
        private fun createChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // Create the NotificationChannel
                val name: String = getString(R.string.default_notification_channel_id)
                val channel = NotificationChannel(name, "default", NotificationManager.IMPORTANCE_HIGH)
                val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/raw/sample");
                val att = AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                        .build();
                channel.setSound(soundUri, att)
    
                val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
    
                /*
                val id = mapData["id"]
                val name = mapData["name"]
                val descriptionText = mapData["description"]
                val sound = "sample"
                val importance = NotificationManager.IMPORTANCE_HIGH
                val mChannel = NotificationChannel(id, name, importance)
                mChannel.description = descriptionText
    
    
    
    
    
                // Register the channel with the system; you can't change the importance
                // or other notification behaviors after this
                val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(mChannel)
                completed = true
                 */
            }
        }
    }
    
    And android manifest.xml
    
        
    
        
        
        
        
        
    
        
        
    
    
    
    
            
                
                
                
                    
                    
                
                
                    
                    
                
            
            
            
    
            
    
    
            
    
            
        
    
    
    
    
    
    finally you must put your notification sound.wav file in res/raw
    
    that's it
    

提交回复
热议问题