【问题标题】:not receiving foreground notification with FirebaseMessaging but working in background没有收到 FirebaseMessaging 的前台通知,但在后台工作
【发布时间】:2019-11-10 04:43:58
【问题描述】:

我添加了一个 FirebaseMessagingService 类来接收前台通知。 我没有收到前台通知。 当应用程序最小化时,它在后台运行良好。

我已经使用 firebase 功能设置了后台通知,它工作正常,现在我尝试在前台获取此通知,但通知没有出现。

我的 FirebaseMessagingService 类:

public class FirebaseMessaging extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getNotification() != null) {
            String notification_title = remoteMessage.getNotification().getTitle();
            String notification_message = remoteMessage.getNotification().getBody();

            Notification.Builder mBuilder = new Notification.Builder(this)
                    .setContentTitle(notification_title)
                    .setContentText(notification_message)
                    .setSmallIcon(R.drawable.default_avatar);

            int mNotificationId = (int) System.currentTimeMillis();

            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, mBuilder.build());
        }
    }
}

没有错误信息:

实际结果:通知永远不会到达前台。 预期结果:我希望在应用程序中时收到通知,而不仅仅是最小化时。

【问题讨论】:

    标签: java android firebase-cloud-messaging


    【解决方案1】:

    我将我的 FirebaseMEssagingService 类更改为它可以工作:

    public class FirebaseMessaging extends FirebaseMessagingService {
    private final String CHANNEL_ID = "personal_notifications";
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
        String notification_title = remoteMessage.getNotification().getTitle();
        String notification_message = remoteMessage.getNotification().getBody();
    
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
    
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this, CHANNEL_ID)
                            .setSmallIcon(R.drawable.default_avatar)
                            .setContentTitle(notification_title)
                            .setContentText(notification_message);
    
            notificationManager.createNotificationChannel(notificationChannel);
    
            int mNotificationId = (int) System.currentTimeMillis();
            mNotifyMgr.notify(mNotificationId, mBuilder.build());
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      相关资源
      最近更新 更多