【问题标题】:Google Firebase notifications working on console but not on APIGoogle Firebase 通知在控制台上工作,但在 API 上不工作
【发布时间】:2017-04-10 03:18:26
【问题描述】:

通知从 firebase 控制台发送时工作正常,但从 API 发送时不起作用。即使结果显示成功: {"multicast_id":5946406103096345260,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1480093752122166%13791f60f9fd7ecd"}]}

代码如下:

<?php
// Payload data you want to send to Android device(s)
// (it will be accessible via intent extras)    
$data = array('title' => 'Notification Title' ,'message' => 'Hello World!');

// The recipient registration tokens for this notification  
$ids = array('TOKEN');

// Send push notification via Google Cloud Messaging
sendPushNotification($data, $ids);

function sendPushNotification($data, $ids)
{
    // Insert real GCM API key from the Google APIs Console        
    $apiKey = 'API_KEY';

    // Set POST request body
    $post = array(
                    'registration_ids'  => $ids,
                    'data'              => $data,
                 );

    // Set CURL request headers 
    $headers = array( 
                        'Authorization: key=' . $apiKey,
                        'Content-Type: application/json'
                    );

    // Initialize curl handle       
    $ch = curl_init();

    // Set URL to GCM push endpoint     
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');

    // Set request method to POST       
    curl_setopt($ch, CURLOPT_POST, true);

    // Set custom request headers       
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // Get the response back as string instead of printing it       
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Set JSON post data
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));

    // Actually send the request    
    $result = curl_exec($ch);

    // Handle errors
    if (curl_errno($ch))
    {
        echo 'GCM error: ' . curl_error($ch);
    }

    // Close curl handle
    curl_close($ch);

    // Debug GCM response       
    echo $result;
}

?> 

【问题讨论】:

    标签: php ios firebase push-notification google-play-services


    【解决方案1】:

    FCM 仅在使用 notification 有效负载时发送推送通知,例如:

    { 
      "to: "registration token",
      "priority": "high",
      "notification": {
        "title": "Title",
        "text": "Text"
      },
      ...
    }
    

    }

    另见Firebase Docs

    【讨论】:

    • 忘了补充:我还需要包含 priority 字段,否则我的推送通知在 iOS 上没有收到。例如。 "priority":"high" 在最高层级
    【解决方案2】:

    对于那些不熟悉 php 的人(如我)@Simon Heinzle 的答案可能有点神秘。这只是意味着在原始代码中应该进行这些更改:

    $data = array('title' => 'Notification Title' ,'text' => 'Hello World!');
    ...
    $post = array(
                  'registration_ids'  => $ids,
                  'priority'  => 'high',
                  'notification' => $data,
                  );
    

    请注意,如果您只需要将通知发送到单个设备,则可以将registration_ids(一个令牌数组)更改为to(单个令牌)。 更多通知选项可以在FCM docs找到。

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2015-06-08
      相关资源
      最近更新 更多