-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
358 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
TalkIn/app/src/main/java/com/example/talk_in/FCMNotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.talk_in | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Intent | ||
import android.media.RingtoneManager | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Vibrator | ||
import androidx.core.app.NotificationCompat | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
|
||
class FCMNotificationService : FirebaseMessagingService() { | ||
|
||
private lateinit var mNotificationManager: NotificationManager | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
super.onMessageReceived(remoteMessage) | ||
|
||
// Play notification sound | ||
val notification: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | ||
val r = RingtoneManager.getRingtone(applicationContext, notification) | ||
r.play() | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
r.isLooping = false | ||
} | ||
|
||
// Vibrate the device | ||
val v = getSystemService(VIBRATOR_SERVICE) as Vibrator | ||
val pattern = longArrayOf(100, 300, 300, 300) | ||
v.vibrate(pattern, -1) | ||
|
||
val resourceImage = resources.getIdentifier(remoteMessage.notification?.icon, "drawable", packageName) | ||
|
||
val builder = NotificationCompat.Builder(this, "CHANNEL_ID") | ||
builder.setSmallIcon(resourceImage) | ||
|
||
// Extract data payload | ||
val senderUid = remoteMessage.data["senderUid"] | ||
val senderName = remoteMessage.data["senderName"] | ||
|
||
// Open ChatActivity when notification is clicked | ||
val resultIntent = Intent(this, ChatActivity::class.java).apply { | ||
putExtra("uid", senderUid) | ||
putExtra("name", senderName) | ||
} | ||
|
||
val pendingIntent = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT) | ||
|
||
builder.setContentTitle(remoteMessage.notification?.title) | ||
builder.setContentText(remoteMessage.notification?.body) | ||
builder.setContentIntent(pendingIntent) | ||
builder.setStyle(NotificationCompat.BigTextStyle().bigText(remoteMessage.notification?.body)) | ||
builder.setAutoCancel(true) | ||
builder.priority = Notification.PRIORITY_MAX | ||
|
||
mNotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val channelId = "Your_channel_id" | ||
val channel = NotificationChannel( | ||
channelId, | ||
"Messages Notification", | ||
NotificationManager.IMPORTANCE_HIGH | ||
) | ||
mNotificationManager.createNotificationChannel(channel) | ||
builder.setChannelId(channelId) | ||
} | ||
|
||
mNotificationManager.notify(100, builder.build()) | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
TalkIn/app/src/main/java/com/example/talk_in/FcmNotificationsSender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.example.talk_in | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.util.Log | ||
import com.android.volley.AuthFailureError | ||
import com.android.volley.Request | ||
import com.android.volley.RequestQueue | ||
import com.android.volley.Response | ||
import com.android.volley.toolbox.JsonObjectRequest | ||
import com.android.volley.toolbox.Volley | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
import java.util.HashMap | ||
|
||
class FcmNotificationsSender( | ||
private val userFcmToken: String, | ||
private val title: String, | ||
private val body: String, | ||
private val senderUid: String, | ||
private val senderName: String, | ||
private val mContext: Context, | ||
private val mActivity: Activity | ||
) { | ||
private val requestQueue: RequestQueue = Volley.newRequestQueue(mContext) | ||
private val postUrl = "https://fcm.googleapis.com/fcm/send" | ||
private val fcmServerKey = "AAAAt0loRzU:APA91bE7oxyox_4Q2dHihHCKlQCe9pV7wbGgPXL4uvRsC-1kKfV-H0v37M4prqmV784RdKK7nW8c7POGh3Psyb-gAaqq78wya6yoEdimE0XLARHPtvi1VHdG5VrwHx4n9m6YRqZvY7-s" | ||
|
||
fun sendNotifications() { | ||
Log.d(TAG, "Sending FCM notification") | ||
val mainObj = JSONObject() | ||
try { | ||
mainObj.put("to", userFcmToken) | ||
val notiObject = JSONObject() | ||
notiObject.put("title", title) | ||
notiObject.put("body", body) | ||
notiObject.put("icon", R.drawable.talkin) | ||
mainObj.put("notification", notiObject) | ||
|
||
// Add data payload | ||
val dataObject = JSONObject() | ||
dataObject.put("senderUid", senderUid) | ||
dataObject.put("senderName", senderName) | ||
dataObject.put("message", body) | ||
mainObj.put("data", dataObject) | ||
|
||
val request = object : JsonObjectRequest( | ||
Request.Method.POST, postUrl, mainObj, | ||
Response.Listener { response -> | ||
|
||
}, | ||
Response.ErrorListener { error -> | ||
|
||
} | ||
) { | ||
@Throws(AuthFailureError::class) | ||
override fun getHeaders(): Map<String, String> { | ||
val header = HashMap<String, String>() | ||
header["content-type"] = "application/json" | ||
header["authorization"] = "key=$fcmServerKey" | ||
return header | ||
} | ||
} | ||
requestQueue.add(request) | ||
} catch (e: JSONException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TAG = "FcmNotificationsSender" | ||
} | ||
} |
Oops, something went wrong.