【问题标题】:How to send push notification to 2 devices without creating topic or group using just the FCM tokens in Firebase?如何在不使用 Firebase 中的 FCM 令牌创建主题或组的情况下向 2 个设备发送推送通知?
【发布时间】:2016-12-09 05:47:23
【问题描述】:

我只需要将推送通知发送到少数设备。我将他们的设备令牌放在一个数组中。我需要为此创建主题或群组吗?

我可以使用 for 循环单独发送推送通知,但有更好的方法吗?

编辑:请不要在不理解的情况下将此问题标记为重复。问题

FCM (Firebase Cloud Messaging) Send to multiple devices

有使用 FCM 令牌和组的答案,我已明确表示这不是我的要求。至于其他问题提到的“registration_ids”,官方文档中没有提到它,如果提到,请指出它是一个有效的答案。

【问题讨论】:

标签: android firebase firebase-cloud-messaging


【解决方案1】:

使用“registration_ids”而不是“to”。

var message = { 
    registration_ids:['id1','id2','id3'],
    notification: {
        title: 'Hello There...!', 
        body: 'this is test notification' 
    }
};

希望它有效..!!!

【讨论】:

  • firebase 中有这方面的文档吗?
  • @Utsav Gupta 您可以从服务器端使用它。看看
  • 它说“不支持多个收件人。”
【解决方案2】:

我知道这个问题已经得到解答,而不是一个最新的问题。

当然,您可以构建string array 并按照谷歌文档find here 中提到的方式发送它。

HTTP POST 请求

例如,要将注册 ID 为 51 的设备添加到 appUser-Chris,您可以发送以下请求:

{
   "operation": "add",
   "notification_key_name": "appUser-Chris",
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ",
   "registration_ids": ["51"] //you can insert something more of your strings here.
}

回复格式

添加或删除设备的成功请求会返回如下所示的 notification_key:

{
   "notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ"
}

【讨论】:

    【解决方案3】:
    <?php
    // Your code here!
    class FCM
    {
        private $key;
    
        function __construct($api_key)
        {
            $this->key = $api_key;
        }
    
        /**
         * Sending Push Notification
         */
        public function send_notification($registatoin_ids, $notification)
        {
            $fields = array ( 'to' => $registatoin_ids,'notification' => $notification); //'restricted_package_name' => "com.apps.firebasenotificationphp");
            
            $headers = array (
                    'Authorization: key=' . $this->key,
                    'Content-Type: application/json'
            );
    
           $ch = curl_init ();
           curl_setopt ( $ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
           curl_setopt ( $ch, CURLOPT_POST, true );
           curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
           curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
           curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );
           return curl_exec ( $ch );
           curl_close ( $ch );
        }
    }
    
    $api_key = "";
    $device_id = "";
    $fcm = new FCM($api_key);
    $arrNotification = array ("body" => "hii","title" => "hello",'image' => "https://www.gstatic.com/devrel-devsite/prod/vb1c70bbe2f68b543db3deb1075af42e62f8f21e5fc703b8398dc6b9860f1711f/firebase/images/lockup.png",'link' => "",'sound' => "default");
    
    echo $fcm->send_notification($device_id,$arrNotification);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      相关资源
      最近更新 更多