Reputation: 11
I want to send token code when app is opened. I don't want to send new token, only same token. This code work fine but only for first time.
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);
Log.d("TOKEN", token);
}
private void registerToken(String token){
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token", token)
.build();
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}}
I know onTokenRefresh
only start when app installed. How can I send token to server when I open my app first time, second time, 3. 4. etc.
I tryed to add this code to my activity but app not run. give me an error
Unfortunately (app name) has stopped.
I have added this code to main activity in onCreate
String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);
and after this code
private void registerToken(String token){
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token", token)
.build();
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}}
Sory for my bad English.
Edit: my MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
my FirebaseInstanceIDService.java
public class FirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);
Log.d("TOKEN", token);
}
private void registerToken(String token){
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("Token", token)
.build();
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Edit 2: I think this code not work in mainactivity.java
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
my app crashed when I add this code to mainactivity.
Full code is
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);
Log.d("TOKEN", token);
}
private void registerToken(String token){
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token", token)
.build();
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
How can I send data with AsyncTask?
Upvotes: 1
Views: 989
Reputation: 599131
You can get the token at any moment by calling:
FirebaseInstanceId.getInstance().getToken();
So you can put this in your MainActivity.onCreate()
and send it.
public void onCreate(Context ctx) {
...
String token = FirebaseInstanceId.getInstance().getToken();
if (token != null) {
registerToken(token);
}
...
}
private void registerToken(String token){
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token", token)
.build();
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}}
When you first start the application, the token won't have been generated yet by the time the activity starts. For that reason you need to check for null
and also implement onTokenRefresh
in the service.
Upvotes: 2
Reputation: 4258
try this,
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("myprefs", MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("token", token);
edit.commit();
}
}
now you can get the token inside your MainActivity onCreate method send to server
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("myprefs", MODE_PRIVATE);
String token = sharedpreferences.getString("token", "");
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("Token", token)
.build();
Request request = new Request.Builder()
.url("url")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
now everytime when token get changed via firebase it will automatically changed in shared preference too
Upvotes: 0
Reputation: 2872
I would suggest you to make a custom method in your FirebaseInstaceIdService like this:
public class FirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = FirebaseInstanceIDService.class.getSimpleName();
private static String fcmToken;
public static String getToken() {
fcmToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Old token: " + fcmToken);
return fcmToken;
}
}
Now in your onCreate of mainActivity call that method and retrieve the token on every app lauch like this:
FirebaseInstanceIdService.getToken();
Do let me know if this gives you an idea to achieve what you want to do.
Upvotes: 1