“如何解决问题”;默认FirebaseApp未能初始化,因为未找到默认选项";
我尝试在我的应用程序中使用Firebase云消息传递,并将“遵循指南”编码为“”以使设备启动 主要活动:“如何解决问题”;默认FirebaseApp未能初始化,因为未找到默认选项";,firebase,token,Firebase,Token,我尝试在我的应用程序中使用Firebase云消息传递,并将“遵循指南”编码为“”以使设备启动 主要活动: FirebaseApp.initializeApp(this); FirebaseInstanceId.getInstance().getInstanceId() .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
FirebaseApp.initializeApp(this);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w("FCM", "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("FCM", msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
雄激素单
<service android:name=".utilities.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
但当我在我的移动设备上运行测试时,在日志中获取问题:”
D/FirebaseApp:Default FirebaseApp未能初始化,因为未找到默认选项。这通常意味着com.google.gms:google服务未应用于您的gradle项目。
E/AndroidRuntime:致命异常:主
进程:com.logistic.myapplication,PID:32022
java.lang.RuntimeException:无法启动活动组件信息{com.trixLogistic.myapplication/com.trixLogistic.myapplication.MainActivity}:java.lang.IllegalStateException:默认FirebaseApp未在此过程中初始化。请确保先调用FirebaseApp.initializeApp(上下文)。”
我不知道我做错了什么,即使我是
将Firebase添加到Firebase控制台上我的项目中的我的Android应用程序中,并填充值包名,值调试证书SHA1;下载最新的json配置文件并复制到Android studio中的我的项目中。
如何修复它
<service android:name=".utilities.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
//TODO: xu ly thong bao co du lieu
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage.getData());
}
//TODO: xu ly thong bao tin thong thuong
if (remoteMessage.getNotification() != null) {
sendNotificationBody(remoteMessage.getNotification().getBody());
}
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
sendRegistrationToServer(token);
}
....