【问题标题】:Why notifications doesn't show as popup?为什么通知不显示为弹出窗口?
【发布时间】: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


    【解决方案1】:

    您尝试实现的是所谓的“提醒”通知。不幸的是,即使您符合向用户显示此类通知所需的条件,Android 系统通常也会自行决定它是否从顶部向用户显示“弹出窗口”。

    当您将各自的应用程序置于前台时,可能不会显示提示通知。

    如果您已经符合以下文档中提到的所有条件,那么您无能为力:

    https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up

    注意:如果您最初以低优先级创建通知通道,然后将其更新为较高优先级,则有助于重新安装您的应用程序。

    【讨论】:

    • 您好,感谢您的回答。我在我的应用程序的设置中发现(我在我的设备中去了这里:设置-> 应用程序-> -> 通知)通知通道默认禁用声音和弹出权限。当我打开它时,所有通知都会发出声音并显示为弹出窗口。有没有办法默认打开这个设置? @J。黑格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    相关资源
    最近更新 更多