Skip to content

Commit

Permalink
Notifications for request made and accepted
Browse files Browse the repository at this point in the history
* Notification stuff initialised
-Node.Js part added
-Notification added to DB on request sent and request accepted, with different 'type' value

* Some bugfixes
-changed onSuccessListener to onCompleteListener in ReceivedRequestTEA
-Added some "No internet" toasts here and there.

* Foreground notifications in place

* Added notification sound
  • Loading branch information
divya21raj authored Mar 11, 2018
1 parent 101f34d commit f2ebb80
Show file tree
Hide file tree
Showing 18 changed files with 4,694 additions and 18 deletions.
66 changes: 65 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ANDROID STUDIO
*.iml
.gradle
/local.properties
Expand All @@ -6,4 +7,67 @@
/captures
.externalNativeBuild
/.idea/
/.project
/.project

#NODE JS
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
15 changes: 14 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,25 @@

<activity
android:name=".activities.RequestActivity.RequestActivity"
android:label="@string/title_requests" />
android:label="@string/title_requests">
<intent-filter>
<action android:name="android.intent.action.TARGET_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity android:name=".activities.MessageListActivity" />

<activity android:name=".activities.SettingsActivity.SettingsActivity"
android:label="Settings"/>

<service
android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package garbagecollectors.com.snucabpool;

import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

import com.google.firebase.messaging.RemoteMessage;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService
{

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
super.onMessageReceived(remoteMessage);

String notificationTitle = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();

String clickAction = remoteMessage.getNotification().getClickAction();

Uri sound = Uri.parse(remoteMessage.getNotification().getSound());

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(sound);

//clicking on the notification
Intent resultIntent = new Intent(clickAction);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);

//issuing
int notificationId = (int) System.currentTimeMillis();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// Builds notification and issues it
notificationManager.notify(notificationId, mBuilder.build());


}
}
15 changes: 14 additions & 1 deletion app/src/main/java/garbagecollectors/com/snucabpool/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ public class User
//Key is the entryId of entry requested, Value is list of userIDs who've requested that entry
//We have Map because we're taking TripEntry object of the entry that we have made (that the other person has clicked on)

private String deviceToken;

private ArrayList<PairUp> pairUps;

public User(String userId, String name, ArrayList<TripEntry> userTripEntries, ArrayList<TripEntry> requestSent, HashMap<String, ArrayList<String>> requestsReceived, ArrayList<PairUp> pairUps)
public User(String userId, String name, ArrayList<TripEntry> userTripEntries, ArrayList<TripEntry> requestSent, HashMap<String, ArrayList<String>> requestsReceived, String deviceToken, ArrayList<PairUp> pairUps)
{
this.userId = userId;
this.name = name;
this.userTripEntries = userTripEntries;
this.requestSent = requestSent;
this.requestsReceived = requestsReceived;
this.deviceToken = deviceToken;
this.pairUps = pairUps;
}

Expand Down Expand Up @@ -79,4 +82,14 @@ public ArrayList<PairUp> getPairUps()
{
return pairUps;
}

public String getDeviceToken()
{
return deviceToken;
}

public void setDeviceToken(String deviceToken)
{
this.deviceToken = deviceToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class BaseActivity extends AppCompatActivity implements BottomNa
protected static DatabaseReference userMessageDatabaseReference;
protected static DatabaseReference entryDatabaseReference = FirebaseDatabase.getInstance().getReference("entries");
protected static DatabaseReference pairUpDatabaseReference = FirebaseDatabase.getInstance().getReference("pairUps");
protected static DatabaseReference notificationDatabaseReference = FirebaseDatabase.getInstance().getReference("notifications");

public static User finalCurrentUser;

Expand Down Expand Up @@ -144,7 +145,7 @@ public void onDataChange(DataSnapshot dataSnapshot)
public void onCancelled(DatabaseError error)
{
// Failed to read value
Log.w("Hello", "Failed to read value.", error.toException());
Log.w("UserDB", "Failed to read userDB value.", error.toException());
}
});

Expand Down Expand Up @@ -181,7 +182,7 @@ public void onChildMoved(DataSnapshot dataSnapshot, String s)
public void onCancelled(DatabaseError databaseError)
{
// Failed to read value
Log.w("Hello", "Failed to read value.", databaseError.toException());
Log.w("userDB", "Failed to read UserMessages.", databaseError.toException());
}
});

Expand Down Expand Up @@ -279,7 +280,7 @@ public boolean onOptionsItemSelected(MenuItem item)
return true;

case R.id.action_refresh:
RequestActivity.refreshRequests();
RequestActivity.refreshRequests(getApplicationContext());
break;

}
Expand Down Expand Up @@ -361,6 +362,11 @@ public static void setEntryDatabaseReference(DatabaseReference entryDatabaseRefe
BaseActivity.entryDatabaseReference = entryDatabaseReference;
}

public static DatabaseReference getNotificationDatabaseReference()
{
return notificationDatabaseReference;
}

public static User getFinalCurrentUser()
{
return finalCurrentUser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.iid.FirebaseInstanceId;

import java.text.ParseException;
import java.util.ArrayList;
Expand Down Expand Up @@ -267,7 +268,9 @@ private void dummyInitFinalCurrentUser(FirebaseUser user) throws ParseException
ArrayList<PairUp> dummyPairUps = new ArrayList<>();
dummyPairUps.add(dummyPairUp);

finalCurrentUser = new User(user.getUid(), user.getDisplayName(), dummyUserEntries, dummyRequestSent, dummyRequestReceived, dummyPairUps);
String deviceToken = FirebaseInstanceId.getInstance().getToken();

finalCurrentUser = new User(user.getUid(), user.getDisplayName(), dummyUserEntries, dummyRequestSent, dummyRequestReceived, deviceToken, dummyPairUps);
}

