此消息格式是否正确,可以通过Firebase Cloud Messaging Service发送?

时间:2019-01-22 11:41:18

标签: java firebase firebase-cloud-messaging

我们对服务器的后端进行了编辑,以在通知有效负载中包含自定义声音文件,并在FCM消息中包含数据有效负载,并且在消息中还包含优先级字段。进行这些更改后,推送通知对于我们的应用程序不再起作用。

在编辑后端之前,通知在iOS和Android上均能正常工作,但是通知有效负载中未指定声音,一旦单击通知,通知消息体就会丢失,如下图所示。

{
    "message": {
        "to": tokens,
        "notification": {
            "title": "Fergal SOS",
            "body": "Fergal Alarm: Fergal has issued an SOS alert at 12:00:00"
        }   
    }
}

此消息格式是使用以下Java代码创建的:

public static class Notification {
    @JsonProperty("body")
    private String body;
}

public static class Message {
    @JsonProperty("registration_ids")
    private String[] tokens;
    @JsonProperty("notification")
    private Notification notification;
}

public NotificatorFirebase() {
    key = Context.getConfig().getString("notificator.firebase.key");
}

@Override
public void sendSync(long userId, Event event, Position position) {
    final User user = Context.getPermissionsManager().getUser(userId);
    if (user.getAttributes().containsKey("notificationTokens")) {

        Notification notification = new Notification();
        notification.body = NotificationFormatter.formatShortMessage(userId, event, position).trim();

        Message message = new Message();
        message.tokens = user.getString("notificationTokens").split("[, ]");
        message.notification = notification;

        Context.getClient().target(URL).request()
                .header("Authorization", "key=" + key)
                .async().post(Entity.json(message), new InvocationCallback<Object>() {
            @Override
            public void completed(Object o) {
            }

            @Override
            public void failed(Throwable throwable) {
                LOGGER.warn("Firebase notification error", throwable);
            }
        });
    }
} 

编辑了后端以创建以下FCM消息格式:

{
    "message": {
        "to": tokens,
        "priority": "high",
        "notification": {
            "title": "Fergal SOS",
            "body": "Fergal Alarm: Fergal has issued an SOS alert at 12:00:00",
            "sound": "sos.wav",
        },
        "data": {
           "device-name": device-name,
           "alert-content": "Fergal Alarm: The device Fergal has issued an SOS alert at 12:00:00",
           "device-number": device-phone,
           "sound": "sos.wav",
           "location": device-coordinates
        }    
    }
}

这是使用以下Java代码完成的:

public void sendSync(long userId, Event event, Position position)
  {
    User user = Context.getPermissionsManager().getUser(userId);
    if (user.getAttributes().containsKey("notificationTokens")) {
      Device device = (Device)Context.getDeviceManager().getById(position.getDeviceId());

      Notification notification = new Notification();
      title = (device.getName() + " SOS");
      body = NotificationFormatter.formatShortMessage(userId, event, position).trim();
      sound = 
        ("sos".equals(event.getString("alarm")) ? "sos.wav" : "alert.wav");

      Data data = new Data();
      deviceName = device.getName();
      alertContent = body;
      deviceNumber = device.getPhone();
      sound = sound;
      location = (position.getLatitude() + " " + position.getLongitude());

      Message message = new Message();
      tokens = user.getString("notificationTokens").split("[, ]");
      priority = "high";
      notification = notification;
      data = data;

      Context.getClient().target("https://fcm.googleapis.com/fcm/send").request()
        .header("Authorization", "key=" + key)
        .async().post(Entity.json(message), new InvocationCallback()
        {
          public void completed(Object o) {}


          public void failed(Throwable throwable)
          {
            NotificatorFirebase.LOGGER.warn("Firebase notification error", throwable);
          }
        });
    }
  }

  public void sendAsync(long userId, Event event, Position position)
  {
    sendSync(userId, event, position);
  }

  public static class Message
  {
    @JsonProperty("registration_ids")
    private String[] tokens;
    @JsonProperty("priority")
    private String priority;
    @JsonProperty("notification")
    private NotificatorFirebase.Notification notification;
    @JsonProperty("data")
    private NotificatorFirebase.Data data;

    public Message() {}
  }

  public static class Data
  {
    @JsonProperty("device-name")
    private String deviceName;
    @JsonProperty("alert-content")
    private String alertContent;
    @JsonProperty("device-number")
    private String deviceNumber;
    @JsonProperty("sound")
    private String sound;
    @JsonProperty("location")
    private String location;

    public Data() {}
  }

  public static class Notification
  {
    @JsonProperty("title")
    private String title;
    @JsonProperty("body")
    private String body;
    @JsonProperty("sound")
    private String sound;

    public Notification() {}
  }
}

预期结果是将通知传递到设备并显示在通知托盘中,同时还播放自定义声音(我已将sos.wav文件添加到android和ios中platform目录下的android中的res / raw中我的cordova应用程序),点击通知后,它将打开该应用程序,然后显示存储在数据有效载荷中的信息。

发生了什么事,警报已在我们的服务器数据库中注册,但没有推送通知发送到用户的手机。

我目前正在终端中测试消息,并在完成后发布结果。

0 个答案:

没有答案