Skip to content

Commit

Permalink
show notification for new friend request
Browse files Browse the repository at this point in the history
  • Loading branch information
mRahulJain committed May 23, 2020
1 parent 3a750e8 commit 590c064
Show file tree
Hide file tree
Showing 70 changed files with 16,785 additions and 5 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<activity android:name=".Activities.ProfileActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity" />

<service android:name=".Notification.FriendRequestMessagingService">
<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
Expand Up @@ -6,10 +6,8 @@ import android.util.Log
import androidx.fragment.app.Fragment
import com.android.beastchat.Fragments.InboxFragment
import com.android.beastchat.Models.constants
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessagingService

class InboxActivity : BaseFragmentActivity() {
override fun createFragment(): Fragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ class FindFriendsFragment : BaseFragments(), FindFriendsAdapter.UserListener {
mAllUsers.clear()
for(data in p0.children) {
val mUser = data.getValue(User::class.java)
if(mUser!!.email != mUserEmailString && mUser!!.hasLoggedIn) {
//it should not matter whether the user is logged in or not
if(mUser!!.email != mUserEmailString) {
mAllUsers.add(mUser)
}
adapter.setmUsers(mAllUsers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.android.beastchat.Notification

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.media.RingtoneManager
import android.os.Build
import androidx.core.app.NotificationCompat
import com.android.beastchat.Activities.FriendsActivity
import com.android.beastchat.R
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class FriendRequestMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(p0: RemoteMessage) {
super.onMessageReceived(p0)
var title = p0.data!!["title"]
var body = p0.data!!["body"]
sendNotification(title!!, body!!)

}

@SuppressLint("WrongConstant")
private fun sendNotification(title: String, body: String) {
val intent = Intent(this, FriendsActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
val pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val pattern: Array<Long> = arrayOf(
500,500,500,500,500
)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

val nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// nm.createNotificationChannel(
// NotificationChannel("first","FriendRequest",
// NotificationManager.IMPORTANCE_HIGH)
// )
val importance = NotificationManager.IMPORTANCE_HIGH
var mChannel = nm.getNotificationChannel("first")
if(mChannel == null) {
mChannel = NotificationChannel("first", "FriendRequest", importance)
mChannel!!.description = "FriendRequest"
mChannel!!.enableVibration(true)
mChannel!!.lightColor = Color.BLUE
mChannel!!.vibrationPattern = pattern!!.toLongArray()
nm.createNotificationChannel(mChannel)
}
}

val clickableNotification = NotificationCompat.Builder(this, "first")
.setContentTitle(title)
.setContentText(body)
.setVibrate(pattern.toLongArray())
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLights(Color.BLUE, 1, 1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)

nm.notify(0, clickableNotification.build())
}
}
28 changes: 26 additions & 2 deletions node-server/firebase/friend-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var admin = require("firebase-admin");
var FCM = require("fcm-push");
var serverKey = '---';
var fcm = new FCM(serverKey);


var userFriendRequests = (io) => {
io.on('connection', function(socket){
Expand Down Expand Up @@ -70,8 +74,28 @@ function sendOrDeleteFriendRequest(socket, io) {
userPicture: snapshot.val().userPicture,
dateJoined: snapshot.val().dateJoined,
hasLoggedIn: snapshot.val().hasLoggedIn,
})
})
});
});

var tokenRef = db.ref('userToken');
var friendToken = tokenRef.child(encodeEmail(friendEmail));

friendToken.once('value', (snapshot)=> {
var message = {
to:snapshot.val().token,
data:{
title: 'Beast Chat',
body: `Friend request from ${userEmail}`
}
};
fcm.send(message)
.then((response)=> {
console.log('Message Sent!');
}).catch((err)=>{
console.log(err);
});
});

} else {
friendRef.remove();
}
Expand Down
21 changes: 21 additions & 0 deletions node-server/node_modules/bluebird/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions node-server/node_modules/bluebird/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node-server/node_modules/bluebird/changelog.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 590c064

Please sign in to comment.