Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
I am trying to send a POST request to google's FCM server , so that I can Subscribe to a particular Topic and send/Receive messages.

Below I have also used RxJava3 to set the Call Adapter for my Retrofit. I am new to these functions , I think I am missing something here. I am not sure should I change the return types for my API or Change the Call Adapters. Any Help is Appreciated.

EXCEPTION IN LOGCAT : Unable to create call adapter for retrofit2.Response<okhttp3.responsebody> for method NotificationAPI.postNotification


BUILD.GRADLE(APP)

    plugins {
    id 'com.android.application'
}
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    defaultConfig {
        applicationId "com.deepesh.myfcmapp"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation platform('com.google.firebase:firebase-bom:26.0.0')
    implementation 'com.google.firebase:firebase-messaging:21.0.0'

    // retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
}
apply plugin: 'com.google.gms.google-services'



MainActivity.java


package com.deepesh.myfcmapp;

import androidx.appcompat.app.AppCompatActivity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.gson.Gson;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends AppCompatActivity {

    public static final String FCM_CHANNEL_ID = "fcm_channel_id";
    private static final String TAG = "TAG";
    EditText title_editText, message_editText, token_editText;
    Button send_button;
    Retrofit retrofit1;
    NotificationAPI notificationAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseMessaging.getInstance().subscribeToTopic(MyConstants.TOPIC);

        init();

        // CREATE NOTIFICATION CHANNEL
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel fcm_channel = new NotificationChannel(FCM_CHANNEL_ID, "FCM channel", NotificationManager.IMPORTANCE_HIGH);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(fcm_channel);
        }


    }


    void init() {
        title_editText = findViewById(R.id.title_editText);
        token_editText = findViewById(R.id.token_editText);
        message_editText = findViewById(R.id.message_editText);
        send_button = findViewById(R.id.send_button);
        send_button.setOnClickListener(clickListener);
    }


    void sendNotififcation(PushNotification pushnotification) {

        retrofit1 = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(MyConstants.BASE_URL)



NotificationService.java


package com.deepesh.myfcmapp;

   import android.app.Notification;
   import android.app.NotificationManager;
   import android.app.PendingIntent;
   import android.content.Intent;
   import androidx.annotation.NonNull;
   import androidx.core.app.NotificationCompat;
   import com.google.firebase.messaging.FirebaseMessagingService;
   import com.google.firebase.messaging.RemoteMessage;
   import java.util.Random;

   public class NotificationService extends FirebaseMessagingService {


       @Override
       public void onMessageReceived(@NonNull RemoteMessage message) {
           super.onMessageReceived(message);

           Intent intent = new Intent(this,MainActivity.class);
           int NotififcationID = new Random().nextInt();
           PendingIntent pendingIntent = PendingIntent.getActivity(this,0 , intent,PendingIntent.FLAG_ONE_SHOT);
           NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

           Notification notification = new NotificationCompat.Builder(this, MainActivity.FCM_CHANNEL_ID)
                   .setContentTitle(message.getData().get("title"))
                   .setContentText(message.getData().get("message"))
                   .setSmallIcon(R.drawable.ic_baseline_chat_24)
                   .setAutoCancel(true)
                   .setContentIntent(pendingIntent)
                   .build();
           manager.notify(NotififcationID,notification);

       }


       @Override
       public void onNewToken(@NonNull String s) {
           super.onNewToken(s);
       }

   }




NotificationData.java

package com.deepesh.myfcmapp;

public class NotififcationData {

    String title;
    String message;

    public NotififcationData(String title, String message) {
        this.title = title;
        this.message = message;
    }
    
}



PushNotification.java


package com.deepesh.myfcmapp;

public class PushNotification {

// variable names should be same as in the formats

    String to;
    NotififcationData data;

    public PushNotification(String to, NotififcationData data) {
        this.to = to;
        this.data = data;
    }
}




NotificationAPI.java


package com.deepesh.myfcmapp;

import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface NotificationAPI {


    @Headers({"Authorization: key="+ MyConstants.SERVER_KEY,"Content-Type:"+ MyConstants.CONTENT_TYPE} )
    @POST("fcm/send")
    Response<ResponseBody> postNotification(@Body PushNotification pushNotification);

}


What I have tried:

I tried importing some Call adapters like RXJAVA2 , 3 , but they still give the sa,e result.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900