I've just installed Firebase in my Android app to enable push notifications. I confirmed it's working by sending a push notification from the Firebase console to all devices running the app. Now I'm trying to get the FCM token (the unique identifier of the app instance) so I can send messages to specific devices. However, when I override the onNewToken() function in my class extending the FirebaseMessagingService, the method is not overridden. According to the docs it should work. The other methods in the doc are successfully overridden. I've found and decompiled the FirebaseMessagingService class file in my external libraries, and found out that the onNewToken function is indeed not present there.
How can I override a function that should exist according to the docs, but doesn't exist in the class file?
I'm using com.google.firebase:firebase-messaging:17.3.0 and com.google.firebase:firebase-core:16.0.0
The reason is because, as you comment before, Firebase Instance ID is deprecated but it refers to the complet class, then you should use #Override ... onNewToken in a different class, not in Firebase Instance Id class .Sorry for my english. The example is this:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onNewToken(String s) {
Log.e("NEW_TOKEN", s);
}
Related
I wanted to make a network fragment on my Android app so I could upload and download information from my database server. Following the guide on Android networking on the Developer page and the corresponding example project on Github, I created a demo to test a network connection.
I copied the files DowloadCallback.java (contains the implemented network interface) and NetworkFragment.java (the network fragment thread) word for word from the example project and added the necessary permissions in AndroidManifest.xml.
When I tried to implement the fragment into my activity, I got errors in several rather odd and counterintuitive places:
Code
public class MainActivity extends FragmentActivity implements DownloadCallback {
...
#Override
public void updateFromDownload(String result) {
...
}
}
Errors
Class 'MainActivity' must either be declared in abstract or implement method 'updateFromDownload(T)' in 'DownloadCallback'
Method does not override method from its superclass
The public class says it needs a particular method for the class to implement DownloadCallback, but when I add such method it says that it does not exist in its superclass. How can these errors coexist? How can I fix this?
By the way, this is the exact same way the main activity class is defined in the sample project. Also I have posted this as an issue on Github but I am hoping to get a quicker response and attention here.
base on this You have to define <T> for DownloadCallback
In your case T is String
So change your code like below
public class MainActivity extends FragmentActivity implements DownloadCallback<String>
I am trying to log custom events in the Android app using Facebook analytics. The custom events are working correctly whenever I log them in MainActivity and other Activities in my app, but are not getting logged at all when I log them inside Fragments and static methods.
I initialize the Facebook SDK and AppEventsLogger in my Application class like his:
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
And I track events in my app like this:
TrackingService.logEvent(getActivity(), "Public.Clicked");
And here is the logEvent method that I defined in my TrackingService:
static public void logEvent(Activity activity, String event) {
AppEventsLogger logger = AppEventsLogger.newLogger(activity);
logger.logEvent(event);
}
When I call logEvent in a method in my MainActivity, the event shows up in my Facebook Analytics dashboard:
TrackingService.logEvent(MainActivity.this, "SearchTestA");
When I call logEvent in a method in one of my Fragments that is contained within MainActivity, the event does not show up in my dashboard:
TrackingService.logEvent(getActivity(), "Public.Clicked");
I have confirmed that getActivity() above returns an instance of MainActivity. Any idea what I'm doing wrong?
Turns out you can't have periods in your event names. Underscores and spaces are allowed, though.
I'm building an android app that access a server. I'm using a full google solution. The backend is in GAE and I'm using endpoints to expose my API, I'm also using GCM. I use the auto generate tools that are offered by android studio to get my classes.
In my app module I have a class called offer, this is where I put data to be sent to the server, I have also an AsyncTask class that allows to make the api call.
In my backend module I have the exposed API and I also I have a class offer from which the API is generated by android studio and app engine sdk.
Now my problem is I made an attempt, but it resulted in failure, its like the classes in app and backend are not compatible. Whereas they are the same, in fact the one in backend is a simple copy from the one in app, the difference is the "objectify" annotation that I added. Below are pieces from my code and screenshots of my project structure.
public class InsertOfferAsyncTask extends AsyncTask <Offer, Void, Boolean>{
private static OfferApi offer_service;
private Context context;
public InsertOfferAsyncTask(Context context) {
this.context = context;
}
protected Boolean doInBackground(Offer... offer) {
if (offer_service == null) {
OfferApi.Builder builder = new OfferApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setRootUrl("https://flawless-snow-95011.appspot.com/_ah/api/");
offer_service = builder.build();
}
try {
offer_service.insert(offer[0]); //this where I make the actual API call, I know I shouldn't use Object, it was an attempt to make it work
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
This is a part from where I call the AsyncTask, which is the code above.
Log.i("offer", offer.getUsr_id());
Log.i("offer_id", String.valueOf(offer.getId()));
Log.i("offer_date", offer.getPost());
new InsertOfferAsyncTask(getActivity().getBaseContext()).execute(offer);
getActivity().finish();
All the code above is taken from my app module, the following is the endpoint code that code generated, I am posting only the part I make a call to.
#ApiMethod(
name = "insert",
path = "offer",
httpMethod = ApiMethod.HttpMethod.POST)
public Offer insert(Offer offer) {
ofy().save().entity(offer).now();
logger.info("Created Offer with ID: " + offer.getId());
return ofy().load().entity(offer).now();
}
What I need now is how I can use what I have to send my data to the server. I know that I can connect to the server, I tested.
This is the error message, that i get when I try to build.
Error:(233, 73) error: no suitable method found for execute(.model.Offer)
method AsyncTask.execute(Runnable) is not applicable
(actual argument .model.Offer cannot be converted to Runnable by method invocation conversion)
method AsyncTask.execute(backend.model.offerApi.model.Offer...) is not applicable
(argument type app.model.Offer does not conform to vararg element type backend.model.offerApi.model.Offer)
Any help?? should I use JSON (I doubt, the job is done by the auto-generated classes, as it is shown in the builder)
Could it be that you are using two different "Offer" objects
app.model.Offer and backend.model.offerApi.model.Offer?
The type backend.model.offerApi.model.Offer appears to be the one generated for your Endpoints API, you need to use that type everywhere on the client (android) side.
I believe that you should create a separate project that contains all classes that are shared between the android app and your api.
google docs have a tutorial on how to make an AIDL service. However, when I tried to do the same, I got an error "The method registerCallback(IRemoteInterface) is undefined for the type IRemoteInterface.
I have not seen this "registerCallback" method in any tutorial that I have been googling and my question is why is this not working/why do other places not use it?
I feel like part of my issue is a fundamental misunderstanding about services and their 'callback' to send information to what it is bound to it.
Thanks!
AIDL:
package com.mine.ben;
import com.mine.servicenexus.RelPoint;
interface IRemoteInterface {
Location getLastLocation();
RelPoint getRelPoint();
int logControlActivity(in String text,in int severity);
int getRunningStatus();
}
Updated question:
I get a syntax error in my AIDL file when i add
void registerCallback(IRemoteServiceCallback);
void unregisterCallback(IRemoteServiceCallback);
I have cleaned my workspace and it builds automatically. Is this a problem with the gen file?
Few lines from my application:
interface IGpsService {
Bundle getNavigationMessage();
void resetStatistics();
void recordingEnable(boolean recordingEnabled, boolean continueLastTrack);
void registerCallback(IGpsChanged cb);
void unregisterCallback(IGpsChanged cb);
boolean isRecordingEnabled();
void setGhost(int trackId);
void startGhost();
void sendUserEvent(int eventId);
}
second file for calback interface itself:
interface IGpsChanged{
oneway void onLocationChanged(out Bundle message);
}
Best of all - usually you do not need those .aidl definitions. This is necessary only when exposing service methods outside of the application.
It's also helpful to understand how it works - forst aidl defines basic service's methods. registerCallback is not a "special" method, it's as oridinary as any other, just enables defining callbacks for two-way communication (in this particular case - to send some position info from sesrvice to binded activity (or other component).
As you mentioned in comment - you do not want to create service, just consume some service from outside of application. In that case you need those external aidl file, not the one written by you. Stubs for consuming service's method will be generated in the /gen directory.
google docs have a tutorial on how to make an AIDL service.
That is not a tutorial. It is just ordinary documentation.
However, when I tried to do the same, I got an error "The method registerCallback(IRemoteInterface) is undefined for the type IRemoteInterface.
That is because you do not have a method named registerCallback() in your AIDL.
I have not seen this "registerCallback" method in any tutorial
It is not in a tutorial. The only occurrences of registerCallback() in the Web page that you linked to are from "some sample code demonstrating calling an AIDL-created service, taken from the Remote Service sample in the ApiDemos project". The ApiDemos project is in your SDK installation, if you elected to download sample code from the SDK Manager.
Code example from AIDL guide that you have referenced is taken from the Remote Service sample in the ApiDemos project.
And registerCallback() in it is implemented by using android.os.RemoteCallbackList<E extends android.os.IInterface> object.
Make an .aidl file like:
package com.example;
oneway interface IRemoteServiceCallback {
/**
* Goes to client.
*/
void valueChanged(int value);
}
Make another .aidl file where you import this interface and use it as a method's parameter like:
package com.example;
import com.example.IRemoteServiceCallback;
interface IRemoteService {
/**
* Goes to service.
*/
void registerCallback(IRemoteServiceCallback cb);
}
Generate code from .aidl on both sides.
Implement IRemoteService.Stub in service and return it in onBind()
Implement IRemoteServiceCallback.Stub in client and pass it in ServiceConnection's onServiceConnected() callback to the received from IRemoteService.Stub.asInterface() instance of IRemoteService.
Now you service can talk back to the client over the passed IRemoteServiceCallback implementation.
I have Android application and own Application derived class holding some internal data.
Among other there are some string fields. The problem is that if I put the application in foreground, work on other application, switch back to my app again, the app may be restarted because it got killed by system. Unfortunatelly the Application object seems not to be created again because the onCreate method of application object doesn't get called and all fields are set to null. My Activity gets recreated but all Application's object fields are null. When is the Application.onCreate method called? How to handle it?
there is no onCreate that you can register to.in later API's there's a way to register to the Activity lifecycle functions. and then you can do what ever you want.
basically, what you should do is use SharedPrefrences for storing information.
what I would do is:
class MyApp extends Application {
private static String someResource = null;
public static String getSomeResource(Context context) {
if(someResource == null) {
SharedPrefrences prefs = (SharedPrefrences)
context.getSystemService(Context.SHARED_PREFRENCES);
someResource = prefs.getString(SOME_RESOURCE, null);
}
return someResource;
}
Application onCreate() will called only for one time during its life-cycle, i.e.. only when application is started.
As suggested by thepoosh below answer is valid ,if your application is killed,still the data is saved in shared preference.