Now, I save data in datastore of google cloud.
module-backend
#Entity
public class ComprarProducto {
#Id
private String producto;
private boolean comprado;
public ComprarProducto() {
}
public ComprarProducto(String producto) {
this.producto = producto;
this.comprado = false;
}
...
}
Example data of CompraProducto
This data is displayed in a listview. When I add one item, the list is updated and saved in datastore.For save data I use AsyncTask in app.
Then, If I add one item in device1, I want to see that listview as both device1 as device2 is updated, but I don't know how to do it.
I use too GCM (google cloud messaging) and I've got to send a message to devices but only this. I want to see the data update in the device 2 without taking any action.
I want to see the data update in the device 2 without taking any
action.
This is not possible without somehow notifying from the backend server that new data has been saved to datastore. Sending a push notification to device2 (or all other devices or whatever) would definitely be the correct way to do it. What you do is have the push notification tell device2 to query datastore.
If you really don't want to use GCM you COULD have device2 just query datastore every couple minutes but that is not recommended. The correct way to do it is to use GCM to give your device2 a "tickle" to indicate that there is new data in datastore and to query accordingly.
Not sure what kind of app you are building but these are some options.
Related
I want to be able to send a notification to a user IF something changes.
For example, my application is crime-related. So users can submit reports of crimes that have happened in their neighborhoods.
When a new crime is reported, I want to be able to send ALL users in that specific neighbourhood a notification, even if they are not actively using the app.
How can this be done? I'm quite new at this but to my understanding services like Firebase Messaging require you to type out a message manually and select users to send the message to manually. I'm wondering if there's a way this can be done without someone having to manually do work?
Similar to how snapchat/instagram and stuff will send you notifications that someone has sent you a message even when you are not using your phone.
In my case, I just want the same standard notification "New crime in your area" to be displayed...
How can I do this? (Currently for notifications I'm just using Notification Channels), thank you so much!
You can easily do this using Parse Server through FCM integration.
First, you need to setup your Android app to be able to receive push notifications
Just follow this Quickstart: https://docs.parseplatform.org/parse-server/guide/#push-notifications-quick-start
Second, you need to create a cloud code function
I suggest you to create a cloud code function that will receive the neighborhood as parameter, will query for the user installations in that neighborhood and send the push notification to all of them.
It would be something like this:
Parse.Cloud.define('notifyCrime', async req => {
const query = new Parse.Query(Parse.Installation);
query.equalTo('neighborhood', req.params.neighborhood); // I'm supposing you have a field called neighborhood in your installation class - if not, you can save this field there when the user sign up
await Parse.Push.send({
where: query,
data: {
alert: 'There is a crime in your neighborhood'
},
useMasterKey: true
});
});
Reference: https://docs.parseplatform.org/js/guide/#sending-pushes-to-queries
Third, you need to call the cloud function from your Android app
Once some user has reported a crime, you can call the cloud code function that you created in step 2 to notify all other users in the same neighborhood.
It would be something like this:
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("neighborhood", "The neighborhood goes here");
ParseCloud.callFunctionInBackground("notifyCrime", params, new FunctionCallback<Object>() {
void done(Object response, ParseException e) {
if (e == null) {
// The users were successfully notified
}
}
});
Reference: https://docs.parseplatform.org/cloudcode/guide/#cloud-functions
"my understanding services like Firebase Messaging require you to type out a message manually and select users to send the message to manually".
This is not completely true. There is a method name Firebase Topic Messaging, that lets you send notifications to specific user segments only. You have to register from the app for that topic and then, you can send customized message to your user groups based on topics they subscribed to.
I have two android applications in my single firebase project. That project belongs to the connection of vehicles. One app is for the driver and other app is for the passenger. So whenever passenger requests for a ride the driver needs to be notified of that request. So is there any way to send notification to the driver using firebase UID instead of FCM registration token.
I want to know whether registration token will be fix for a particular user or it will refresh/change over time.
Yes you can, use this to register the UID as a topic:
FirebaseMessaging.getInstance()
.subscribeToTopic(appUser.getUid());
Now, when sending the notification, you can use this topic to send the notif. to the particular user.
Tokens keep on refreshing at instances, topics defined by you will not change. Every device for which the topic is defined will be notified.
NOTE:
Registering too many topics will raise
messaging/too-many-topics error. Details here. Hence, token
registration in the preferred way.
Fetching and keeping track of tokens:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onNewToken(String s) {
super.onNewToken(s);
// Save the new token here in a place from where you want to fetch it and send notification
Log.e("NEW_TOKEN",s);
}
}
The answer is No. The FCM tokens are device generated tokens which is generated when,
App is installed
App Data is cleared
and UID is a unique identification generated from a particular user account. Both are not related to each other.
As per your problem, you need to store your tokens wrt to UID and then use it to send notification. Or you can use subscribe tokens for seperate user group. Thats the only option I see.
I want to implement chat between two users in mobile, I'm using android studio and store the messages in AWS(amazon) MySQL database.
I manage to read messages every time user starts the chat activity like so:
void onCreate() {
readHistoryMessagesFromLocalDataBase();
readMessagesFromGlobalDataBase();
}
The first function is used to read the history messages from the local database of Android using SQLite.
The second function is used to read any new messages waiting on the global database of Amazon using MySQL.
This works perfectly, but how can I receive messages in real time while I'm already in the activity without reloading or refreshing?
Is there any data change listener to achieve this goal?
Really hoping someone may be able to point me in the right direction for this one. We have an app that uses Realm to manage our data locally on the device.
Through general use there will be data inputted to the device and then this will be attempted to be sent to the live service we have on our api. However due to the location of the users we cannot be sure if this has been uploaded before we re-download the data the next time the app is opened.
We need it to work just like Git in a way. You can't pull without first committing your changes. But instead of committing we are pushing up.
We believe the live data should take priority here so if there is a change on live it should get pulled down, but if there is something null on live but we have it locally then we should change that.
Is there something we can use to code this functionality, can't believe we are the first to face this issue. Either that or a flow for how we should get the data before the user can start working.
An example:
We are looking to save the imei number that the user enters. This will save to the realm db at the time and attempt to update the live database. If this is not possible when the app is restarted and the imei numbers are pulled we don't what that to be possible until the data we have locally that is missing on live to be pushed.
Imei.java (Realm model)
public class Imei extends RealmObject {
#PrimaryKey
public int id;
public int deviceId;
public String imei;
}
When we get a response on the fetch:
#Override
public void onResponse(JSONObject response) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
ImeiResponse imeiResponse = gson.fromJson(response.toString(), ImeiResponse.class);
for (Imei imei : imeiResponse.imeis()) {
addToDB(imei);
}
}
And then we use addToDb, which is where I would imagine that some kind of check would take place as if we were able to pull then we have a good enough connection to push the data we have locally
public void addToDB(Imei imei)
{
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(imei);
realm.commitTransaction();
}
Just in case someone stumbles across this question we did eventually find a solution.
Any time we need to save data we are adding it into a request queue table and then attempting to run each of the requests. If they pass they are removed, if they fail they are deleted.
Every time we then go to pull data we check that requests queue first and run if there is anything in there, if they continue to fail you then don't pull.
This seemed the best solution for us. Hope it helps
I am developing an Android app which takes the current location of the user and displays a list of restaurants close to his/her location. The restaurants' data is available to me (i.e I do have the lat/long of each restaurant I want to display in the search results). I can't use Google Places API, because I need to show only those restaurants that are available in our database(in our website). My question is how do I access my database(or even an URL),which is on a computer, to extract the restaurants' data and display as search results in my android app?
I am actually making a Seamless ( http://bit.ly/Jp7pUN ) type application for my company.
I am a complete newbie to android app development. So, pardon me if this is really a very broad or a stupid question. Please just tell me what topics I need to study to implement this. I would study and do it myself.
Thanks.
You will need:
a Sqlite database to store the restaurants and their longitude/latitude
a MapView to display the map (Don't forget to register your Google Maps API key)
a map overlay to show the markers on the map
GPS access to get the user's location (needs the appropriate Android permission)
a simple search algorithm that retrieves a result set of restaurants within x distance of the user's location
EDIT
If your database is stored on a server, you will need a way to query the server, preferably using an HTTP-based protocol such as REST. It is useful (but not required) to cache the restaurant locations on the Android device (using Sqlite), in case the user is offline (The good news: Since you can use Java both on Android and the server, 90% of your data access layer you will only need to write once).
For the data transfer from server to the Android client, JSON is a popular format.
To acces database on your computer (not SQLite on Android) you should use url for your database server changing localhost to: 10.0.2.2. But in case your database will be on the Internet - you should create maybe some REST API to get the data you need. Then use HttpClient to fetch the data from server.
Everything that you need is in Developer Guide: MapView
And for retrieving current location I advice using MyLocationOverlay
For example (url to server):
//public static final String SERVER_ADDRESS = "http://10.0.2.2:3000"; // for localhost server
public static final String SERVER_ADDRESS = "http://railsserver.herokuapp.com"; //for remote server
Accessing data on your server - this depends on that how you implement (and using what thechnology) your server (REST API?, WebService?, Plain HTML?) and what will be the format of the response from server (JSON? XML?, etc.)
I suggest using JSON because it is easy to parse using included classes in Android SDK:
String json = execute(new HttpGet(Constants.SERVER_URL + "/fetchData"));
JSONObject responseJSON = new JSONObject(json);
if(responseJSON.has("auth_error")) {
throw new IOException("fetchData_error");
}