2

我正在尝试实现 GCM 客户端和服务器架构。到目前为止一切正常。

例外:当我的活动关闭并且我收到 GCM 的新通知时,通知会显示在通知栏中。到目前为止,一切都很好。但是当我点击通知时,我的活动被打开但onReceive我的事件BroadcastReceiver没有被触发。:( 如果 Activity 已打开,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 示例复制和改编的。

4

1 回答 1

3

BroadcastReceiver 在通知栏显示通知之前触发。它包含显示通知并在被点击时打开活动的代码(除非它正在启动执行该工作的意图服务)。

因此,如果您看到通知,则表示已触发 BroadcastReceiver。

您不需要额外的 BroadcastReceiver 将通知数据从第一个接收器传递到您的应用程序。如果您希望在点击通知时将通知数据传递给正在启动的 Activity,您可以将其传递给用于启动该 Activity 的意图。

假设您将sendNotification呼叫更改为:

sendNotification(extras, notificationID);

然后你可以像这样实现它:

  private void sendNotification(Bundle extras, final int aNotificationID)
  {
    mNotificationManager = (NotificationManager) this
        .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent demoIntent = new Intent(this, DemoActivity.class);
    demoIntent.putExtras (extras);
    demoIntent.putExtra (Utils.NOTIFICATION_ID_KEY, notificationID);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, demoIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(
        R.drawable.ic_stat_gcm).setContentTitle(extras.getString(Utils.TICKER_TITLE_MESSAGE_KEY)).setStyle(
        new NotificationCompat.BigTextStyle().bigText(extras.getString(Utils.TICKER_TEXT_MESSAGE_KEY))).setContentText(extras.getString(Utils.TICKER_TEXT_MESSAGE_KEY));

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(aNotificationID, mBuilder.build());
  }

这样,您DemoActivity将获得通知 ID 和所有保存通知数据的附加信息。您可以在 Activity 中访问它们onCreate(或者最好在Activity 中进行onResume,以防您的 Activity 已经启动)。

于 2013-10-24T15:40:12.263 回答