0

I'm trying to import Firebase and FCM into my android app, but I'm getting

Unresolved class for firebase ".MyFirebaseInstanceIDService" Unresolved package for firebase ".java.MyFirebaseMessagingService"

as an "error." Of course, it isn't stopping my app from running or building, but I cannot call onTokenRefresh in any class at all. So, I believe this problem is preventing me from using Firebase.

here is my app build;

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.northlandcaps.crisis_response"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            android {
                lintOptions {
                    checkReleaseBuilds false
                    // Or, if you prefer, you can continue to check for errors in release builds,
                    // but continue the build even when errors are found:
                    abortOnError false
                }
            }
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'com.google.firebase:firebase-core:16.0.5'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:mediarouter-v7:28.0.0'
    implementation 'com.android.volley:volley:1.1.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
}
apply plugin: 'com.google.gms.google-services'

Screenshot of problem

Alec
  • 33
  • 8
  • The `MyFirebaseInstanceIDService` and `MyFirebaseMessagingService` don't automatically come from importing Firebase. They are classes that you need to create yourself. See https://firebase.google.com/docs/cloud-messaging/android/client#monitor-token-generation – Frank van Puffelen Nov 29 '18 at 14:16
  • @FrankvanPuffelen That was what i was following. I have that exact page opened right now. But when I was walking through it, I couldn't add those services in the app gradle – Alec Nov 29 '18 at 14:18
  • 1
    Please add the content of your build.gradle file and please responde with @. – Alex Mamo Nov 29 '18 at 14:19
  • @AlexMamo I did add the screenshot of my app build.gradle. You dont see it? – Alec Nov 29 '18 at 14:21
  • That's the Android Manifest file and I have asked you for the build.gradle file. – Alex Mamo Nov 29 '18 at 14:27
  • @AlexMamo my bad, i get them confused often. I added the app build in the question now – Alec Nov 29 '18 at 14:33
  • Looks fine to me. So have you created those 2 classes? – Alex Mamo Nov 29 '18 at 14:36
  • @AlexMamo Classes? No, I just added those chunks of code into my gradle and manifest like Google told me to – Alec Nov 29 '18 at 14:42
  • As Frank van Puffelen mentioned in his comment, in order to use those classes in your Manifest file you first need to create them. – Alex Mamo Nov 29 '18 at 14:46
  • The Java and Kotlin code for `MyFirebaseMessagingService` is right under where it tells you to add the dependencies to your `build.gradle` file. Implementing that class will get rid of one error. I actually didn't see the code for `MyFirebaseInstanceIDService` in there, but assume it's equally findable with a search. – Frank van Puffelen Nov 29 '18 at 14:51
  • @AlexMamo Google didn't tell me I had to make the class. I thought i was just adding firebase like a plugin/dependency. So how do i even make this class? i thought i was just adding in Firebase – Alec Nov 29 '18 at 14:58
  • @FrankvanPuffelen What? Im sorry im very new and youre confusing – Alec Nov 29 '18 at 14:58
  • Your manifest declares that your app contains `MyFirebaseInstanceIDService` and a `MyFirebaseMessagingService` classes, which it doesn't. That's what the error message means. You don't need either class to work with basic Firebase Cloud Messaging notifications (docs: "A service that extends FirebaseMessagingService. This is required **if you want to do any message handling beyond receiving notifications on apps in the background**."). But if you declare them, you must implement them. And if you don't want to use them (yet), you should not include their declaration from your app's manifest file – Frank van Puffelen Nov 29 '18 at 15:05
  • @FrankvanPuffelen oh, so im trying to reference a class that doesnt exist in my xml? ok, so I can just get rid of it? Why is it called a service then? What happens when I don't have it? Do I even need it to be able to send a notif to everyone that logs into my app? – Alec Nov 29 '18 at 15:19
  • If you don't have `MyFirebaseMessagingService`, you will just see the default notifications when your app is not active, and no notifications when your app **is** active and foregrounded. – Frank van Puffelen Nov 29 '18 at 15:58

1 Answers1

1

This how you two classes should look like:

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

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

And:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();

        sendNotification(notification, data);
    }

    private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
        //Send notification
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • As I see those classes are already added. If you'll add both classes, your errors will disappear. Can you confrim this? – Alex Mamo Nov 30 '18 at 13:22
  • Everything works, but OnTokenRefresh is deprecated along with FirebaseInstanceIDService – Alec Nov 30 '18 at 13:32
  • Good to hear that it worked! It is an example. And yes, you should use the latest version. Cheers! – Alex Mamo Nov 30 '18 at 13:33
  • I have another question if you wanna hear it. It should be fairly easy to solve. I dont want to make it another whole question on this site because itll probably be taken down for being a bad question – Alec Nov 30 '18 at 13:36
  • No, we should stick to one question at a time. If you have another problem after this one is solved, post a new question for that problem with its own MCVE. – Alex Mamo Nov 30 '18 at 13:37
  • MCVE? whats that? – Alec Nov 30 '18 at 13:38
  • Oh no, it isnt a scripting problem. Its just a question on what i should follow if i want to make an app that sends a notif to every phone logged into the app. Do i use Group Messaging? Topic messaging? Device Groups? Upstream messages? – Alec Nov 30 '18 at 13:42
  • You should use [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/). – Alex Mamo Nov 30 '18 at 13:43
  • Thats what i am using, but underneath FCM, there is multiple ways to handle messaging, and group messaging. And i dont know which one i should use – Alec Nov 30 '18 at 13:45
  • I have exaplained in one of my tutorials step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Cloud Firestore` and `Node.js`. You can also take a look at my answer from this **[post](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)**. – Alex Mamo Nov 30 '18 at 13:47
  • Thats a bit too confusing for a beginner like me. Ill just make it a group message – Alec Nov 30 '18 at 13:49