gpt4 book ai didi

android - Android 版 Firebase 云消息传递中的无效注册错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:13 25 4
gpt4 key购买 nike

我正在开发一个使用推送通知功能的 Android 应用程序。我需要从服务器推送。我为此使用 Firebase。这是我第一次使用 Firebase。但是当我使用 PHP 和 CURL 从服务器推送时,它会给我无效的注册错误。

我这样在 Android 中获取 Firebase token

String token = FirebaseInstanceId.getInstance().getToken();

然后我将该 token 发送到服务器并保存在数据库中。

在服务器端,我是这样推的

class Pusher extends REST_Controller {

function __construct()
{
parent::__construct();
}

public function notification_get()
{
$rows = $this->db->get('device_registration')->result();
$tokens= array();
if(count($rows)>0)
{
foreach($rows as $row)
{
$tokens[] = $row->token;
}
}
$message = array("message"=>"FCM PUSH NOTIFICATION TESTING");
if(count($tokens)>0)
{
$result = $this->send_notification($tokens,$message);
if(!$result)
{
die("Unable to send");
}
else{
$this->response($result, REST_Controller::HTTP_OK);
}
}

}

function send_notification($tokens,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids'=>$tokens,
'data'=>$message
);

$headers = array(
'Authorization:key = AIzaSyApyfgXsNQ3dFTGWR6ns_9pttr694VDe5M',//Server key from firebase
'Content-Type: application/json'
);
$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_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result==FALSE)
{
return FALSE;
}
curl_close($ch);
return $result;
}
}

我正在使用 CodeIgniter 3 框架来构建 Rest API。当我从浏览器推送访问 URL 时,它返回带有错误的 JSON 数据,如下面的屏幕截图所示。

enter image description here

如您所见,它给出了 InvalidRegistration 错误并且消息未推送到设备。我的代码有什么问题?

补充

这是我在 Android 中显示通知的 FirebaseMessagingService 类

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message)
{
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setAutoCancel(true)
.setContentTitle("FCM Test")
.setContentText(message)
.setSmallIcon(R.drawable.info)
.setContentIntent(pendingIntent);

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}

最佳答案

虽然我没有使用 codeigniter,但我在发送到 iOS 设备时遇到了 InvalidRegistration 错误,所以我想在这里分享我的解决方案。

在向单个设备 token 发送通知消息时,我必须在 PHP 中将 registration_ids 更改为 to ,并确保 to 的值是字符串而不是数组。

改变这个:

'registration_ids'=>$tokens,

对此:

'to'=>$tokens[0],

关于android - Android 版 Firebase 云消息传递中的无效注册错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com