我在 android studio 中创建了一个 android 应用程序,并将其与 Firebase 连接。我知道它已连接,因为当应用程序打开时 onMessageRecieved 正在被调用,我可以在日志中看到它。
这里有类似的帖子,我尝试了我能找到的所有内容,包括:
将其添加到清单中:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README() for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher_background" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README() for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/white" />
在 MainActivity OnCreate() 和扩展 FirebaseMessagingService 的类中使用此 createNotificationChannel() 方法:
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"default_notification_channel_id",
"Channel name",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel description");
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}
下面我将添加我的 AndroidManifest 和扩展 FirebaseMessagingService 的类,老实说,我觉得我一直缺少一件明显的事情,但我已经尝试修复这个问题好几天了,但没有运气。如果我需要添加其他内容,请告诉我。
Android 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.TestApp"
android:usesCleartextTraffic="true"
tools:targetApi="31" >
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README() for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher_background" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README() for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/white" />
<service android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".RealSettingsActivity"
android:exported="false"
android:label="@string/title_activity_real_settings"
android:theme="@style/Theme.TestApp.NoActionBar" >
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".WebviewActivity"
android:exported="false" >
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".SettingsActivity"
android:exported="false"
android:label="@string/title_activity_settings" >
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
我的Firebase消息服务:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "Push-Event";
public static final String CHANNEL_ID = String.valueOf(R.string.default_notification_channel_id);
public static final String CHANNEL_NAME = "com.example.androidappfortesting";
@Override
public void onNewToken(@NonNull String token) {
Log.d(TAG, "Refreshed token: " + token);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a Notification payload.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String description = remoteMessage.getNotification().getBody();
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Description: " + description);
createNotificationChannel();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"default_notification_channel_id",
"Channel name",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel description");
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}
}
最有可能的问题是您使用
NotificationChannel
创建了 default_notification_channel_id
,而在 NotificationCompat.Builder
中您使用了 channel_id
。
所以尝试更换它:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
对于:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default_notification_channel_id")