【问题标题】:Firebase Notification white circle issue [duplicate]Firebase通知白圈问题[重复]
【发布时间】:2016-10-26 17:39:05
【问题描述】:

我对@9​​87654329@ 有疑问,如果我在应用程序在屏幕上运行时发送通知,通知会正确显示如下:

如果我在应用程序在后台运行时发送通知,通知显示如下:

这是我的FirebaseMessagingService 课程

    public class AppFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage);
    }

    private void sendNotification(RemoteMessage remoteMessage) {
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_video_label_white_24dp: R.drawable.ic_launcher;
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setWhen(System.currentTimeMillis());
        notificationBuilder.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
        notificationBuilder.setSmallIcon(icon);
        notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Intent resultIntent = new Intent(this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(pendingIntent);
            notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        }

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

为什么会这样?

【问题讨论】:

    标签: java android firebase firebase-cloud-messaging


    【解决方案1】:

    这是 Firebase 中尚未解决的错误。链接在这里:https://stackoverflow.com/a/37332514/1507602

    另一种选择是不使用 Firebase 控制台发送通知,而是使用 POST API,这样您的通知将直接发送到onMessageReceived(),您可以在其中创建自己的通知。

    要发送数据负载消息,您必须发出 curl 请求:

    HTTP POST 请求

    https://fcm.googleapis.com/fcm/send
    Content-Type:application/json
    Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
    
    { "data": {
        "score": "5x1",
        "time": "15:10"
      },
      "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
    }
    

    您可以从 firebase 控制台获取服务器密钥 (AIzaSyZ-1u...0GBYzPu7Udno5aA):您的项目 -> 设置 -> 项目设置 -> 云消息传递 -> 服务器密钥

    【讨论】:

    • 感谢您的回答,但我可以通过一个实例向所有用户发送消息?
    • 是的,您可以 - 只需为此使用主题消息传递
    • 谢谢大家,还是希望尽快解决这个bug
    【解决方案2】:

    发生这种情况的原因是 Firebase 通知的工作方式,以及应用在前台和后台时不同的行为:

    1. 前景 - 这里是兼容性,与过去的内容和 GCM - 您完全控制并且您的 AppFirebaseMessagingService 代码已执行
    2. 背景 - 在这种情况下,系统/库负责显示消息,并用于此应用程序的主图标。如果您在数据节点中有任何数据,则将其作为意图传递给您的应用的主要活动(在用户按下通知后)

    因此 - 我决定在我的应用程序中不使用它: 1.我无法控制 2.使用应用程序的默认图标(在我的情况下也看起来像你的 - 圆形 3. 我不能用这个做额外的事情(在我的情况下,我还展示了图像+添加操作,以及 Firebase 通知 - 我 当应用程序在后台时,无法做到这一点)

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多