我的問題是我在我的 android 應用程式客戶端中沒有收到 firebase 云訊息,這是我從 Spring MVC 應用程式發送的。這是 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);
我的 pom.xml:
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>7.3.0</version>
</dependency>
當我在 Spring MVC 專案中運行單元測驗時,我得到了結果:
result:projects/heliosemenu-9eba9/messages/6047526430479807699
這是我的 android 應用程式客戶端代碼: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());
}
}
}
在 app 檔案夾中添加了 google-services.json。構建.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
}
應用程式/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'
}
在 android 應用程式的 logcat 中,我從未收到來自 firebase 的任何訊息!我不知道我在這里錯過了什么。
更新 1:該應用程式處于前臺并正在運行,我沒有收到任何來自 firebase 的資訊。
uj5u.com熱心網友回復:
收到訊息后,您必須顯示通知。onMessageReceived()因此,在in 中呼叫以下函式MyFirebaseInstanceIDService.java。但首先,請確保您從服務器獲得回應。
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());
}
uj5u.com熱心網友回復:
我從 firebase 控制臺重新檢查組態檔,清理構建和重建并最終作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485912.html
標籤:爪哇 安卓 春天 弹簧MVC firebase-云消息传递
上一篇:將XML與Java相互轉換
