我从此网站下载了Google云端消息。 Site
只有当我在Genymotion中运行应用程序时才会出现通知。但不是文字。 通知给出了空。
我有2个java的GcmBroadcastReceiver和GcmIntentService。 但我不知道在哪里寻找错误。
GcmIntentService:
package com.example.provenlogic1.myapplication;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
String TAG="pavan";
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
Log.d("pavan","in gcm intent message "+messageType);
Log.d("pavan","in gcm intent message bundle "+extras);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that
GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
String recieved_message=intent.getStringExtra("text_message");
sendNotification("message recieved :" +recieved_message);
Intent sendIntent =new Intent("message_recieved");
sendIntent.putExtra("message",recieved_message);
LocalBroadcastManager.getInstance(this).sendBroadcast(sendIntent);
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_signin_btn_text_disabled_dark)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
GcmIntentService
package com.example.provenlogic1.myapplication;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import java.util.Iterator;
import java.util.Set;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Genymotion上的输出:
日志:
10-17 12:29:25.998 12541-17229/com.example.provenlogic1.myapplication D/pavan: in gcm intent message gcm
10-17 12:29:25.998 12541-17229/com.example.provenlogic1.myapplication D/pavan: in gcm intent message bundle Bundle[mParcelledData.dataSize=268]
10-17 12:29:26.006 12541-17229/com.example.provenlogic1.myapplication I/pavan1: data =Bundle[{android.support.content.wakelockid=9, price=A test , collapse_key=do_not_collapse, from=693176399203}]
10-17 12:29:26.010 12541-12541/com.example.provenlogic1.myapplication D/pavan: in local braod null
price =测试
这就是我发送的内容。
答案 0 :(得分:0)
以下是您需要查看的内容:
String recieved_message=intent.getStringExtra("text_message");
sendNotification("message recieved :" +recieved_message);
由于某些原因, recieved_message
为空。记录数据
Log.d(LOG_TAG, "intent.getExtras() >> " + intent.getExtras().toString());
数据应该是您的JSON
播放量,您需要正确解析它。
使用该日志更新您的帖子也许我可以帮助您进一步
试试这个:
String receivedMessage = intent.getExtras().getString("text_message");
Log.d(LOG_TAG, "Received message >> " + receivedMessage);
答案 1 :(得分:0)
我在我的项目中有这个问题,serverSide代码中的问题,如果它的php,用这个改变你的代码
array( "message" => $message )
$greetMsg = $_POST['message'];
$message = $respJson;
<?php
//Generic php function to send GCM push notification
function sendPushNotificationToGCM($registation_ids,$message) {
//Google cloud messaging GCM-API url
$url = 'https://gcm-http.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registation_ids,
'data' => array( "message" => $message ),
);
// Update your Google Cloud Messaging API Key
if (!defined('GOOGLE_API_KEY')) {
define("GOOGLE_API_KEY", "");
}
$headers = array(
'Authorization: key=' . GOOGLE_API_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);
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) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
<?php
include_once 'db_functions.php';
$db = new DB_Functions();
$selUsers = $_POST['sendmsg'];
if(empty($selUsers))
{
echo("You didn't select any users.");
}
else
{
$resp = "<tr id='header'><td>GCM Response [".date("h:i:sa")."]</td></tr>";
$userCount = count($selUsers);
$greetMsg = $_POST['message'];
$respJson = $greetMsg;
$registation_ids = array();
for($i=0; $i < $userCount; $i++)
{
$gcmRegId = $db->getGCMRegID($selUsers[$i]);
$row = mysql_fetch_assoc($gcmRegId);
//Add RegIds retrieved from DB to $registration_ids
array_push($registation_ids, $row['gcm_regid']);
}
// JSON Msg to be transmitted to selected Users
$message = $respJson;
$pushsts = sendPushNotificationToGCM($registation_ids, $message);
$resp = $resp."<tr><td>".$pushsts."</td></tr>";
echo "<table>".$resp."</table>";
}
?>