My problem is that I don't receive the firebase cloud message in my android app client, which I send from my Spring MVC app. Here is the code in the Spring App Test.
GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new ClassPathResource("my-config.json").getInputStream());
FirebaseOptions firebaseOptions = FirebaseOptions
.builder()
.setCredentials(googleCredentials)
.build();
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "my-app");
FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance(app);
Notification notification = Notification
.builder()
.setTitle("My title")
.setBody("My body")
.build();
Message message = Message
.builder()
.setTopic("test1")
.build();
String result = firebaseMessaging.send(message);
System.out.println("result:" result);
My pom.xml:
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>7.3.0</version>
</dependency>
When I ran the Unit test in the Spring MVC Project I am getting the result:
result:projects/heliosemenu-9eba9/messages/6047526430479807699
And here is my android app client code: AndroidManifest.xml
<service
android:name="com.example.heliosfirebasereceiver.MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(getApplicationContext());
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
FirebaseMessaging.getInstance().subscribeToTopic("HeliosEMenuTest").
addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
Log.i(TAG,task.isSuccessful() "|");
}
});
}
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseInstanceIDService";
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " token);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
Log.d(TAG, "From: " remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " remoteMessage.getNotification().getBody());
}
}
}
Added the google-services.json in the app folder. build.gradle:
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app/build.gradle:
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.heliosfirebasereceiver"
minSdk 26
targetSdk 32
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.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-messaging:23.0.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
In the logcat of the android app I never received any Message from the firebase! I don't know what I am missing here.
UPDATE 1: The app is in the foreground and running, and I don't receiving nothing from the firebase.
CodePudding user response:
You have to show a Notification once the message is received. So, call this below function in onMessageReceived()
in MyFirebaseInstanceIDService.java
. But first, make sure you are getting a response from the server.
private void showNotification(@NonNull RemoteMessage remoteMessage) {
String channelId = "fcm_default_channel";
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(remoteMessage.getNotification().getBody();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
CodePudding user response:
I recheck the config files from the firebase console, clean build and rebuild and finally worked.