Google Firebase通知在控制台上工作,但不在API上 [英] Google Firebase notifications working on console but not on API

查看:152
本文介绍了Google Firebase通知在控制台上工作,但不在API上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通知从firebase控制台发送时工作得很好,但从API发送时不起作用。即使结果显示成功:
{multicast_id:5946406103096345260,success:1,failure:0,canonical_ids:0,results:[{message_id:0:



无论如何,继承人的代码:

 <?php 
// Payload要发送给Android设备的数据
//(可通过意向附加功能访问)
$ data = array('title' ='Notification Title','message'=>'Hello World!');

//此通知的收件人注册令牌
$ ids = array('TOKEN');

//通过Google Cloud Messaging发送推送通知
sendPushNotification($ data,$ ids);

函数sendPushNotification($ data,$ ids)
{
//从Google API控制台插入真正的GCM API密钥
$ apiKey ='API_KEY';

//设置POST请求体
$ post = array(
'registration_ids'=> $ ids,
'data'=> $ data,
);

//设置CURL请求标题
$ headers = array(
'Authorization:key ='。$ apiKey,
'Content-Type:application / json'
);

//初始化curl句柄
$ ch = curl_init();

//将URL设置为GCM推送端点
curl_setopt($ ch,CURLOPT_URL,'https://fcm.googleapis.com/fcm/send');

//将请求方法设置为POST
curl_setopt($ ch,CURLOPT_POST,true);

//设置自定义请求头文件
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);

//以字符串形式获取响应,而不是打印它
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);

//设置JSON发布数据
curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ post));

//实际发送请求
$ result = curl_exec($ ch);

//处理错误
if(curl_errno($ ch))
{
echo'GCM error:'。 curl_error($ CH);
}

//关闭卷曲句柄
curl_close($ ch);

//调试GCM响应
echo $ result;
}

?>


解决方案

FCM仅在使用<$ c时发送推送通知

$ {
to:注册令牌,
priority:high,
通知:{
title:标题,
text:文本
},
...
}

}



另请参阅 Firebase文档


The notification work just fine when sent from the firebase console, but don't work when sent from the API. Even when the result displays a success: {"multicast_id":5946406103096345260,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1480093752122166%13791f60f9fd7ecd"}]}

Anyway heres the code:

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

?> 

解决方案

FCM only sends push notifications when using the notification payload, e.g.:

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

}

See also Firebase Docs

这篇关于Google Firebase通知在控制台上工作,但不在API上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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