/*
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.cloud.backend.android;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
/**
* This class manages Google Cloud Messaging push notifications and CloudQuery
* subscriptions.
*
*/
public class GCMReceiver extends BroadcastReceiver {
private final static String GCM_KEY_SUBID = "subId";
private final static String GCM_TYPEID_QUERY = "query";
/**
* Returns registration id associated with the specified {@link Context}.
*
* @param context
* {@link Context}
* @return registration id
*/
public static String getRegistrationId(Context context) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String registrationId = null;
try {
registrationId = gcm.register(Consts.PROJECT_NUMBER);
} catch (IOException e) {
e.printStackTrace();
}
return registrationId;
}
@Override
public void onReceive(Context context, Intent intent) {
// decode subId in the message
String subId = intent.getStringExtra(GCM_KEY_SUBID);
Log.i(Consts.TAG, "onMessage: subId: " + subId);
String[] tokens = subId.split(":");
String typeId = tokens[1];
// dispatch message
if (GCM_TYPEID_QUERY.equals(typeId)) {
CloudBackendAsync.handleQueryMessage(tokens[2]);
}
}
}