成功:1 在android中仍然没有通知
Posted
技术标签:
【中文标题】成功:1 在android中仍然没有通知【英文标题】:Success:1 still no notification in android 【发布时间】:2016-01-16 08:03:20 【问题描述】:我正在尝试通过 GCM 发送推送通知,但我得到:
"multicast_id":4793018731783267982,
"success":1,
"failure":0,
"canonical_ids":0,
"results": [
"message_id":"0:1452928098906665%a69ccee8f9fd7ecd"
]
但仍然没有通知。这是我的代码:
//index.php code
$app->get( '/notify',function () use($app)
$response = array ();
$regId="device registration id"; //hard coded
$message="hi everyone.this is to notify you" ;
$registatoin_ids = array($regId);
$message = array("price" => $message);
$db = new DbHandler ();
$result = $db->send_notification($registatoin_ids, $message);
echoRespnse ( 200, $result);
);
//Dbhandler code
public function send_notification($registatoin_ids, $message)
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=AIzaSyByR3xuTkhB-OZSNTlQqlwnqsLBzqXUWb0', //server key
'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);
// 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;
注意:我直接从高级 Rest 客户端而不是 android 设备运行此 API。我得到了成功的响应,但在我在这里硬编码的 reg_id 的设备上没有通知。请帮助
编辑================== 我在android端写了这段代码:
if (checkPlayServices())
gcm = GoogleCloudMessaging.getInstance(this);
if (regid.isEmpty())
new register().execute();
else
Log.i("hello", "No valid Google Play Services APK found.");
public class register extends AsyncTask<Void, Integer, String>
@Override
protected void onPreExecute()
super.onPreExecute();
@Override
protected String doInBackground(Void... params)
String msg = "";
try
if (gcm == null)
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
regid = gcm.register(AppConst.SENDER_ID);
Log.i("hello", "Current Device's Registration ID is: "+regid);
catch (IOException ex)
msg = "Error :" + ex.getMessage();
return null;
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
在 Mainfest 我添加了:
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example" />
</intent-filter>
</receiver>
【问题讨论】:
你写过代码来在你的安卓应用中接收通知吗? 有"success":1
不代表消息发送成功。它在documentation 中声明该消息已被处理且没有错误,这可能意味着该消息已排队等待发送。您可以参考这个thread,它可能会有所帮助并可能会回答您的问题。
我收到来自服务器 ip 的通知,但不是来自域 www.example.com 的通知。请告诉我为什么??
【参考方案1】:
在 Android 设备上处理通知:
@Override
public void onMessageReceived(String from, Bundle data)
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
// Handle received message here.
欲了解更多信息,请访问:https://developers.google.com/cloud-messaging/
【讨论】:
以上是关于成功:1 在android中仍然没有通知的主要内容,如果未能解决你的问题,请参考以下文章