not sure about GCM implention - android

My website api send a gcm notification to user in case of he received a normal texting message from another user.
Google docs explain the implention steps like i have been coding android for 100 years, but in fact im a nope lvl5 so i didnt understand it.
In addition all web tutorial explain the old way,
thus what i did is:
1-created a new app on Google console and downloaded that service.json file and add the menifest and gradle things, no errors now all good,
2-then i went to official Github gcm project and copied every thing into my project without any changes except that the Mainactivity content of Github project i added it to the login activity of my project,
still no error, but am i doing it right?.
The second question is:
when user login i should send the user devise id to my server along with username and passowrds params,
How to get that from shered prefenince.
Soory for the long story and thanks in advance.
This is the github project
https://github.com/googlesamples/google-services/tree/master/android/gcm

for the first time when an app is launched the app registers in gcm and recieves token and it is stored in sharedpreference with a keyword. look for the gcm registering in your launching activity. then use tis key to retrive the token from sharedpreference in login activity
goto this url
https://github.com/googlesamples/google-services/blob/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart/RegistrationIntentService.java
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
you have to store value in token in sharedpreference
then retrieve it in login activity and send it to your server

Related

FCM: Invalid registration token. Check the token format

I’m implementing Firebase Cloud Messaging (FCM) and am experiencing a problem that I’m unable to solve. I have implemented FirebaseMessagingService and FirebaseInstanceIdService according to the guide(s). When I go to Firebase Console for my app, and use the Notification function, I can successfully send a message to ALL my app instances (using the package name).
Now, in the code I have fetched the Firebase Instance Id (token) by use of the following code:
String token = FirebaseInstanceId.getInstance().getToken();
SendFirebaseTokenToServer(token);
(note that currently I’m using HTTP protocol, as my server does not yet have a cert). Anyway using the token I get from the call above, I go back to the Firebase Console and try to send a message to one (1) installed instance of my app. I grab the token from our server DB where it is stored as "varchar(max)". When I do that I get the following error message:
Invalid registration token. Check the token format.
I have googled that and found only one hit (having to do with Firebase and iOS):
http://stackoverflow.com/questions/41343520/ios-invalid-registration-token-check-the-token-format
That issue indicates that a cert was required (I think I’m reading it correctly). I’m not sure what I’m doing wrong. I need to get this to work using the Firebase Console first, then my server guy can start on his end knowing that it should work.
Turns out i was programatically encoding all POST or PUT parameters prior to sending to our server. the FCM token had a semicolon in it, which got encoded to a "%3A", seemingly causing the problem.
do NOT encode the FCM token.

Notifications using firebase for android studio

I'm making an app that requires to book people for a specific purpose. I would like to notify the person that he is being booked by someone. I can't figure out how this is done.
Examples of sending notifications from the server to everyone who has the app is available but that is not what i want.
Once someone clicks the book button from his account, the other person should get a notification. Please help!
You have to follow below steps for achieve this same.
Step 1: Need to store token from "FirebaseInstanceId" when user signup or signin and also store that token on web server.
Step 2: Need web-service where we can send that "Book" button selected user userId through web-service. After that web-service working on that request, find TOKEN from server of that same user and set notification payload with
{ to: "token", ... }
and then send notification with some intial details using FCM.
Step 1 : Create account in FCM
Step 2 : Create app in FCM Console
step 3 : Get Token id of device and send to server.

The active or pending auth credentials were superseded by another call to auth

I am developping an android app based on firebase that connects to the Nest API. While it worked for some time, it now rejects authentification.
I get the folowing error "The active or pending auth credentials were superseded by another call to auth" when I'm pretty sure firebaseRef.auth() method is only called once...
EDIT : I'm getting close to the 1000 Users limitation after wich we should request more users from Nest.
Any idea where this is comming from ? Can it be comming from the online service I am calling or is this necessarilly due to my code ?
thanks.
P.
We discovered that every time the user is passed through the Works with Nest web sign up dialog, it generates a new auth token. Is it possible you've run through the process on multiple devices and generated a new auth token, invalidating your previous one?

getting Google oauth authorization token from Android- return with invalid_scope/ Unknown error

I try to use Google oauth to authenticate users on my android app.
Then I would like to send it to my app server so it can connect at any time with Google calendar.
I tried to use
GoogleAuthUtil.getToken(getApplicationContext(), mAccountName, mScope);
Following this article:
https://developers.google.com/accounts/docs/CrossClientAuth
When I use it with scope
mScope = "oauth2:https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
I get a token, which is valid for an hour
But when I try to get an authorization code (so I can get a refresh token that is valid for longer time, using
mScope2 ="oauth2:server:client_id:{CLIENT_ID}.apps.googleusercontent.com"+ ":api_scope:https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
I receive either "invalid_scope" or "Unknown" exceptions.
What am I doing wrong?
EDIT:
OK, After creating a new app on google API console and adding plus.login to the scope I get a code, but for some reason my server can't resolve this token. When tying to resolve server gets an error about the redirection URL.
BTW, When I do the web flow with same parameters it works.
OK, found the solution, I expected Google to have a lot better documentation about working with Google Oauth and Android. A few things you have to know to work with Android and offline token
When you create google Client ID Don't create a service application before you create a web application
Must include https://www.googleapis.com/auth/plus.login in your scope
The weirdest, to resolve the one time authorization code on my server, I had to use the redirection URL from the Android client ID details (which doesn't even look like a url) and not from the Web client details on Google API console.
That scope string is only documented to work when passed to GoogleAuthUtil(), see http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html, on Android. But it would be cool if it worked on iOS too; our infrastructure there is a little behind where we’re at on Android.
I have had the same issue then i realised that my app is not published and is in debug mode, so i had to add test users to the Google project -> Consent Screen, then i was able to fetch the token for the added test user.
You just need to follow the correct steps/format for specifying the scopes. Find them here https://developers.google.com/android/guides/http-auth#SpecifyingScopes

Android - How to send registration ID to server once user signs up or logins without restarting App

I have an app with a webview. My site allows registration and login. If the user installs the app for the first time, the app will register the device for GCM. However, when they actually login to the site, the gcm_id is not sent to the server because I have it attached to the webView.load_url as a parameter which is only ran when you first start the app.
How do you reload or send the gcm_id without having to wait for them to run the app a 2nd time? I don't want it to be continuous either where it appends a url on every page load if I can help it. I've seen other examples but none have help my issue. BTW my server runs php if that matters.
I found a very nice class that will send http get requests including authentication if you need it via http or https with two lines of code.
EasyHttpClient
Very easy to use and you can use it like so.
EasyHttpClient client = new EasyHttpClient();
System.out.println(client.get("https://encrypted.google.com/"));
So what I came up with is when they first launch the app there will be a flag set when the device is initially registered. Then as long as that flag is set, it will periodically send a request using this http client to register it and if successfull it will null the flag and not try anymore. Since that flag is only set if the registration ID has not been created yet, it will never be set again unless they uninstall it of course. Seems good so far. I have to do this because until they login they are anonymous and my site can't register anonymous users. Just thought someone else might like to know as I found all kinds of tedious ways but that EasyHttpClient is the best I've found.
BTW The reason I did this is because what if they use the app once and get a message after that, the push notification wont work so I had to get it to register with my server the first time they use it.

Categories

Resources