首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不适用于Android的简单Firebase通知

不适用于Android的简单Firebase通知
EN

Stack Overflow用户
提问于 2021-03-26 15:48:49
回答 1查看 342关注 0票数 0

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

在我的应用程序中,我已经在使用防火墙进行存储、分析和广告,没有问题。

我跟踪了官方教程这段视频

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

这是我的舱单:

代码语言:javascript
运行
复制
    <?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

代码语言:javascript
运行
复制
    // 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
    }

应用层构建文件

代码语言:javascript
运行
复制
    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'
    }

这是我的应用程序课程:

代码语言:javascript
运行
复制
    class App: Application() {
    
        override fun onCreate() {
            super.onCreate()
            MobileAds.initialize(this)
        }
    }

这是我的主要活动:

代码语言:javascript
运行
复制
    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控制台上,我看到了以下内容:

注意:我已经尝试了一个新的应用程序从零开始,并做同样的操作,它是有效的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-26 19:28:03

我让它起作用了,但我不知道怎么.我觉得是安卓工作室的错。

我已经完成了这些步骤

  • 创建了一个关于防火墙的新项目
  • 下载并导入新的google-services.json
  • 清除缓存数据,并使用Android中的“app”按钮在模拟器上重新安装应用程序
  • 这个应用程序仍然显示来自旧的防火墙应用程序的东西,就像它在使用旧的google-services.json
  • 从Android删除gradle的构建文件夹
  • 重新安装应用程序,就像以前使用旧配置一样
  • 通知现在起作用了
  • 哇!??

我认为这个问题是因为gralde/android工作室。

我在这件事上损失了5个小时.我喜欢编程:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66820146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档