【问题标题】:set FCM high-priority when using firebase-admin使用 firebase-admin 时设置 FCM 高优先级
【发布时间】:2018-09-11 02:41:40
【问题描述】:

我有以下代码,它使用 firebase-admin 使用 Firebase 云消息发送消息

Message message = null;
message = Message.builder().putData("From", fromTel).putData("To", toTel).putData("Text", text)
            .setToken(registrationToken).build();

String response = null;
try {
    response = FirebaseMessaging.getInstance().sendAsync(message).get();
    responseEntity = new ResponseEntity<String>(HttpStatus.ACCEPTED);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
System.out.println("Successfully sent message: " + response);

上面的代码工作正常。但我需要发送“高优先级”消息,以便设备可以在打盹模式下接收它们。

我怎样才能使消息“高优先级”?

【问题讨论】:

    标签: firebase firebase-cloud-messaging


    【解决方案1】:

    为了发送到 Android 设备,在构建消息时,将其 AndroidConfig 设置为具有 Priority.HIGH 的值:

    AndroidConfig config = AndroidConfig.builder()
            .setPriority(AndroidConfig.Priority.HIGH).build();
    
    Message message = null;
    message = Message.builder()
            .putData("From", fromTel).putData("To", toTel).putData("Text", text)
            .setAndroidConfig(config) // <= ADDED
            .setToken(registrationToken).build();
    

    有关更多详细信息,请参阅example in the documentation

    发送到 Apple 设备时,请使用setApnsConfig(),如the documentation 中所述。

    【讨论】:

    • 感谢您的回答。高优先级正在工作,但如果我有一段时间不触摸我的手机,当我向它发送 Firebase 云消息时,手机需要很长时间才能处理它。我是否应该使用 Firebase Job Dispatcher 以便我的应用程序在手机处于睡眠状态时工作?当手机处于唤醒状态时,手机会在几秒钟内处理完所有事情
    • 对于高优先级消息,我希望消息接收延迟对于唤醒和睡眠设备大致相同。你测试的是什么型号的手机?我看到了根据设备型号不同行为的报告。
    • 它在 Moto G4 Play 上运行
    • 您可以尝试从 Firebase 控制台发送通知。默认情况下是高优先级,可以让您了解不同设备状态的接收延迟。
    • 我做了一些调试,现在我在应用程序屏幕上打印一些带有时间戳的东西,当收到 Firebase 消息时。该消息确实出现了,但它不会继续完成其余的工作。它应该创建一个类的新实例,然后创建一个新线程并下载图像。这是收到 Firebase 消息后将使用的类 pastebin.com/RUrZwb4L 我不确定我是否做错了什么
    【解决方案2】:

    这可能对某人有所帮助。

    public String sendFcmNotification(PushNotificationRequestDto notifyRequest) throws FirebaseMessagingException {
            String registrationToken = notifyRequest.getToken();
    
            AndroidConfig config = AndroidConfig.builder()
                    .setPriority(AndroidConfig.Priority.HIGH).build();
    
            Notification notification = Notification.builder()
                    .setTitle(notifyRequest.getTitle())
                    .setBody(notifyRequest.getBody())
                    .build();
    
            Message message = Message.builder()
                    .setNotification(notification)
    //                .putData("foo", "bar")
                    .setAndroidConfig(config)
                    .setToken(registrationToken)
                    .build();
    
    
            return FirebaseMessaging.getInstance().send(message);
        }
    

    【讨论】:

    • 工作得很好!谢谢。
    【解决方案3】:

    没有 AndroidConfig 构建器

    function sendFCM(token, from, to, text) {
        var admin = require("firebase-admin");
        var data = {
            from: from,
            to: to,
            text: text
        };
        let message = {       
            data: data,
            token: token,
            android: {
                priority: "high",  // Here goes priority
                ttl: 10 * 60 * 1000, // Time to live
            }
        };
        admin.messaging()
            .send(message)
            .then((response) => {
                // Do something with response
            }).catch((error) => {
                console.log(error);
            });
    }
    

    【讨论】:

      【解决方案4】:
      public async Task send_PushNotification(FirebaseAdmin.Messaging.Message MESSAGE)
          {   
              var defaultApp = FirebaseApp.Create(new AppOptions()
              {
                  Credential = GoogleCredential.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "key_FB.json")),
              });
      
              var message = MESSAGE;
              message.Token = FB_TOKEN;
              message.Android = new AndroidConfig();
              message.Android.Priority = Priority.High;
              message.Android.TimeToLive = new TimeSpan(0,0,5);
             
              var messaging = FirebaseMessaging.DefaultInstance;
              var result = await messaging.SendAsync(message);
              Console.WriteLine(result);
          }
      

      【讨论】:

      • 请对您的回答提供更多解释,例如把它放在一个问题的上下文中。
      猜你喜欢
      • 1970-01-01
      • 2016-07-08
      • 2015-02-17
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多