diff --git a/messaging/app/build.gradle b/messaging/app/build.gradle index 02fc3ef37..806380e48 100644 --- a/messaging/app/build.gradle +++ b/messaging/app/build.gradle @@ -58,6 +58,12 @@ dependencies { // Firebase Cloud Messaging (Kotlin) implementation 'com.google.firebase:firebase-messaging-ktx' + // Cloud Functions for Firebase (Java) + implementation 'com.google.firebase:firebase-functions' + + // Cloud Functions for Firebase (Kotlin) + implementation 'com.google.firebase:firebase-functions-ktx' + // For an optimal experience using FCM, add the Firebase SDK // for Google Analytics. This is recommended, but not required. implementation 'com.google.firebase:firebase-analytics' diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/java/MainActivity.java b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/java/MainActivity.java index 3194e8f36..5750b59cd 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/java/MainActivity.java +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/java/MainActivity.java @@ -29,10 +29,15 @@ import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; +import com.google.firebase.functions.FirebaseFunctions; +import com.google.firebase.functions.HttpsCallableReference; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.quickstart.fcm.R; import com.google.firebase.quickstart.fcm.databinding.ActivityMainBinding; +import java.util.HashMap; +import java.util.Map; + public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @@ -97,26 +102,46 @@ public void onClick(View v) { // Get token // [START log_reg_token] FirebaseMessaging.getInstance().getToken() - .addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (!task.isSuccessful()) { - Log.w(TAG, "Fetching FCM registration token failed", task.getException()); - return; - } - - // Get new FCM registration token - String token = task.getResult(); - - // Log and toast - String msg = getString(R.string.msg_token_fmt, token); - Log.d(TAG, msg); - Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); - } + .addOnCompleteListener(task -> { + if (!task.isSuccessful()) { + Log.w(TAG, "Fetching FCM registration token failed", task.getException()); + return; + } + + // Get new FCM registration token + String token = task.getResult(); + + // Log and toast + String msg = getString(R.string.msg_token_fmt, token); + Log.d(TAG, msg); + Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); }); // [END log_reg_token] } }); } + @Override + protected void onResume() { + super.onResume(); + + // [START send_reg_token] + FirebaseMessaging.getInstance().getToken() + .addOnCompleteListener(task -> { + if (!task.isSuccessful()) { + Log.w(TAG, "Fetching FCM registration token failed", task.getException()); + return; + } + + String token = task.getResult(); + Map tokenMap = new HashMap<>(); + tokenMap.put("fcm_token", token); + + // Send token to backend server + HttpsCallableReference callable = + FirebaseFunctions.getInstance().getHttpsCallable("updateToken"); + callable.call(tokenMap).getResult(); + }); + // [END send_reg_token] + } } diff --git a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt index 7c4ef05a5..d5769bdd8 100644 --- a/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt +++ b/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MainActivity.kt @@ -8,10 +8,14 @@ import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.gms.tasks.OnCompleteListener +import com.google.android.gms.tasks.Task +import com.google.firebase.functions.FirebaseFunctions import com.google.firebase.ktx.Firebase +import com.google.firebase.messaging.FirebaseMessaging import com.google.firebase.messaging.ktx.messaging import com.google.firebase.quickstart.fcm.R import com.google.firebase.quickstart.fcm.databinding.ActivityMainBinding +import com.google.firebase.quickstart.fcm.java.MainActivity class MainActivity : AppCompatActivity() { @@ -84,6 +88,32 @@ class MainActivity : AppCompatActivity() { Toast.makeText(this, "See README for setup instructions", Toast.LENGTH_SHORT).show() } + override fun onResume() { + super.onResume() + + // [START send_reg_token] + FirebaseMessaging.getInstance().token + .addOnCompleteListener { task: Task -> + if (!task.isSuccessful) { + Log.w( + TAG, + "Fetching FCM registration token failed", + task.exception + ) + return@addOnCompleteListener + } + val token = task.result + val tokenMap: MutableMap = + HashMap() + tokenMap["fcm_token"] = token + + // Send token to backend server + val callable = FirebaseFunctions.getInstance().getHttpsCallable("updateToken") + callable.call(tokenMap).getResult() + } + // [END send_reg_token] + } + companion object { private const val TAG = "MainActivity"