Skip to content

Commit

Permalink
Revert "Fcm Notification added #19, #138, #110"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ijaiswalshivam authored May 19, 2024
1 parent 1722bf7 commit 895dc6b
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 360 deletions.
8 changes: 0 additions & 8 deletions TalkIn/.idea/deploymentTargetSelector.xml

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

2 changes: 0 additions & 2 deletions TalkIn/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ dependencies {
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.activity:activity:1.8.0'
implementation 'com.google.firebase:firebase-auth:23.0.0'
implementation 'com.google.firebase:firebase-messaging-ktx:24.0.0'
implementation 'com.android.volley:volley:1.2.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
10 changes: 2 additions & 8 deletions TalkIn/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down Expand Up @@ -38,15 +35,12 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<service android:name=".FCMNotificationService" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
Expand Down
95 changes: 10 additions & 85 deletions TalkIn/app/src/main/java/com/example/talk_in/ChatActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.talk_in

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
Expand All @@ -25,29 +26,13 @@ class ChatActivity : AppCompatActivity() {
private lateinit var messageAdapter: MessageAdapter
private lateinit var messageList: ArrayList<Message>
private lateinit var mDbRef: DatabaseReference
private var receiverRoom: String? = null
private var senderRoom: String? = null
private lateinit var receiverUid: String
private lateinit var senderName: String
var receiverRoom: String?=null
var senderRoom: String?=null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)

val name = intent.getStringExtra("name")
receiverUid = intent.getStringExtra("uid") ?: ""
val senderUid = FirebaseAuth.getInstance().currentUser?.uid
mDbRef = FirebaseDatabase.getInstance().reference
senderRoom = "$receiverUid$senderUid"
receiverRoom = "$senderUid$receiverUid"
supportActionBar?.title = name


chatRecyclerView = findViewById(R.id.chatRecyclerView)
messageBox = findViewById(R.id.messageBox)
sendButton = findViewById(R.id.sendButton)
messageList = ArrayList()
messageAdapter = MessageAdapter(this, messageList)

val name= intent.getStringExtra("name")
val receiveruid= intent.getStringExtra("uid")
Expand Down Expand Up @@ -82,37 +67,19 @@ class ChatActivity : AppCompatActivity() {
messageAdapter.notifyDataSetChanged()
}


chatRecyclerView.layoutManager = LinearLayoutManager(this)
chatRecyclerView.adapter = messageAdapter

// Fetch sender's name
senderUid?.let {
mDbRef.child("user").child(it).addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
senderName = dataSnapshot.child("name").value.toString()
// Load chat messages
loadChatMessages()
}

override fun onCancelled(databaseError: DatabaseError) {
override fun onCancelled(error: DatabaseError) {

}

})
}

// Adding message to database
sendButton.setOnClickListener {
val message = messageBox.text.toString()
val messageObject = Message(message, senderUid)
//adding message to data base
sendButton.setOnClickListener{
val message=messageBox.text.toString()
val messageObject= Message(message,senderUid)
mDbRef.child("chats").child(senderRoom!!).child("messages").push()
.setValue(messageObject).addOnSuccessListener {
mDbRef.child("chats").child(receiverRoom!!).child("messages").push()
.setValue(messageObject)
.addOnSuccessListener {
sendNotificationToReceiver(receiverUid, message)
}

}
messageBox.setText("")
}
Expand Down Expand Up @@ -151,46 +118,4 @@ class ChatActivity : AppCompatActivity() {
}
popupMenu.show()
}

private fun loadChatMessages() {
mDbRef.child("chats").child(senderRoom!!).child("messages").addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
messageList.clear()
for (postSnapshot in snapshot.children) {
val message = postSnapshot.getValue(Message::class.java)
messageList.add(message!!)
}
messageAdapter.notifyDataSetChanged()
// Scroll RecyclerView to the bottom
chatRecyclerView.scrollToPosition(messageAdapter.itemCount - 1)
}

override fun onCancelled(error: DatabaseError) {
}
})
}

private fun sendNotificationToReceiver(receiverUid: String, message: String) {
// Fetch receiver's device token
mDbRef.child("users-device-tokens").child(receiverUid).addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val receiverDeviceToken = dataSnapshot.child("deviceToken").value.toString()

val notificationSender = FcmNotificationsSender(
receiverDeviceToken,
"New Message from $senderName",
message,
FirebaseAuth.getInstance().currentUser?.uid ?: "",
senderName,
applicationContext,
this@ChatActivity
)
notificationSender.sendNotifications()
}

override fun onCancelled(databaseError: DatabaseError) {

}
})
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 895dc6b

Please sign in to comment.