[英]google cloud messaging gives null
我從該站點下載了谷歌雲消息。 現場
僅當我在Genymotion中運行該應用程序時,通知才會出現。 但不是文字。 通知給出一個Null。
我有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
價格=測試
這就是我發送的。
這是您需要查看的行:
String recieved_message=intent.getStringExtra("text_message");
sendNotification("message recieved :" +recieved_message);
由於某些原因, recieved_message
為null。 記錄數據
Log.d(LOG_TAG, "intent.getExtras() >> " + intent.getExtras().toString());
數據應該是您的JSON
播放量,並且您需要正確解析它。
使用該日志更新您的帖子,也許我可以為您提供進一步的幫助
嘗試這個:
String receivedMessage = intent.getExtras().getString("text_message");
Log.d(LOG_TAG, "Received message >> " + receivedMessage);
我在我的項目中有該問題,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>"; } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.