Android
First to start Android integration, be sure that Setup – Firebase setup was fully completed.
Note
Push Notifications are available from Targets API level 19 (KitKat) and Android 4.4, and later.
- Put a
google-services.json
into the app-level folder of the app. - Add the Google Services package into the app:
- Into the root-level Gradle file
build.gradle
add the Google Service plugin:
buildscript { repositories { google() // Google's Maven repository } dependencies { // ... classpath 'com.google.gms:google-services:4.3.10' // Google Services plugin } } allprojects { // ... repositories { google() // Google's Maven repository // ... } }
- In the app-level Gradle file
build.gradle
, apply the plugin:app/build.gradleapply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' // Google Services plugin android { // ... }
- Into the root-level Gradle file
-
Integrate the Firebase SDK.
-
Into the app-level gradle file
build.gradle
declare dependencies with/without Firebase Android BOM:app/build.gradledependencies { implementation platform('com.google.firebase:firebase-bom:29.0.0') implementation 'com.google.firebase:firebase-messaging' implementation 'com.google.firebase:firebase-analytics' // if Firebase Analytics required }
app/build.gradledependencies { implementation 'com.google.firebase:firebase-messaging:23.0.0' implementation 'com.google.firebase:firebase-analytics:20.0.0' // if Firebase Analytics required }
app/build.gradledependencies { implementation platform('com.google.firebase:firebase-bom:29.0.0') implementation 'com.google.firebase:firebase-messaging-ktx' implementation 'com.google.firebase:firebase-analytics-ktx' // if Firebase Analytics required }
app/build.gradledependencies { implementation 'com.google.firebase:firebase-messaging-ktx:23.0.0' implementation 'com.google.firebase:firebase-analytics-ktx:20.0.0' // if Firebase Analytics required }
-
Add the service in the manifest:
app/src/main/AndroidManifest.xml<service android:name=".java.MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
app/src/main/AndroidManifest.xml<service android:name=".kotlin.MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
-
Create the
FirebaseMessaging.getInstance().getToken()
method to receive the current token.MainActivity
class example:MainActivity.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); binding.logTokenButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FirebaseMessaging.getInstance().getToken() .addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull Task<String> task) { if (!task.isSuccessful()) { return; } String token = task.getResult(); String msg = getString(R.string.msg_token_fmt, token); // send request to the NeuCurrent method for token registartion according to Swagger UI } }); } } } }
MainActivity.kt1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) binding.logTokenButton.setOnClickListener { FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> if (!task.isSuccessful) { return@OnCompleteListener } val token = task.result val msg = getString(R.string.msg_token_fmt, token) // send request to the NeuCurrent method for token registartion according to Swagger UI }) } } }
Request sending with NeuCurrent API
Please, notice that in the code you should send a HTTP request to NeuCurrent API with a received device token on https://app.neucurrent.com/push/api/v1/device-token/add. Docs: https://app.neucurrent.com/push/api/v1/docs
-
Create
MyFirebaseMessagingService
file and add the regenerated token receiving method:public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String token) { // send request to the NeuCurrent method for token registartion according to Swagger UI } }
class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { // send request to the NeuCurrent method for token registartion according to Swagger UI } }
-
-
Congratulations! You've finished the integration process.
Additional sources: