通过GCM通知,但只需显示消息不是整个字符串
这是我第一次尝试使用Android推送通知。我有推动工作,并收到通知,但收到的消息是一个刺痛。通过GCM通知,但只需显示消息不是整个字符串
捆绑[{机器人= { “警报”: “这是一个消息”},来自= 512815927951,android.support.content.wakelockid = 1,collapse_key的= do_not_collapse}]
所有我想看到通知提醒。“这是一个消息”
这里是我的GCMIntentService.java
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.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private static final String TAG = "GcmIntentService";
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm;
gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
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());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification(extras.toString());
Log.i(TAG, "Received: " + extras.toString());
}
}
// 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.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setVibrate(new long[] { 1000, 1000});
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
根据您的输出,您发送以下名称/值对 - android={"alert":"This is a Message"}
。
这意味着您应该获得“android”键的值,并将其解析出来。
变化
sendNotification(extras.toString());
到
String android = extras.getString("android");
int begin = android.indexOf(":\"");
int end = android.indexOf("\"}");
String text = null;
if (begin >= 0 && end >= begin) {
text = android.substring(begin+2,end);
}
sendNotification(text);
更改此
String msg = intent.getStringExtra("message");
这个
String msg = extras.getString("alert");
Android工作室表示msg是一个未使用的变量? – 2015-04-04 17:10:53
将'sendNotification(extras.getString(“alert”)'改为'sendNotification(msg)' – Aadi 2015-04-04 17:24:40
Eran的建议得到了它的工作,但是谢谢你的回复和帮助! – 2015-04-04 17:53:04
感谢您的答复!我改变了这个,现在没有在通知中的文字? – 2015-04-04 17:11:29
@SamKing我误解了你的extras.toString()输出。查看编辑 – Eran 2015-04-04 17:24:37
工作正常!但是我在消息文本的末尾看到了一个}?非常感谢你.. – 2015-04-04 17:33:17