Question about app bouncing when sending firebase cloud messaging (fcm) to the Android app!

Asked 2 years ago, Updated 2 years ago, 126 views

2019-11-07 16:01:18.615 22887-22887/com.example.api21test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.api21test, PID: 22887
    java.lang.RuntimeException: Unable to instantiate service com.example.api21test.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.example.api21test.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.example.api21test-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.api21test-1/lib/arm64, /system/lib64, /vendor/lib64]]
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3248)
        at android.app.ActivityThread.-wrap5(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.api21test.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.example.api21test-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.api21test-1/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:74)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3245)
        at android.app.ActivityThread.-wrap5(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:165) 
        at android.app.ActivityThread.main(ActivityThread.java:6375) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802) 
package com.example.api21test;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;


public class MyFirebaseMessagingService extends FirebaseMessagingService{
    @Override
    public void onNewToken(String token) {
        Log.d ("Refreshed token: " + token");

        // // If you want to send messages to this application instance or
        // // manage this apps subscriptions on the server side, send the
        // // Instance ID token to your app server.
        sendRegistrationToServer(token);
    }
    private void sendRegistrationToServer(String token) {
        // // TODO: Implement this method to send token to your app server.
    }

}
package com.example.api21test;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (!task.isSuccessful()) {
                            Log.w("Failure:", "getInstanceId failed", task.getException()));
                            return;
                        }

                        // // Get new Instance ID token
                        String token = task.getResult().getToken();

                        // // Log and toast
                        String msg = getString(R.string.msg_token_fmt, token);
                        Log.d ("Successful: ", msg);
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
        FirebaseMessaging.getInstance().setAutoInitEnabled(true);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.api21test">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="false" />
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="false" />
        <activity android:name="com.example.api21test.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.example.api21test.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'
        classpath 'com.google.gms:google-services:4.3.2'
        // // NOTE: Do not place your application dependencies here; they belong
        // // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.api21test"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:support-core-utils:28.0.0'
    implementation 'com.google.firebase:firebase-messaging:20.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

apply plugin: 'com.google.gms.google-services'

Error MyFirebaseMessagingService, MainActivity, Manifest, build.gradle(project), build.gradle(module) code in order

When you send a personal message or a full message with a token from an app registered in firebase, this error pops up and bounces off Which part is wrong?

fcm android

2022-09-22 19:46

1 Answers

I think it's all done well, but I don't know which other part of the problem is.

...
        android:name="com.example.api21test.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
...

Would you like to try fixing it like this?

Also, please override the onMessageReceived().


2022-09-22 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.