Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Demo always sending token to server #1353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions messaging/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -97,26 +102,46 @@ public void onClick(View v) {
// Get token
// [START log_reg_token]
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> 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<String, String> 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]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

@thatfiredev thatfiredev Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comment above gets applied, we'll need to import functions ktx.

Suggested change
import com.google.firebase.functions.FirebaseFunctions
import com.google.firebase.functions.FirebaseFunctions
import com.google.firebase.functions.ktx.functions

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() {

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a Kotlin snippet, we might want to demonstrate KTX here

Suggested change
FirebaseMessaging.getInstance().token
Firebase.messaging.token

.addOnCompleteListener { task: Task<String> ->
if (!task.isSuccessful) {
Log.w(
TAG,
"Fetching FCM registration token failed",
task.exception
)
return@addOnCompleteListener
}
val token = task.result
val tokenMap: MutableMap<String, String?> =
HashMap()
tokenMap["fcm_token"] = token
Comment on lines +106 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to:

Suggested change
val tokenMap: MutableMap<String, String?> =
HashMap()
tokenMap["fcm_token"] = token
val tokenMap: MutableMap<String, String?> = hashMapOf("fcm_token" to token)


// Send token to backend server
val callable = FirebaseFunctions.getInstance().getHttpsCallable("updateToken")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can demonstrate KTX here too:

Suggested change
val callable = FirebaseFunctions.getInstance().getHttpsCallable("updateToken")
val callable = Firebase.functions.getHttpsCallable("updateToken")

callable.call(tokenMap).getResult()
}
// [END send_reg_token]
}

companion object {

private const val TAG = "MainActivity"
Expand Down