Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add topic messaging #53

Merged
merged 1 commit into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/src/main/java/in/antaragni/ant/GCMClientManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

Expand Down Expand Up @@ -47,7 +48,6 @@ public GCMClientManager(Activity activity, String projectNumber) {
public void registerIfNeeded(final RegistrationCompletedHandler handler) {
if (checkPlayServices()) {
regid = getRegistrationId(getContext());

if (regid.isEmpty()) {
registerInBackground(handler);
} else { // got id from cache
Expand Down Expand Up @@ -77,6 +77,11 @@ protected String doInBackground(Void... params) {
regid = instanceID.getToken(projectNumber, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, regid);

if (regid != null)
{
GcmPubSub pubSub = GcmPubSub.getInstance(getContext());
pubSub.subscribe(regid, "/topics/antaragni", null);
}
// Persist the regID - no need to register again.
storeRegistrationId(getContext(), regid);

Expand Down Expand Up @@ -106,7 +111,7 @@ protected void onPostExecute(String regId) {
* @return registration ID, or empty string if there is no existing
* registration ID.
*/
private String getRegistrationId(Context context) {
public String getRegistrationId(Context context) {
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Expand Down
29 changes: 26 additions & 3 deletions app/src/main/java/in/antaragni/ant/GcmMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import in.antaragni.ant.datahandler.DatabaseAccess;

public class GcmMessageHandler extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
private DatabaseAccess databaseAccess;





@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");

createNotification(message, from);
String title = data.getString("title");
String time = data.getString("time");
String venue = data.getString("venue");
String event_name = data.getString("eventname");
String message = "" ;
message = message.concat(event_name);
databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();

if (!time.isEmpty()) {
databaseAccess.updateinfo(event_name, Integer.parseInt(time));
message = message.concat( " " + time);
}
if (!venue.isEmpty()) {
databaseAccess.updateinfo(event_name, venue);
message = message.concat( " " + venue);
}
createNotification(title, message);
databaseAccess.close();
}

// Creates notification based on title and body received
Expand All @@ -28,5 +50,6 @@ private void createNotification(String title, String body) {
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}


}

16 changes: 10 additions & 6 deletions app/src/main/java/in/antaragni/ant/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.mikepenz.iconics.typeface.FontAwesome;
import com.mikepenz.materialdrawer.Drawer;
Expand All @@ -19,6 +17,7 @@
import com.mikepenz.materialdrawer.model.interfaces.Nameable;
import com.mikepenz.materialdrawer.util.KeyboardUtil;


import in.antaragni.ant.fragments.ContactFragment;
import in.antaragni.ant.fragments.EventFragment;
import in.antaragni.ant.fragments.FoodFragment;
Expand All @@ -42,7 +41,6 @@ public class MainActivity extends AppCompatActivity
private GCMClientManager pushClientManager;
String PROJECT_NUMBER = "138444406408";


@Override
protected void onCreate(Bundle savedInstanceState)
{
Expand Down Expand Up @@ -152,17 +150,20 @@ public void onDrawerSlide(View drawerView, float slideOffset)
gcmregister();
}





public void gcmregister()
{
pushClientManager = new GCMClientManager(this, PROJECT_NUMBER);
pushClientManager.registerIfNeeded(new GCMClientManager.RegistrationCompletedHandler()
{

@Override
public void onSuccess(String registrationId, boolean isNewRegistration)
{
Toast.makeText(MainActivity.this, registrationId,
Toast.LENGTH_SHORT).show();
Log.wtf("gcm token", registrationId);

// SEND async device registration to your back-end server
// linking user with device registration id
// POST https://my-back-end.com/devices/register?user_id=123&device_id=abc
Expand Down Expand Up @@ -200,6 +201,9 @@ public void onBackPressed()
if (result != null && result.isDrawerOpen())
{
result.closeDrawer();
}
else
{
super.onBackPressed();
}
}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/in/antaragni/ant/datahandler/DatabaseAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

Expand Down Expand Up @@ -141,4 +142,18 @@ public List<Food> getFood()
cursor.close();
return list;
}

public void updateinfo(String event_name,int time)
{
String query = "UPDATE eventdetails SET start_time=" + time + " WHERE name='" + event_name + "';";
database.execSQL(query);
}

public void updateinfo(String event_name,String venue)
{
String query = "UPDATE eventdetails SET venue='" + venue + "' WHERE name='" + event_name + "';";
database.execSQL(query);
}


}