Make sure you followed the installation instructions first, before proceeding here. You must have imported, and installed firebase first as well
Integration
To use Laudspeaker you need to import and install Laudspeaker, and then initialize with the right endpoint, and api key. You can find your api key in Laudspeakers settings page.
Initialize Laudspeaker
You can initialize laudspeaker in a few ways.
Direct Initialization
You can import the following in your application class:
import com.laudspeaker.android.Laudspeaker
import com.laudspeaker.android.LaudspeakerAndroid
import com.laudspeaker.android.LaudspeakerAndroidConfig
and add the following before your onCreate method in your Application class.
private lateinit var laudspeaker: Laudspeaker
Then in the onCreate class:
FirebaseApp.initializeApp(this);
// Initialize LaudspeakerAndroidConfig without dependency injection
val config = LaudspeakerAndroidConfig(
this,
"WjzfCfRwSXDtG3H6u72b4PGate0B1a4MDhu51j0x",
"https://app.laudspeaker.com/api",
MainActivity::class.java // Adjust if needed
)
// Initialize Laudspeaker and get the instance
laudspeaker = LaudspeakerAndroid.with(this, config)
// Now you can use 'laudspeaker' as needed
laudspeaker.setNotificationIcon(R.drawable.ic_launcher_background)
laudspeaker.handlePushOpened(intent)
A full example is as follows:
package com.example.examplewithlaudspeaer
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.examplewithlaudspeaer.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.FirebaseApp
import com.laudspeaker.android.Laudspeaker
import com.laudspeaker.android.LaudspeakerAndroid
import com.laudspeaker.android.LaudspeakerAndroidConfig
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private lateinit var laudspeaker: Laudspeaker
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
FirebaseApp.initializeApp(this);
// Initialize LaudspeakerAndroidConfig without dependency injection
val config = LaudspeakerAndroidConfig(
this,
"WjzfCfRwSXDtG3H6u72b4PGate0B1a4MDhu51j0x",
"https://app.laudspeaker.com/api",
MainActivity::class.java // Adjust if needed
)
// Initialize Laudspeaker and get the instance
laudspeaker = LaudspeakerAndroid.with(this, config)
// Now you can use 'laudspeaker' as needed
laudspeaker.setNotificationIcon(R.drawable.ic_launcher_background)
laudspeaker.handlePushOpened(intent)
val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.setAnchorView(R.id.fab).show()
}
val fireButton = findViewById<Button>(R.id.fire_button)
fireButton.setOnClickListener {
val myMap: MutableMap<String, Any> = HashMap()
myMap["time"] = System.currentTimeMillis()
laudspeaker.fire("hello", myMap)
}
}
Dependency injection approach:
In your AppModule.kt file, you provide the information to set up Laudspeaker correctly. Specifically you need to grab your api key from the settings page:
package com.laudspeaker.myapplication
import com.laudspeaker.android.LaudspeakerAndroid
import com.laudspeaker.android.LaudspeakerAndroidConfig
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
val appModule = module {
single {
val config = LaudspeakerAndroidConfig(
this.androidContext(),
"WjzfCfRwSXDtG3H6u72b4PGate0B1a4MDhu51j0x", //your api key found in laudspeaker settings
"https://app.laudspeaker.com/api",
MainActivity::class.java // Might need to adjust based on actual usage
)
LaudspeakerAndroid.with(get(), config)
}
}
After that in your main activity you can use Laudspeaker like this:
package com.laudspeaker.myapplication
import ...
class MainActivity : AppCompatActivity() {
private var appBarConfiguration: AppBarConfiguration? = null
private var binding: ActivityMainBinding? = null
private val laudspeaker: Laudspeaker by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FirebaseApp.initializeApp(this)
binding = ActivityMainBinding.inflate(
layoutInflater
)
....
laudspeaker.setNotificationIcon(R.drawable.ic_launcher_background)
laudspeaker.handlePushOpened(intent)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId
return if (id == R.id.action_settings) {
true
} else super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
...
}
}
At this point your app should be able to receive push messages!
If you want to see a sample application implementation go here ()
Testing
To test, you will need a real device, and access to its firebase device token
Then go to the template page and create a push notification, go to the test page, and select the device firebase token and send a push!