我一直在努力解决这个问题,所以我决定问你。
我正在开发聊天应用程序,并使用Firebase Functions进行推送通知。我总是两次从onMessageReceived获取数据。对于某些人来说,解决方案是删除de gcm依赖项,仅使用firebase依赖项,但我认为并非如此。无论如何,我正在向您展示所有代码:
这是我的index.js:
exports.notifications = functions.database.ref('chats/{chatUid}/messages/{pushId}').onWrite( (change, context) => {
var valueObject = change.after.val();
var chatUid = change.after.ref.parent.parent.key;
var data = {
'data' : {
'conversation_id' : chatUid,
'message_key' : change.after.key,
'message_title' : valueObject.title,
'message_body' : valueObject.message,
'userFrom' : valueObject.userFrom.toString()
}
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic(chatUid, data, options)
});
我的gradle设置:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 27
defaultConfig {
applicationId "xxx"
minSdkVersion 21
targetSdkVersion 27
versionCode xxx
versionName "x.x.x"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildToolsVersion '27.0.3'
productFlavors {
}
}
ext {
support_version = '27.1.1'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.support:design:$support_version"
implementation "com.android.support:appcompat-v7:$support_version"
implementation "com.android.support:customtabs:$support_version"
implementation "com.android.support:support-v4:$support_version"
implementation "com.android.support:cardview-v7:$support_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation "com.android.support:percent:$support_version"
implementation "com.android.support:recyclerview-v7:$support_version"
implementation 'com.android.support:support-v4:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation('com.squareup.retrofit2:retrofit:2.3.0') {
exclude module: 'okhttp'
}
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.google.maps.android:android-maps-utils:0.5'
implementation 'com.facebook.android:facebook-android-sdk:4.18.0'
implementation 'com.google.firebase:firebase-analytics:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.google.firebase:firebase-config:16.0.0'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.1.0'
implementation 'com.firebaseui:firebase-ui-database:3.3.1'
implementation 'com.firebaseui:firebase-ui-storage:3.3.1'
implementation 'commons-codec:commons-codec:1.11'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'}
谢谢您的时间:D
答案 0 :(得分:0)
我很愚蠢。
这是我在android代码中的错,与firebase函数无关:
我使用的是onWrite(index.js),在我的android代码中,我在firebase数据库中写了两次,一个用于编写message_key( push()),另一个用于设置属性(< strong> setValue()):
finalReference.push().apply {
val map2 = HashMap<String, Any?>()
val dateFormat = SimpleDateFormat(DATE_FORMAT, Locale.getDefault())
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
map2[FB.DATE] = dateFormat.format(Date())
map2[FB.MESSAGE] = entryText.text.toString()
map2[FB.READ] = false
map2[FB.RECEIVED] = false
map2[FB.TITLE] = fbUser.displayName
map2[FB.USER_FROM] = fbUser.uid.toLong()
map2[FB.USER_TO] = conversation_data!!.users_id.find { it != fbUser.uid }?.toLong()
setValue(map2).addOnSuccessListener {
child(FB.RECEIVED).setValue(true)
}
entryText.setText("")
}
因此解决方案是将index.js上的 onCreate 更改为 onWrite 。
希望它对任何人都有帮助。