Marcell Tanure
Lurker
I'm trying to connect posts from a Wordpress website using the Wordpress plugin and Firebase for cloud messaging. So whenever there's a new post in the website, the app users will receive a notification about it First, I need to send an API POST request with the device's token to the plugin. Ex:
URL structure: http://yoursite/pnfw/register/
Method: POST
Parameters: token (string): token given by APNs or FCM identifying the device, often called device ID (read here for devices without token, e.g. iOS simulator). os (string): operating system. It must be iOS or Android (case sensitive).
--Sample request for new registration: POST /pnfw/register/ HTTP/1.1 Host: yoursite Content-Length: 26 Content-Type: application/x-www-form-urlencoded
token=new_device_id&os=iOS
It is working when I test the API, but Android Studio keeps sending me this response:
2021-06-16 16:18:20.488 24664-24664/com.bc.brendan.soccernbandroidapp I/ResponseMsg: {"error":"500","reason":"Internal Server Error","detail":"Mandatory parameter os missing"}
Debug:
Request{method=POST, url=https://myWebSite.org/appdev/pnfw/register?token=fiCob....cdJmg2&os=Android, tags={class retrofit2.Invocation=com.bc.brendan.myApp.JsonPlaceholderAPI.createRegister() [fiCob...cdJmg2, Android]}}
Code for the Firebase class:
package com.bc.brendan.myApp;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.Query;
public class FirebasePush extends FirebaseMessagingService {
public FirebasePush() {
super();
}
private String newToken;
private JsonPlaceholderAPI jsonPlaceholderAPI;
@override
public void onMessageReceived(@androidx.annotation.NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
@override
public void onNewToken(String token) {
super.onNewToken(token);
Log.e("newToken", token);
newToken = token;
sendToken();
}
private void sendToken() {
Register register = new Register(newToken, "Android");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://myWebSite.org/appdev/pnfw/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceholderAPI = retrofit.create(JsonPlaceholderAPI.class);
Call<Register> call = jsonPlaceholderAPI.createRegister(register.getToken(), register.getOs());
call.enqueue(new Callback<Register>() {
@override
public void onResponse(Call<Register> call, Response<Register> response) {
try {
Log.i("ResponseMsg", response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@override
public void onFailure(Call<Register> call, Throwable t) {
Log.i("ErrorMsg", t.getMessage());
}
});
}
}
Json placeholder interface:
package com.bc.brendan.myApp;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface JsonPlaceholderAPI {
@GET("posts")
Call<List<Post>> getPosts();
@POST("register")
Call<Register> createRegister(@Query("token") String token,
@Query("os") String os);
}
Any help would be appreciated. I've been stuck in this issue for three days now.
URL structure: http://yoursite/pnfw/register/
Method: POST
Parameters: token (string): token given by APNs or FCM identifying the device, often called device ID (read here for devices without token, e.g. iOS simulator). os (string): operating system. It must be iOS or Android (case sensitive).
--Sample request for new registration: POST /pnfw/register/ HTTP/1.1 Host: yoursite Content-Length: 26 Content-Type: application/x-www-form-urlencoded
token=new_device_id&os=iOS
It is working when I test the API, but Android Studio keeps sending me this response:
2021-06-16 16:18:20.488 24664-24664/com.bc.brendan.soccernbandroidapp I/ResponseMsg: {"error":"500","reason":"Internal Server Error","detail":"Mandatory parameter os missing"}
Debug:
Request{method=POST, url=https://myWebSite.org/appdev/pnfw/register?token=fiCob....cdJmg2&os=Android, tags={class retrofit2.Invocation=com.bc.brendan.myApp.JsonPlaceholderAPI.createRegister() [fiCob...cdJmg2, Android]}}
Code for the Firebase class:
package com.bc.brendan.myApp;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.Query;
public class FirebasePush extends FirebaseMessagingService {
public FirebasePush() {
super();
}
private String newToken;
private JsonPlaceholderAPI jsonPlaceholderAPI;
@override
public void onMessageReceived(@androidx.annotation.NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
@override
public void onNewToken(String token) {
super.onNewToken(token);
Log.e("newToken", token);
newToken = token;
sendToken();
}
private void sendToken() {
Register register = new Register(newToken, "Android");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://myWebSite.org/appdev/pnfw/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceholderAPI = retrofit.create(JsonPlaceholderAPI.class);
Call<Register> call = jsonPlaceholderAPI.createRegister(register.getToken(), register.getOs());
call.enqueue(new Callback<Register>() {
@override
public void onResponse(Call<Register> call, Response<Register> response) {
try {
Log.i("ResponseMsg", response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@override
public void onFailure(Call<Register> call, Throwable t) {
Log.i("ErrorMsg", t.getMessage());
}
});
}
}
Json placeholder interface:
package com.bc.brendan.myApp;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface JsonPlaceholderAPI {
@GET("posts")
Call<List<Post>> getPosts();
@POST("register")
Call<Register> createRegister(@Query("token") String token,
@Query("os") String os);
}
Any help would be appreciated. I've been stuck in this issue for three days now.