Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

java - Firebase massaging

I am trying to handle the messaged from FCM(Firebase Cloud Messaging) in andorid;

This is my code

 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
                        Log.d("TAG", token);
                        Toast.makeText(StartActivity.this, token, Toast.LENGTH_SHORT).show();
                    }
                });
        FirebaseMessaging.getInstance().setAutoInitEnabled(true);

And i have send messaged from firebase console.

But i want to handle the messages. So i'have did this doc says https://firebase.google.com/docs/cloud-messaging/android/receive

This is manifest.xml

  <service
            android:name="com.google.firebase.messaging.FirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
            
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_icon"
                android:resource="@android:drawable/ic_notification_overlay" />
        
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_color"
                android:resource="@color/white" />

            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>

        </service>

This is my FirebaseMessagingService extended class

public class MessageService extends FirebaseMessagingService {

    private static final String TAG = "MessageService";


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Toast.makeText(this, "Got it", Toast.LENGTH_SHORT).show();
    }
}

But I don't know how to resgister this FirebaseMessagingService class to Firebase messaging

question from:https://stackoverflow.com/questions/65932637/firebase-massaging

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to register the class of your messaging service, you need to register the application itself. It should be google-services.json configuration file in your project with all necessary info about the project in firebase console. It will make kind of connection between your app and project created in Firebase console.

There are several ways to get this configuration file:

  1. Generate google-services.json via Firebase console https://firebase.google.com/docs/cloud-messaging/android/client#register_your_app_with_firebase
  2. Generate it via Android studio. Tools -> Firebase -> Cloud messaging -> complete wizard "Setup Firebase cloud messaging"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...