Firebase Cloud Messaging FCM错误,无法发送消息

时间:2019-03-29 16:54:23

标签: java android firebase-cloud-messaging

我正在使用Firebase云消息尝试将消息从我的应用程序内部发送到特定主题。但是我的程序遇到“无法将消息转换为RemoteMessage”错误,如下面的代码所示。 我从herehere那里获取了代码,他们两个都在使用此代码,但是似乎可以正常运行。

我的build.gradle:

apply plugin: 'com.android.application'

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    annotationProcessor 'com.google.firebase:firebase-core:16.0.8'
    implementation ('com.google.firebase:firebase-core:16.0.8'){
        exclude module: 'firebase-common'
    }
    annotationProcessor 'com.google.firebase:firebase-messaging:17.5.0'
    implementation ('com.google.firebase:firebase-messaging:17.5.0'){
        exclude module: 'firebase-common'
    }
    annotationProcessor 'com.google.firebase:firebase-admin:6.3.0'
    implementation 'com.google.firebase:firebase-admin:6.3.0'
    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'
}

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

package com.example.firebaseteste;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new FCMService().send();

    }



    public class FCMService  {
        public void send()  {
            try {

                GoogleCredentials fromStream = GoogleCredentials.fromStream(new FileInputStream(new File("pathToFile")));

                FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                        .setConnectTimeout(10000)
                        .setCredentials(fromStream)
                        .setDatabaseUrl("database url") // you can get this from Firebase -> Project settings -> Service Accounts -> Java
                        .build();

                FirebaseApp firebaseApp = FirebaseApp.initializeApp(firebaseOptions);

                FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance();

                Message message = Message.builder()
                        .putData("score", "850")
                        .putData("time", "NOW")
                        .setTopic("topic")
                        .build();

            //LINE WITH ERROR
            String response = firebaseMessaging.send(message);


                System.out.println(response);

                Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG);


            } catch (FirebaseMessagingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

0 个答案:

没有答案