【问题标题】:Simple Firebase notification not working on Android简单的 Firebase 通知在 Android 上不起作用
【发布时间】:2021-03-26 15:48:49
【问题描述】:

所以,我已经尝试了几个小时来使通知工作,但没有成功。 我的目标是从 Firebase 控制台发送通知并在手机上接收。这意味着我应该只将库添加到我的应用程序并在 Firebase 上配置一个项目。

在我的应用中,我已经在使用 firebase 进行存储、分析和广告,没有任何问题。

我关注了the official tutorialthis video

我没有在清单中添加服务,因为我不需要做任何特定的消息处理

这是我的清单:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.domain.myapp">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
        <application
            android:name=".App"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.myapp">
            <activity android:name="com.domain.myapp.MainActivity"
                android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.provider"
                android:exported="false"
                android:requestLegacyExternalStorage="true"
    
                android:grantUriPermissions="true">
    
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths"/>
            </provider>
    
            <meta-data
                android:name="com.google.android.gms.ads.APPLICATION_ID"
                android:value="myid"/>
        </application>
    
    </manifest>

根级 build.gradle


    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        ext.kotlin_version = "1.4.20"
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:4.1.3'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath 'com.google.gms:google-services:4.3.5'
            def nav_version = "2.3.4"
            classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
            // 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
    }

应用级构建文件


    plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id 'kotlin-android-extensions'
        id 'androidx.navigation.safeargs.kotlin'
        id 'kotlin-kapt'
        id 'com.google.gms.google-services'
    }
    
    android {
        signingConfigs {
            release {
                storeFile file('keystore')
                storePassword 'psw'
                keyAlias 'key0'
                keyPassword 'psw'
            }
        }
        compileSdkVersion 30
        buildToolsVersion "29.0.3"
    
        defaultConfig {
            applicationId "com.domain.myapp"
            minSdkVersion 22
            targetSdkVersion 30
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            def AD_UNIT_ID = "AD_UNIT_ID"
    
            debug {
                buildConfigField "String", AD_UNIT_ID, AD_UNIT_ID_TEST
            }
    
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                buildConfigField "String", AD_UNIT_ID, AD_UNIT_ID_PROD
                signingConfig signingConfigs.release
            }
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    
    dependencies {
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'com.google.firebase:firebase-storage:19.2.1'
        implementation 'com.google.firebase:firebase-messaging-ktx:21.0.1'
        implementation 'com.google.firebase:firebase-analytics-ktx:18.0.2'
        def nav_version = "2.3.4"
        implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
        implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
        implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
        implementation "androidx.navigation:navigation-compose:1.0.0-alpha09"
    
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation 'androidx.core:core-ktx:1.3.2'
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation 'com.google.android.material:material:1.3.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    
        implementation("com.github.bumptech.glide:glide:4.12.0")
        implementation("com.github.bumptech.glide:recyclerview-integration:4.11.0")
        kapt 'com.github.bumptech.glide:compiler:4.12.0'
    
        implementation 'com.firebaseui:firebase-ui-storage:7.1.1'
    
        implementation 'com.github.smarteist:autoimageslider:1.4.0'
    
        implementation 'com.google.android.gms:play-services-ads:19.8.0'
    
        testImplementation 'junit:junit:4.+'
        androidTestImplementation 'androidx.test.ext:junit:1.1.2'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    }

这是我的 App 类:


    class App: Application() {
    
        override fun onCreate() {
            super.onCreate()
            MobileAds.initialize(this)
        }
    }

这是我的主要活动:


    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            setupAdViewInto(adContainer)
        }
    
        private fun setupAdViewInto(adContainer: LinearLayout, adSize: AdSize = SMART_BANNER) {
            val adView = AdView(this)
            adView.adSize = adSize
            adView.adUnitId = AD_UNIT_ID
            adContainer.addView(adView)
            adView.loadAd(AdRequest.Builder().build())
        }
    
    }

在 firebase 控制台上我看到了这个:

注意:我已经尝试从头开始创建一个新应用程序并执行相同的操作并且它正在工作!

【问题讨论】:

    标签: android firebase notifications firebase-cloud-messaging


    【解决方案1】:

    我得到了它的工作,但我不知道如何......我在想是 Android Studio 的错。

    我已经完成了这些步骤

    • 在 firebase 上创建了一个新项目
    • 下载并导入新的 google-services.json
    • 清理缓存数据并使用 Android Studio 中的 Run 'app' 按钮在模拟器上重新安装应用
    • 该应用仍在显示旧 Firebase 应用中的内容,就像它使用旧的 google-services.json 一样
    • 从 Android Studio 中删除了 gradle 的构建文件夹
    • 使用旧配置重新安装应用程序
    • 通知现已生效
    • WTF!??

    我认为问题是由于 gralde/android studio 造成的。

    我在这上面浪费了大约 5 个小时...我喜欢编程 :) :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      相关资源
      最近更新 更多