Добавить пользователя в группу Firebase - PullRequest
0 голосов
/ 02 января 2019

Поэтому я попытался добавить пользователя в группу Firebase, чтобы иметь возможность отправлять уведомления о данных нескольким пользователям в простом приложении, которое отображает уведомление при его получении.

Моя проблема в том, что, следуя документации Firebase, я попытался добавить следующую строку в свой mainactivity.java

FirebaseMessaging.getInstance().subscribeToTopic("weather")

Но студия android сказала мне, что .getInstance не может быть вызван / "Не удается разрешить символ 'getInstance' ', поэтому я не знаю, что сделать, чтобы он работал

редактирование:

Приложение build.gradle:

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.shayru.myapplication"
        minSdkVersion 21
        targetSdkVersion 28
        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:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'com.google.firebase:firebase-core:16.0.6'
}

Проект build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        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
}

и при необходимости это то, что делает mainacctivity:

package com.shayru.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.firebase.messaging.FirebaseMessaging;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    FirebaseMessaging.getInstance().subscribeToTopic("News");


}

1 Ответ

0 голосов
/ 02 января 2019

Проблема не в Firebase, а в вашем коде.Подпишитесь на тему в вашем методе onCreate(), а не вне метода.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FirebaseMessaging.getInstance().subscribeToTopic("News");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...