private void updateUI(FirebaseUser currentUser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -27,6 +28,8 @@ public class ReceivedRequestsFragment extends Fragment
RecyclerView recycle;
static ReceivedRequestsTEA recyclerAdapter;

String TAG = "Requests";

User user;
HashMap<String, ArrayList<String>> receivedRequestsMap;
static ArrayList<TripEntry> receivedRequestsList;
Expand Down Expand Up @@ -69,6 +72,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

task.addOnSuccessListener(o ->
{
Log.i(TAG, receivedRequestsMap.toString() + "\n\n");
Log.i(TAG, receivedRequestsList.toString());

recyclerAdapter = new ReceivedRequestsTEA(receivedRequestsList, getContext());

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package garbagecollectors.com.snucabpool.activities.RequestActivity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
Expand All @@ -14,6 +15,7 @@
import android.view.MenuInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
Expand Down Expand Up @@ -95,7 +97,7 @@ public boolean onCreateOptionsMenu(Menu menu)
return true;
}

public static void refreshRequests()
public static void refreshRequests(Context context)
{
requestsProgressBar.setVisibility(View.VISIBLE);

Expand Down Expand Up @@ -169,6 +171,9 @@ public void onCancelled(DatabaseError databaseError)
requestsProgressBar.setVisibility(View.INVISIBLE);
});
});

allTask.addOnFailureListener(e ->
Toast.makeText(context, "Can't fetch requests, problem with the internet connection!", Toast.LENGTH_LONG).show());
}

private void setupViewPager(ViewPager viewPager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public void onDataChange(DataSnapshot dataSnapshot)
try
{
MessageDBSource.setResult(dataSnapshot);
}catch (IllegalStateException ignored)
}
catch (IllegalStateException ignored)
{}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void onBindViewHolder(MyHolder holder, int position)
tripEntryUser[0] = snapshot.getValue(User.class);

DatabaseReference userDatabaseReference = FirebaseDatabase.getInstance().getReference("users");
DatabaseReference notificationDatabaseReference = BaseActivity.getNotificationDatabaseReference();

ArrayList<TripEntry> requestSent = user.getRequestSent();
HashMap<String, ArrayList<String>> requestsReceived = tripEntryUser[0].getRequestsReceived();
Expand All @@ -117,7 +118,13 @@ public void onBindViewHolder(MyHolder holder, int position)
Task<Void> task1 = userDatabaseReference.child(user.getUserId()).child("requestSent").setValue(requestSent);
Task<Void> task2 = userDatabaseReference.child(tripEntryUser[0].getUserId()).child("requestsReceived").setValue(requestsReceived);

Task<Void> allTask = Tasks.whenAll(task1, task2);
HashMap<String, String> notificationObject = new HashMap<>();
notificationObject.put("from", user.getUserId());
notificationObject.put("type", "requestCreated");

Task<Void> task3 = notificationDatabaseReference.child(tripEntryUser[0].getUserId()).push().setValue(notificationObject);

Task<Void> allTask = Tasks.whenAll(task1, task2, task3);
allTask.addOnSuccessListener(bVoid ->
{
progressDialog.dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ public void onBindViewHolder(MyHolder holder, int position)
requestsProgressDialog.setMessage("Please wait...");
requestsProgressDialog.show();

MessageDBTask.addOnSuccessListener(o -> requestsProgressDialog.dismiss());
MessageDBTask.addOnCompleteListener(o -> requestsProgressDialog.dismiss());

holder.itemView.setOnClickListener(view ->
{
DatabaseReference userDatabaseReference = FirebaseDatabase.getInstance().getReference("users");
DatabaseReference pairUpDatabaseReference = BaseActivity.getPairUpDatabaseReference();
DatabaseReference notificationDatabaseReference = BaseActivity.getNotificationDatabaseReference();

TripEntry tripEntry = list.get(position);

Expand Down Expand Up @@ -120,6 +121,10 @@ public void onBindViewHolder(MyHolder holder, int position)
BaseActivity.getChatList().add(tripEntryUser[0]);
ChatFragment.recycleAdapter.notifyDataSetChanged();

HashMap<String, String> notificationObject = new HashMap<>();
notificationObject.put("from", finalCurrentUser.getUserId());
notificationObject.put("type", "requestAccepted");

Task<Void> task1 = userDatabaseReference.child(finalCurrentUser.getUserId()).child("pairUps").setValue(currentUserPairUps);
Task<Void> task2 = userDatabaseReference.child(tripEntryUser[0].getUserId()).child("pairUps").setValue(tripEntryUserPairUps);

Expand All @@ -128,11 +133,13 @@ public void onBindViewHolder(MyHolder holder, int position)

Task<Void> task5 = pairUpDatabaseReference.child(pairUpId).setValue(pairUp);

Task<Void> allTask = Tasks.whenAll(task1, task2, task3, task4, task5);
Task<Void> task6 = notificationDatabaseReference.child(tripEntryUser[0].getUserId()).push().setValue(notificationObject);

Task<Void> allTask = Tasks.whenAll(task1, task2, task3, task4, task5, task6);
allTask.addOnSuccessListener(bVoid ->
{
requestsProgressDialog.dismiss();
RequestActivity.refreshRequests();
RequestActivity.refreshRequests(context);
});

allTask.addOnFailureListener(e ->
Expand Down
Loading

0 comments on commit f2ebb80

Please sign in to comment.