Simple FirebaseApp is crashing after installation - android

I am learning to implement firebase in an android studio. And I have studied various tutorials on this subject.
So in order to minimize the chances of making mistakes, I decided to implement a simple token generated scripts in my mainactivity inside the onCreate() method; these codes below:
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;
}
String token = task.getResult().getToken();
}
});
Then the app keeps crashing after generating its apk.
Then following codes were then implemented:
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
#Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
}
});
I am using latest dependency :
firebase messaging:17.6.0
firebase-core:16.0.8
The problem is each time I generated the APK , the app keeps crashing with error message "app has stopped".
But when I built any other android app without firebase ,the app will work perfectly.

Related

Unable to find FCM token

I am trying to implement notifications and can send a notification using the firebase console to all devices of the app, however I am having problem trying to retrieve the token of a device so i can send the notification to one particular device. I have a service that extends FirebaseMessagingService and includes the method onNewtoken seen below. I have added this service to my manifest and tried running the app but still unable to find the token. Is there something im doing wrong?
#Override
public void onNewToken(#NonNull String s) {
super.onNewToken(s);
Log.d("NEW_TOKEN",s);
}
OnNewtoken will trigger only by following below scenarios
The app deletes Instance ID
The app is restored on a new device
The user uninstalls/reinstalls the app The user clears app data.
you can get user token by below code as well
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
#Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
// send it to server
}
});
You can get token forcefully like this u can call this code from onCreate() as sometimes when app has already generated to token it does not call onNewToken
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(#NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
KLog.w("getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
if (task.getResult() != null) {
String token = task.getResult().getToken();
}
}
});

How can I subscribe user to a topic for notifications as soon as app starts up the android app?

How can I subscribe user to a topic for notifications as soon as app starts up?
I have an app I want to subscribe all those that they will install the app automatically on firebase messaging so that i can send them notification on ANDROID STUDIO
Per the Firebase Messaging documentation, you can simply call subscribeToTopic in your main activity's onCreate. To do this, you can add this line:
FirebaseMessaging.getInstance().subscribeToTopic("your_topic_name");
If you would like to take some action after subscribing, you can add a listener that will get called when the subscription action is complete. For example (in Java):
FirebaseMessaging.getInstance().subscribeToTopic("your_topic_name")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
// do something, such as showing a Toast to the user
} else {
// subscribing failed for some reason, maybe perform some error-handling
}
}
});
There are more examples in the documentation above, including a Kotlin example (you didn't specify which language you were using, so I made an assumption), which you may find useful. Hope that helps!
FirebaseMessaging.getInstance().subscribeToTopic("your_topic")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
String msg = getString(R.string.msg_subscribed);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
for more information read the documentation

Why task is unsuccessful for firebase authentication

I tried to sign in with Firebase. I coded these codes but when I sign in on my app, I take the message from !task.isSuccessful at my code. How can I solve this problem basically? I'm new for the android studio.
I think I could code wrongly in this part when I click sign in button call this function
private void createAccount(){
String email=userEmail.getText().toString();
String password=userPassword.getText().toString();
mAuth.createUserWithEmailAndPassword(email,password).
addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
//when Authentication is successful
if (task.isSuccessful()){
Log.d(TAG,"createUserWithEmail:onComplete"+task.isSuccessful());
Toast.makeText(SignInActivity.this,"Registration was
provided",Toast.LENGTH_SHORT).show();
Intent signIn= new
Intent(SignInActivity.this,UserMainActivity.class);
startActivity(signIn);
}//when Authentication is failed
else {
Toast.makeText(SignInActivity.
this,"Registration is unsuccessful! Please try
again...",Toast.LENGTH_SHORT).show();
}
}
});
}

firebase signInWithCustomToken not working

I'm trying to use firebase authenticate my users with a custom token i generate
FirebaseAuth.getInstance().signInWithCustomToken(token).addOnCanceledListener(new OnCanceledListener() {
#Override
public void onCanceled() {
System.out.println("canceled");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
System.out.println("failure");
}
}).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
System.out.println("complete");
}});
token is a valid authentication token i get from my server
This method is called on a WorkerThread
but no message is printed on console
what might be happening?
===========UPDATE==========
I tested and it work fine on API lvl 27
but it doens't work on API 16
according to firebase page, fibase requires minimum api 9
A couple of ideas:
Make sure you have a valid token. You can paste it into https://jwt.io/ and compare it to a valid token or check the elements that are required as described here. The token has to be signed with a private key of the same Firebase project that you are trying to connect to. You can get the private key from the Firebase Console / Project Settings / Service Accounts
In the OnFailureListener, print out the exception message, e.g. e.getMessage().
addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d(TAG, e.getMessage());
}

Firebase auth anonymous login not responding when screen locked

Firebase auth working perfectly and addOnCompleteListener, addOnFailureListener, addOnSuccessListener events fired after successful anonymous login. But when I installed app using AndroidStudio and unlock screen after installation and app in running mode Firebase Auth never respond and stuck. Following event never fired when Android Studio launches app and triggered following code in activity while phone locked:
task.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.v(TAG, "Anonymous signInAnonymously:complete");
}
});
task.addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e(TAG, e.getMessage(), e);
}
});
task.addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
Log.v(TAG, "Anonymous signInAnonymously:success");
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
If I install and run my application from AndroidStudio with unlocked phone its working fine.
Is it normal behavior?
It all depends on the version of Firebase you are using.
Try to use the latest one for compatibility.
Also make sure you add Firebase from the Firebase Assistant, which adds all the required code.
The service require the SHA? Make sure the Debug key (default usually) and Production key SHA are added and the JSON file is added to the project.

Categories

Resources