我試圖實施GCM客戶端&服務器體系結構。到目前爲止一切正常。GCM:如果應用程序不可見,則不會收到IntentService.sentBroadcast的消息
以下情況除外:當我的活動關閉並且通過GCM得到新通知時,通知欄將顯示在通知欄中。到現在爲止還挺好。但是當我點擊通知時,我的活動已打開,但我的BroadcastReceiver
的事件onReceive
未觸發。 。:( 如果活動是開放的,onReceive
完美觸發
你知道,什麼是錯在這裏
乾杯
克里斯
所以這是我的服務:?
package xy;
import ...;
public class GcmIntentService extends IntentService
{
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);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
final int notificationID = (int) (Math.random() * 100000000);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("GCM notification: Send error", extras.toString(), notificationID);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server", extras.toString(), notificationID);
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification(extras.getString(Utils.TICKER_TITLE_MESSAGE_KEY), extras
.getString(Utils.TICKER_TEXT_MESSAGE_KEY), notificationID);
Intent intentToBroadCast = new Intent(Utils.DISPLAY_MESSAGE_ACTION);
intentToBroadCast.putExtra(Utils.MESSAGE_EXTRA_BUNDLE_KEY, extras);
intentToBroadCast.putExtra(Utils.NOTIFICATION_ID_KEY, notificationID);
sendBroadcast(intentToBroadCast);
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(final String aTitle, final String aText, final int aNotificationID)
{
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
DemoActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(
R.drawable.ic_stat_gcm).setContentTitle(aTitle).setStyle(
new NotificationCompat.BigTextStyle().bigText(aText)).setContentText(aText);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(aNotificationID, mBuilder.build());
}
}
這裏是接收器在我的活動,這是顯示傳入消息:
public class GcmResultReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getBundleExtra(Utils.MESSAGE_EXTRA_BUNDLE_KEY);
String s = extras.getString(Utils.CONTENT_TITLE_MESSAGE_KEY) + "\n"
+ extras.getString(Utils.CONTENT_TEXT_MESSAGE_KEY);
mDisplay.setText(s);
int notificationID = intent.getIntExtra(Utils.NOTIFICATION_ID_KEY, -1);
if (-1 != notificationID) m_SentNotificationIDs.add(notificationID);
if (m_IsVisible) {
clearNotifications();
}
}
};
從Google Android教程的GCM示例中複製並修改了所有內容。
那麼,我該如何調試? 如果我在Eclipse中設置斷點以在我的三星上進行調試,這就是我所看到的: 在派生的'WakefulBroadcastReceiver'類中的'onReceive'每個消息總是被觸發兩次。 然後,派生的IntentService類中的'onHandleIntent'總是被觸發一次。 但是,派生的'BroadcastReceiver'類中的'onReceive'僅在應用程序在屏幕上時觸發。 –
btw:我把'sendBroadcast'放在派生的'IntentService.onHandleIntent'中。它是否正確? –
@ChristianGraf我沒有看到任何理由從意向服務發送廣播。你發送了什麼廣播,目的是什麼?至於調試,如果應用程序沒有在接收通知之前運行,我不知道如何將應用程序連接到調試器。嘗試添加調試打印並檢查您的logcat。 – Eran