前提:1.需要vpn 2.带有google 服务的手机
注意!!! 这个在2023年6月30日时还是测试版,所以手机有概率接收不到消息
编写代码前需要在https://console.firebase.google.com/ 配置好参数
这里的token值需要填写代码内的initFCM()的token值测试
这里需要填入FirebaseInstallations.getInstance().getId().addOnCompleteListener()返回的id测试文章来源:https://www.toymoban.com/news/detail-512313.html
需要引入的依赖
implementation platform(‘com.google.firebase:firebase-bom:32.1.1’)
implementation ‘com.google.firebase:firebase-analytics’
implementation ‘com.google.firebase:firebase-crashlytics’
implementation ‘com.google.firebase:firebase-config’
implementation ‘com.google.firebase:firebase-perf’
implementation ‘com.google.firebase:firebase-messaging’
implementation ‘com.google.firebase:firebase-inappmessaging-display’文章来源地址https://www.toymoban.com/news/detail-512313.html
public static FirebaseRemoteConfig sRemoteConfig = null;
public static FirebaseRemoteConfig getRemoteConfig() {
if (sRemoteConfig == null) {
sRemoteConfig = FirebaseRemoteConfig.getInstance();
}
return sRemoteConfig;
}
public static FirebaseAnalytics sAnalytics = null;
public static FirebaseAnalytics getAnalytics() {
if (sAnalytics == null) {
sAnalytics = FirebaseAnalytics.getInstance(UIUtils.getContext());
}
return sAnalytics;
}
// 初始化firebase 配置
public static void acquireRemoteConfig() {
sRemoteConfig = getRemoteConfig();
String confTime = sRemoteConfig.getString("remote_config_request_time");
int cacheExpiration = Integer.parseInt(confTime.isEmpty() ? "12" : confTime) * 60 * 60;
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(cacheExpiration)
.build();
sRemoteConfig.setConfigSettingsAsync(configSettings);
sRemoteConfig.setDefaultsAsync(R.xml.remote_config_default);
sRemoteConfig.fetchAndActivate()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Slog.d("fetchAndActivate success");
}
// 是否初始化firebase的 In App Message功能
Boolean showInAppMessaging = FBRCUtils.getIsOpenInAppMessage();
FirebaseInAppMessaging.getInstance().setMessagesSuppressed(!showInAppMessaging);
Slog.d("fetchAndActivate success="+showInAppMessaging);
});
// 实时RemoteConfig监听
sRemoteConfig.addOnConfigUpdateListener(new ConfigUpdateListener() {
@Override
public void onUpdate(@NonNull ConfigUpdate configUpdate) {
sRemoteConfig.fetchAndActivate().addOnCompleteListener (task -> {
if (task.isSuccessful()) {
Slog.d("fetchAndActivate success");
} else {
Slog.d("fetchAndActivate fail");
}
});
}
@Override
public void onError(@NonNull FirebaseRemoteConfigException error) {
Slog.d("Config update error with code: " + error);
}
});
}
private void initFCM() {
FirebaseMessaging.getInstance().getToken().addOnCompleteListener( task -> {
if (!task.isSuccessful()) {
Slog.d("Fetching FCM registration token failed ${task.exception}");
return;
}
//TokenResult token = task.getResult();
Slog.d("Fetching FCM FirebaseMessaging token:{}\n"+task.getResult());
});
}
FirebaseAnalytics firebaseAnalytics = OsportApplication.getAnalytics();
if (!btdevice.isEmpty()) {
firebaseAnalytics.setUserProperty("btdevice", btdevice);
firebaseAnalytics.setUserProperty("protoID", protoID);
firebaseAnalytics.setUserProperty("adapterID", adapterId);
firebaseAnalytics.setUserProperty("curVersion", curVersion);
}
FirebaseInstallations.getInstance().getId().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Slog.d("Installation ID: " + task.getResult());
OsportApplication.serverParameter.setFiId(task.getResult());
Slog.d("serverParameter => ${OSportApplciation.serverParameter.toString()}");
} else {
Slog.e("Installations", "Unable to get Installation ID");
}
} );
到了这里,关于实现firebase FCM和Analytics的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!