コード例 #1
0
  /**
   * 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.
   *
   * @param msg
   * @param bundle
   */
  public void sendNotification(String msg, Bundle bundle) {
    Log.v(Globals.TAG, "GcmIntentService Broadcasting Notification");

    if (bundle == null) return;

    action = bundle.getString(Globals.ACTION);

    if (action != null
        && (Globals.ACTION_NOTIFICATION.equals(action)
            || Globals.ACTION_CLEAR_NOTIFICATION.equals(action))) {
      msg_type = action.substring(action.lastIndexOf(".") + 1);

      msg = bundle.getString(Globals.MESSAGE);

      if (msg.contains(Globals.NOT_REGISTERED_2)) {
        // if we shared the product with someone who is not registered with our GCM Server
        pushNotification(msg, false);
      } else if (msg.contains(Globals.NEW_DEVICE_REGISTERED)) {
        pushNotification(msg, true);

      } else {
        senderEmailId = bundle.getString(Globals.SENDER_EMAIL_ID);
        receiverEmailId = bundle.getString(Globals.RECEIVER_EMAIL_ID);
        asinId = bundle.getString(Globals.ASIN_ID);
        productName = bundle.getString(Globals.PRODUCT_NAME);
        brandName = bundle.getString(Globals.BRAND_NAME);
        productPrice = bundle.getString(Globals.PRODUCT_PRICE);
        productImageUrlString = bundle.getString(Globals.PRODUCT_IMAGE_URL);

        productRating = 0;
        try {
          productRating = Integer.parseInt(bundle.getString(Globals.PRODUCT_RATING));
        } catch (NumberFormatException e) {
          e.printStackTrace();
        }
        clearNotificationTo = senderEmailId;

        if (bundle.containsKey("notification_key")
            && (bundle.getString("notification_key") != null)) {
          clearNotificationTo = bundle.getString("notification_key");
        }
        pushNotification(
            msg,
            senderEmailId,
            productImageUrlString,
            asinId,
            productName,
            brandName,
            productPrice,
            productRating,
            receiverEmailId,
            clearNotificationTo);
      }
    }
  }
コード例 #2
0
  @Override
  protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    // The getMessageType() intent parameter must be the intent you receive 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(), null);

      } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
        sendNotification("Deleted messages on server: " + extras.toString(), null);

        // If it's a regular GCM message, do some work
      } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
        // Post notification of received message
        String message = extras.toString();

        if (Globals.ACTION_CLEAR_NOTIFICATION.equals(extras.getString("action"))) {
          clearNotification();
        } else if (Globals.ACTION_NOTIFICATION.equals(extras.getString("action"))) {
          sendNotification("Received: " + message, extras);
        } else {
          stopProgressBarIfRequired(extras);
        }
        Log.v(Globals.TAG, "Received: " + message);
      }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
  }