【发布时间】:2021-03-26 22:40:36
【问题描述】:
我使用 Firebase 在我的应用中实现了通知。我可以从 Firebase 向我的手机发送通知,但此通知不会显示为弹出窗口,只有从我的 pohone 顶部向下滚动以显示通知时才能看到它。我做错了什么?
这里是通知渠道
// Default notifications channel
String defaultChannelID = getString(R.string.notif_channel_default_ID);
String defaultChannelName = getString(R.string.notif_channel_default_name);
String defaultChannelDescription = getString(R.string.notif_channel_default_desctription);
NotificationChannel defaultChannel = new NotificationChannel(defaultChannelID, defaultChannelName, NotificationManager.IMPORTANCE_HIGH);
defaultChannel.setDescription(defaultChannelDescription);
// Alarms notifications channel
String alarmChannelID = getString(R.string.notif_channel_alarms_ID);
String alarmChannelName = getString(R.string.notif_channel_alarms_name);
String alarmChannelDescription = getString(R.string.notif_channel_alarms_desctription);
NotificationChannel alarmChannel = new NotificationChannel(alarmChannelID, alarmChannelName, NotificationManager.IMPORTANCE_HIGH);
alarmChannel.setDescription(alarmChannelDescription);
// Create channels
NotificationManager notificationManager = getSystemService(NotificationManager.class);
List<NotificationChannel> notificationChannels = Arrays.asList(defaultChannel, alarmChannel);
notificationManager.createNotificationChannels(notificationChannels);
这里是 FCM 服务
import android.app.NotificationManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public MyFirebaseMessagingService() {
}
@Override
public void onNewToken(@NonNull String s) {
Log.e("Token", s);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Log.e("Notificacion recibida: ", remoteMessage.getData().toString());
if (remoteMessage.getData().size() > 0) {
displayNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
private void displayNotification(String title, String body){
String CHANNEL_ID = getString(R.string.notif_channel_alarms_ID);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_home)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
}
}
在其他 Stackoverflow 帖子中,他们说问题在于我没有将通知重要性设置为 HIGH,但是,正如您所见,我写了那个并且通知没有显示为弹出窗口。
感谢您的帮助
【问题讨论】:
标签: android firebase notifications firebase-cloud-messaging popup