I'm trying to import Firebase and FCM into my android app, but I'm getting
Unresolved class for firebase ".MyFirebaseInstanceIDService"
Unresolved package for firebase ".java.MyFirebaseMessagingService"
as an "error." Of course, it isn't stopping my app from running or building, but I cannot call onTokenRefresh in any class at all. So, I believe this problem is preventing me from using Firebase.
here is my app build;
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.northlandcaps.crisis_response"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
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.android.support:mediarouter-v7:28.0.0'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
}
apply plugin: 'com.google.gms.google-services'
Screenshot of problem
This how you two classes should look like:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
And:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
sendNotification(notification, data);
}
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
//Send notification
}
}
Related
I started to encounter this error when I upgraded the version of the project to androidx. I also upgraded the firebase sdks. I started getting error "Cannot resolve symbol 'FirebaseInstanceId' " . With this method, I was taking the user's token and printing it to the database. How can I solve this problem?
Manifest :
<service
android:name=".MyFirebaseIdService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_SERVICE" />
</intent-filter>
MyFirebaseIdService;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseIdService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
super.onTokenRefresh();
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String refreshToken = FirebaseInstanceId.getInstance().getToken();
if (firebaseUser != null){
updateToken(refreshToken);
}
}
private void updateToken(String refreshToken) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
Token token = new Token(refreshToken);
reference.child(firebaseUser.getUid()).setValue(token);
}
}
Token ( model class ):
public class Token {
private String token;
public Token(String token) {
this.token = token;
}
public Token() {
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
I was rewriting the token code every time the user entered the home page.
MainActiviy :
updateToken(FirebaseInstanceId.getInstance().getToken());
private void updateToken(String token){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
Token token1 = new Token(token);
reference.child(fuser.getUid()).setValue(token1);
}
build.gradle ( project )
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle ( module )
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
android {
compileSdkVersion 30
defaultConfig {
applicationId "***"
minSdkVersion 19
targetSdkVersion 30
versionCode 21
versionName "1.1.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
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:design: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.firebaseui:firebase-ui-database:4.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.github.orangegangsters:swipy:1.2.3#aar'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.firebaseui:firebase-ui-database:4.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-inappmessaging-display:17.0.1'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.android.gms:play-services-ads:15.0.1'
implementation('com.google.apis:google-api-services-people:v1-rev2-1.21.0')
{
exclude module: 'guava-jdk5'
}
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.google.firebase:firebase-ads:15.0.1'
implementation "io.grpc:grpc-okhttp:1.32.2"
implementation 'com.android.billingclient:billing:3.0.0'
implementation platform('com.google.firebase:firebase-bom:29.3.0')
implementation 'com.google.firebase:firebase-crashlytics'
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-core:20.1.2'
implementation 'com.google.firebase:firebase-messaging:23.0.2'
implementation 'com.google.firebase:firebase-auth:21.0.3'
}
apply plugin: 'com.google.gms.google-services'
----- UPDATE ---- SOLUTÄ°ON :
FirebaseMessaging.getInstance ().getToken ()
.addOnCompleteListener ( task -> {
if (!task.isSuccessful ()) {
return;
}
if (null != task.getResult ()) {
String firebaseMessagingToken = Objects.requireNonNull ( task.getResult () );
updateToken(firebaseMessagingToken);
}
} );
private void updateToken(String firebaseMessagingToken) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
Token token = new Token(firebaseMessagingToken);
reference.child(firebaseUser.getUid()).setValue(token);
}
I'm experimenting with getting the Android Advertising Id, but I can't find the proper way. In fact, I can't even get the Advertising Id Provider.
isAdvertisingIdProviderAvailable( ) always returns false. I'm using a Samsung with 8.0 + PlayStore and also on the emulator with 8.1 + Google Play, running as a debug build.
I've followed this guide:
https://developer.android.com/training/articles/ad-id
This must be something simple, but I can't see it.
Thanks for any suggestions.
I created a blank project and this is my code:
public class MainActivity extends AppCompatActivity {
public static final String TAG="MYTAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isProvider = AdvertisingIdClient.isAdvertisingIdProviderAvailable(getApplicationContext());
Log.i(TAG, "isProviderAvailable:" + isProvider);
}
}
Gradle:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "company.com.adidviewer"
minSdkVersion 26
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.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.gms:play-services-ads:18.3.0'
implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
//implementation 'androidx.ads:ads-identifier-common:1.0.0-alpha04'
//implementation 'androidx.ads:ads-identifier-provider:1.0.0-alpha04'
// Used for the calls to addCallback() in the snippets on this page.
//implementation 'com.google.guava:guava:28.0-android'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
Manifest
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
</application>
If you want to get the advertising id for your published app, use the following approach:
Add this line to your gradle file;
com.google.android.gms:play-services-ads-identifier:16.0.0
And the code like this;
private void determineAdvertisingInfo(Context context) {
AsyncTask.execute(new Runnable() {
#Override
public void run() {
try {
AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
// You should check this in case the user disabled it from settings
if (!advertisingIdInfo.isLimitAdTrackingEnabled()) {
String id = advertisingIdInfo.getId();
// getUserAttributes(id);
} else {
//If you stored the id you should remove it
}
} catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
}
});
}
There is a note specifying this in the document as well:
Note
I am new to android development. Before posting this question, I have checked if same issue had been posted and had a solution. I couldn't find and so posting here.
I am trying to get FCM token and using the below piece of code.
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(#NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
// Log and toast
Log.d(TAG, token);
}
});
I am not getting any error. However method onComplete is not getting executed.
Could please someone help me on this. I have been trying this for couple of days.
Please note that, as per few suggestions on SOF, I followed .on SuccessfullListener also. But the same issue with that as well.
I am using the below things.
Manifest:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
App level Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.abc.xyz"
minSdkVersion 21
multiDexEnabled true
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:design:28.0.0'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.google.firebase:firebase-messaging:17.5.0'
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.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-auth:16.2.0'
implementation 'com.google.firebase:firebase-firestore:18.2.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.firebase:firebase-database:16.1.0'
}
apply plugin: 'com.google.gms.google-services'
Project Gradle:
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
addOnCompleteListener runs on the background thread so it's not clear when it will be done.
I advice using onNewToken(String token) in your custom FirebaseMessagingService :
public class FireBaseCustomReciver extends FirebaseMessagingService {
private static final String TAG = "aaa";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
}
#Override
public void onNewToken(String token) {
Log.e("aaa", "Refreshed token: " + token);
}
}
I followed vogella article: http://www.vogella.com/tutorials/Dagger/article.html
I did everything step by step as they suggested. My build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "test.daggertest"
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'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.dagger:dagger:2.15'
implementation 'com.google.dagger:dagger-android:2.15'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
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'
}
MyApplication class:
public class MyApplication extends Application implements HasActivityInjector
{
#Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
#Override
public void onCreate()
{
super.onCreate();
DaggerMyApplicationComponent.create().inject(this);
}
#Override
public DispatchingAndroidInjector<Activity> activityInjector()
{
return dispatchingAndroidInjector;
}
}
Then I did Rebuild project, and still it throws error DaggerMyApplicationComponent is unresolved.
Am I missing something or their article is outdated? Any ideas?
I try to undo a delete operation through the action provided by Snackbar.
Here is the delete operation:
dbRef.child(footballer.getFullName()).removeValue();
When I call the undo operation through the command added below, getting com.google.firebase.database.DatabaseException: Failed to parse node with class exception:
dbRef.setValue(footballer.getFullName(), footballer);
dbRef is assigned to the root of the collection:
DatabaseReference dbRef = database.getReference("footballers");
Here is my Footballer model:
#IgnoreExtraProperties
public class Footballer {
private String fullName;
private String desc;
private String photoUrl;
private String teamUrl;
public Footballer() {
}
public Footballer(String fullName, String desc, String photoUrl, String teamUrl) {
this.fullName = fullName;
this.desc = desc;
this.photoUrl = photoUrl;
this.teamUrl = teamUrl;
}
// getter-setter methods
}
Here is the structure of Firebase database:
footballers
----Cristiano Ronaldo
--------fullName
--------desc
--------photoUrl
--------teamUrl
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.tk"
minSdkVersion 23
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.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-database:11.0.4'
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'
implementation 'com.android.support:recyclerview-v7:26.1.0'
// Support Design Lib
implementation 'com.android.support:design:26.1.0'
// Swipe Action
compile 'co.dift.ui.swipetoaction:library:1.1'
// Facebook Fresco
compile 'com.facebook.fresco:fresco:0.6.1'
}
apply plugin: 'com.google.gms.google-services'