Android Firebase即使在更改应用程序包名称后仍发送通知

Android Firebase即使在更改应用程序包名称后仍发送通知,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我有一个奇怪的疑问。 我在android应用程序中使用FCM。假设我的应用程序的包名是com.example.example,我使用com.example.example从FCM获得了google service.json文件,我得到的令牌和通知也运行良好。 但是现在我将把包com.example.example重新分解为代码中的com.sample.sample,包括googleservice.json文件,我也可以在这里获得令牌。 现在,如果我带着包com.example.example进入

我有一个奇怪的疑问。 我在android应用程序中使用FCM。假设我的应用程序的包名是
com.example.example
,我使用
com.example.example
从FCM获得了
google service.json
文件,我得到的令牌和通知也运行良好。 但是现在我将把包com.example.example重新分解为代码中的com.sample.sample,包括
googleservice.json
文件,我也可以在这里获得令牌。 现在,如果我带着包
com.example.example
进入FCM项目,并从那里ping,通知将到达
com.sample.sample
我怀疑它不应该是正确的? 请帮我解决这个问题

这是我的应用程序级gradle,包名已在firbase注册:

apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.pushnotification.fcmnotifier"
    minSdkVersion 22
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:mediarouter-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.google.android.gms:play-services:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
}

apply plugin: 'com.google.gms.google-services'
这是我复制的项目的gradle,包名已重构:

apply plugin: 'com.android.application'


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

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:mediarouter-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.google.android.gms:play-services:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
}

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

如果要在应用程序的build.gradle文件中更改应用程序id,则需要使用新id在firebase console中注册新应用程序。 然后下载该应用程序的
googleservice.json
文件

手动更改json文件中的应用程序id将不起作用

还请注意,如果您只更改包名,即清单文件中使用的名称,谷歌将无法识别该名称。更改build.gradle中的应用程序id需要有新的应用程序id


有关更多信息,请检查此项:

我不确定这一点,但可能是此问题背后的原因

更改
程序包
名称引用的
程序包
后,您必须更改
应用程序ID
表单应用程序级渐变文件,然后更新每个
程序包_name=“com.sample.sample”
(新引用的
程序包
名称)从
googleservices.json
文件


希望这将对您有所帮助

这是firebase支持部门对这个问题的回答。希望能有帮助

感谢您联系Firebase支持部门

看来你的案子可能和这条线索有关。正如我引用我们的一位工程师的话,“请注意,Firebase不使用Java代码中的实际包名,而是使用应用程序的build.gradle文件中的applicationId:……但是,如果您将代码重构到另一个包中,应用程序id将不会更新。这解释了为什么在重构后应用程序将继续工作。”

如果你需要进一步的帮助,请告诉我

问候,, 理查德

这是firebase的最终结论:

我和我们的团队讨论过这一点,结果证明FCM使用GMP应用程序Id而不是程序包名称。这就是为什么即使重构了applicationId和包名,通知仍然可以工作的原因。我还建议您为不同版本的Android应用程序使用不同的Firebase应用程序,而不是只使用一个

问候,,
Richard

您是否为
com.sample.sample
创建了新项目?否。这就是我的问题所在。在使用com.example.example创建之后,我没有在firebase中创建任何项目。在android应用程序中,我用新的包名重构了旧的包名。你必须在Firebase>设置中更改包名扫描你显示你的应用程序级别
gradle
文件请你发布你的
gradle
文件,谢谢你的回复。但问题是我也在build.gradle中更改了appId。但它仍然运行良好。这就是我怀疑的地方。即使在gradle和google-service.json文件中更改appId,它也不应该起作用。但在我的情况下,它的工作!!你在firebase控制台中用新id注册了你的应用程序吗?没有,我没有用新id注册。这就是问题所在。只需转到firebase控制台设置,添加新的android应用程序,提供详细信息,完成后,它将为该应用程序提供一个新的json文件,并将其放入您的项目中。现在,这将告诉firebase这是一个新的应用程序。是的,我知道它不应该更改。但我只是出于好奇而改变了,这是有效的。根据firebase的说法,它不应该工作,但它正在工作。是的,也许你的观点是正确的,但这可能是因为这个原因,只要试着从你当前的
谷歌服务.json
文件中将所有的
包名
更改为新的包名,我们就能找到这个问题背后的确切原因。你不能重构包名从google-service.json.what@Kiran我不了解Firebase网站设置