我正在尝试在我的Android设备上使用GCM发送通知,但是我总是收到InvalidRegistration错误。
以下是用于发送通知的PHP代码:
<?php
define('API_ACCESS_KEY', 'API KEY HIDDEN');
$registrationIds = array( $_GET['id']);
// prep the bundle
$msg = array
(
'message' => 'TestMessage',
'title' => 'TestTitle',
'subtitle' => 'TestSubtitle',
'tickerText' => 'TestTicker',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
这是注册的步骤:
private void registerInBackground() {
new AsyncTask<Void, Object, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
//Here I save the registration ID, and when I try to use it manually, by running the PHP script above, I get the error.
sendRegistrationIdToBackend();
// Persist the registration ID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(context, msg + "\n", Toast.LENGTH_SHORT).show();
}
}.execute(null, null, null);
运行应用程序并调用注册应用程序的方法后,我会获得一个注册 ID。然后,当我输入
localhost/app/sendNotification.php?id=xxxxxxxxxxxxxx
时,我会得到:{"multicast_id":8460288429151356251,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
success:1
的提示,但我还有另一个问题,我该如何开始在应用程序中接收消息呢?它们没有到达。 - Val