Gabriel
Gabriel

Reputation: 229

How to setup Android application to use FCM?

I've done the step by step in the tutorial, get json, adding dependecies (classpath 'com.google.gms:google-services:3.0.0'),
apply plugin (apply plugin: 'com.google.gms.google-services'), modify manifest file. (Don't forget to put these into application, not outside it!)

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

create firebaseMessagingService Class (MyFirebaseMessagingService.java)

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}

creating FirebaseIDService.java that extends FirebaseInstanceIdService

public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer() {
    // Add custom implementation, as needed.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}

Here my main activity class snippet:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //FCM
    FirebaseIDService firebaseIDService = new FirebaseIDService();
    MyFirebaseMessagingService myFirebaseMessagingService = new MyFirebaseMessagingService();

Then I send the push notification from console, but nothing happens. What part is missing? or something is not working? I dunt get any logcat info from both class i create

UPDATE: this what i did to fix the Firebase not linked issue: I need to call the class manually and get token, and its working

FirebaseIDService firebaseIDService = new FirebaseIDService();
    firebaseIDService.getId();

But somehow, the push notification still not coming, the FCM class i used not giving any logcat, any idea?

UPDATE: so far i dunt find solution yet on receiving the message. the class that extends FirebaseMessagingService still not working, not showing anything in logcat, but it does showing something like this: D/FA: Logging event (FE): _nf, Bundle[{_o=fcm, _ndt=0, _nmn=Bayu testing PN, _nmt=1492745322, _nmid=2820998932030178856}]

Upvotes: 0

Views: 1449

Answers (2)

Gabriel
Gabriel

Reputation: 229

I have it working now, the issue is because i put service outside of the application, so the services must inside the application, so the onReceivedMessage will work

Upvotes: 1

Mercury
Mercury

Reputation: 7988

You probably skipped this step, since i don't see you mentioned it:
In your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:10.2.1'

  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

Please note the: compile 'com.google.firebase:firebase-core:10.2.1'

Upvotes: 0

Related Questions