Android 使用蜂窝数据时无法一致地接收Firebase云消息数据
与stackoverflow中的其他帖子不同,我的firebase应用程序实际上正在运行,但并不一致。 在大多数情况下,当设备使用蜂窝数据时,应用程序无法接收任何消息。 (随机工作,一段时间后不工作) 但一旦我连接了Wi-Fi,一切都会正常工作,在断开Wi-Fi并切换到使用蜂窝数据后,它仍会工作几分钟。应用程序和google play服务已经设置为允许在后台使用手机数据和不受限制的数据使用 代码如下: MyFirebaseMessagingServiceAndroid 使用蜂窝数据时无法一致地接收Firebase云消息数据,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,与stackoverflow中的其他帖子不同,我的firebase应用程序实际上正在运行,但并不一致。 在大多数情况下,当设备使用蜂窝数据时,应用程序无法接收任何消息。 (随机工作,一段时间后不工作) 但一旦我连接了Wi-Fi,一切都会正常工作,在断开Wi-Fi并切换到使用蜂窝数据后,它仍会工作几分钟。应用程序和google play服务已经设置为允许在后台使用手机数据和不受限制的数据使用 代码如下: MyFirebaseMessagingService public class MyFireb
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d("received", "Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
if (data != null) {
if (data.containsKey("action")) {
// do something
}
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d("received", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
@Override
public void onNewToken(String token) {
Log.d("token", "Refreshed token: " + token);
storeToken(token);
sendToken(token); //send token to my server
}
}
格拉德尔项目:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
有人能解释为什么以及如何解决这个问题吗?
谢谢
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.myfirebaseapp"
minSdkVersion 24
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.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation('com.squareup.retrofit2:converter-simplexml:2.3.0') {
exclude group: 'xpp3', module: 'xpp3'
}
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}