Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用firebase admin时设置FCM高优先级_Firebase_Firebase Cloud Messaging - Fatal编程技术网

使用firebase admin时设置FCM高优先级

使用firebase admin时设置FCM高优先级,firebase,firebase-cloud-messaging,Firebase,Firebase Cloud Messaging,我有以下代码,它使用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 { res

我有以下代码,它使用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);
消息消息=null;
message=message.builder().putData(“From”,fromTel)。putData(“To”,toTel)。putData(“Text”,Text)
.setToken(registrationToken).build();
字符串响应=null;
试一试{
response=FirebaseMessaging.getInstance().sendAsync(message.get();
responseEntity=新的responseEntity(HttpStatus.ACCEPTED);
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}
System.out.println(“成功发送消息:+响应”);
上面的代码运行良好。但我需要发送“高优先级”消息,以便设备可以在打瞌睡模式下接收它们


如何使消息具有“高优先级”?

对于发送到Android设备的消息,在构建消息时,将其设置为具有以下内容的值:

AndroidConfig config=AndroidConfig.builder()
.setPriority(AndroidConfig.Priority.HIGH).build();
Message=null;
message=message.builder()
.putData(“From”,fromTel)。putData(“To”,toTel)。putData(“Text”,Text)

.setAndroidConfig(config)/这可能会帮助某些人

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);
    }

没有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);
        });
}

谢谢你的回答。高优先级正在工作,但如果我有一段时间不触摸手机,并且当我向其发送Firebase cloud消息时,手机会花费很长时间来处理它。当手机处于睡眠状态时,我是否应该使用Firebase作业调度器才能让我的应用程序工作?当手机处于唤醒状态时,手机会在几秒钟内处理所有信息,并发送高优先级的信息,我预计对于处于唤醒状态和睡眠状态的设备,信息接收延迟大致相同。你在测试什么型号的手机?我看到过不同设备型号的不同行为报告。它在Moto G4 Play上运行。您可以尝试从Firebase控制台发送通知。默认情况下是高优先级的,可以让您了解不同设备状态的接收延迟。我做了一些调试,现在在收到Firebase消息时,我会在应用程序屏幕上打印一些带有时间戳的内容。消息确实会出现,但不会继续执行作业的其余部分。它应该创建一个类的新实例,然后创建一个新线程并下载一个图像。这是收到Firebase消息后将使用的类。我不确定自己是否做错了。请对您的答案提供更多解释,例如,将其放在问题的上下文中。
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);
    }
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);
        });
}