gpt4 book ai didi

php - 无法从 GCM 获取消息

转载 作者:行者123 更新时间:2023-11-30 01:48:20 27 4
gpt4 key购买 nike

我正在构建基于 GCM 的应用程序。现在我成功收到了通知,但我收到的消息是一个空对象,我不明白为什么会这样。

这是 GCM 的服务器端:

  <?php


class GCM {



function __construct() {

}

public function send_notification($registatoin_ids, $message) {
// include config
include_once 'connection.php';

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
'registration_ids' => $registatoin_ids,
'data' => array("message" => $message),
);

$headers = array(
'Authorization: key=' .GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}

// Close connection
curl_close($ch);
return $result;
}
}
?>

这是 GCM 客户端实现:GcmBroadcastReciever:

    package com.example.matant.gpsportclient.GoogleCloudNotifications;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;


public class GcmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
context.startService((intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}

}

GCMIntentService:

       package com.example.matant.gpsportclient.GoogleCloudNotifications;


import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

import com.example.matant.gpsportclient.R;
import com.google.android.gcm.GCMBaseIntentService;


public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCM Tutorial::Service";

// Use your PROJECT ID from Google API into SENDER_ID
public static final String SENDER_ID = "8462XXXXXX";

public GCMIntentService() {
super(SENDER_ID);
}

@Override
protected void onRegistered(Context context, String registrationId) {

Log.i(TAG, "onRegistered: registrationId=" + registrationId);
}

@Override
protected void onUnregistered(Context context, String registrationId) {

Log.i(TAG, "onUnregistered: registrationId=" + registrationId);
}

@Override
protected void onMessage(Context context, Intent data) {
String message;
// Message from PHP server
message = data.getStringExtra("message");
// Open a new activity called GCMMessageView
Intent intent = new Intent(this, GCMMessageView.class);
// Pass data to the new activity
intent.putExtra("message", message);
// Starts the activity on notification click
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification with a notification builder
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.gpsport_logo_icon)
.setWhen(System.currentTimeMillis())
.setContentTitle("GPSport Notification")
.setContentText(message).setContentIntent(pIntent)
.getNotification();
// Remove the notification on click
notification.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.string.app_name, notification);

{
// Wake Android Device when notification received
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
mWakelock.acquire();

// Timer before putting Android Device to sleep mode.
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
mWakelock.release();
}
};
timer.schedule(task, 5000);
}

}

@Override
protected void onError(Context arg0, String errorId) {

Log.e(TAG, "onError: errorId=" + errorId);
}
}

最佳答案

GCM 返回包中的数据,尝试使用数组而不是直接使用消息值

 $data=array();
$data['message']=$message;
$data['new_data']=$newData;

然后将该数组传递给 GCMfield 数组

$fields = array(
'registration_ids' => $gcmids,
'data' => $data,
);

还要确保在调用 send_notification 函数时传递 !null 消息 :)

关于php - 无法从 GCM 获取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33408060/

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