我已在我的应用上实施了适用于Android客户端的Google Cloud Messaging。从我们的第三方服务器发送时,我们收到了一条成功消息:
EDITED
事实证明我实际收到了来自Google Cloud Messaging服务器的消息,但我没有在手机上收到通知。我知道我收到了消息,因为它显示了我的Logcat。唯一的问题是我的通知没有建立/发送。
为什么会这样?请注意我的GcmIntentService
是一个独立的课程。和BroadcastReceiver
一样。我猜这与我的sendNotification()
方法有关吗?
IntentService
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
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);
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)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i("GCM", "Working... " + (i+1) + "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
}
catch (InterruptedException e) { e.printStackTrace(); }
}
Log.i("GCM", "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " + extras.toString());
Log.i("GCM", "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, LoginActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
广播接收器
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);
}
}
答案 0 :(得分:3)
想出来!!!
原则上你需要为每个通知提供3件事:
卫生署!
答案 1 :(得分:0)
通过'fake_message_I'd in'在服务器上的响应中,您必须在消息中设置dry_run参数。这只是验证消息而不实际发送它。删除该参数并重新测试。