Firebase无法接收来自PHP的通知 [英] Firebase fail receiving notification from php

查看:43
本文介绍了Firebase无法接收来自PHP的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从Firebase控制台成功接收通知,但是当我调用PHP函数来执行此操作时,什么也不会接收.正如我在互联网上看到的那样,这比我发送这些通知的方式少得多.而且我不明白当我切换到Firebase控制台时有什么不同(因为它可以从那里运行).我不认为问题出在我的应用程序上,因为我将请求发送到他们的服务器,然后他们应该发送到我的设备.很可能会犯错误,请不要太粗鲁.我还是一个初学者.谢谢您的明智回答!

I can succesfully recive notifications from Firebase console, but when I call the PHP function to do it, doesn't recive anything. As I saw on internet, this is more less how I can send these notifications. And what I don't understand what is different when I switch to Firebase console (because it works from there). I don't think the problem is from my app, because I send a request to their server and then they should send to my device. It is very possible to make a mistake, please don't be too rude. I am still a beginner. Thank you for your wise answers!

PHP函数

function push_notification_android($device_id,$message){

    //API URL of FCM
    $url = 'https://fcm.googleapis.com/fcm/send';


    $api_key = 'MY_KEY';
                
    $fields = array (
        'to' => $device_id,
        'data' => array(
                "message" => $message,
                "id" => '1',
        ),
    );

    //header includes Content type and api key
    $headers = array(
        'Content-Type:application/json',
        'Authorization: key='.$api_key
    );
                
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);

    curl_close($ch);
    echo $result;
    return $result;
}

{"multicast_id":7370520341381062896,成功":1,失败":0,"canonical_ids":0,结果":[{"message_id" ::" 0:1611404485967655%6b34551f6b34551f"}]}

{"multicast_id":7370520341381062896,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1611404485967655%6b34551f6b34551f"}]}

推荐答案

@adnandann的答案的确说明了原因:您正在发送数据消息,而Firebase控制台始终会发送通知消息(该消息会由操作自动显示系统).

@adnandann's answer explains the reason indeed: you're sending a data message, while the Firebase console always sends a notification message (which is automatically displayed by the operation system).

因此,您有两种选择:

  1. 自己显示数据消息, @adnandann的答案显示了如何做.

从您的PHP代码发送一条通知消息,您可以执行以下操作:

Send a notification message from your PHP code, which you can do with:

$fields = array (
    'to' => $device_id,
    'notification' => array(
      "title" => "New message from app",
      "body" => $message
    ),
);

这篇关于Firebase无法接收来自PHP的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