-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from binayshaw7777/develop
Develop
- Loading branch information
Showing
10 changed files
with
129 additions
and
75 deletions.
There are no files selected for viewing
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
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
100 changes: 47 additions & 53 deletions
100
app/src/main/java/com/binay/shaw/justap/services/MyFirebaseMessagingService.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 |
---|---|---|
@@ -1,77 +1,71 @@ | ||
package com.binay.shaw.justap.services | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Intent | ||
import android.graphics.BitmapFactory | ||
import android.graphics.Color | ||
import android.media.RingtoneManager | ||
import android.os.Build | ||
import androidx.core.app.NotificationCompat | ||
import com.binay.shaw.justap.R | ||
import com.binay.shaw.justap.helper.NotificationHelper | ||
import com.binay.shaw.justap.helper.Util | ||
import com.binay.shaw.justap.helper.Logger | ||
import com.binay.shaw.justap.ui.mainScreens.MainActivity | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
|
||
|
||
class MyFirebaseMessagingService : FirebaseMessagingService() { | ||
|
||
private lateinit var notificationHelper: NotificationHelper | ||
val channelId = "notification_channel_id" | ||
val channelName = "notification_channel_name" | ||
|
||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
// Handle incoming notification | ||
try { | ||
if (remoteMessage.notification != null) { | ||
showNotification( | ||
remoteMessage.notification?.title, | ||
remoteMessage.notification?.body | ||
) | ||
} else { | ||
showNotification(remoteMessage.data["title"], remoteMessage.data["message"]) | ||
} | ||
} catch (e: Exception) { | ||
Util.log("Error: ${e.localizedMessage}") | ||
remoteMessage.notification?.let { | ||
sendNotification( | ||
it.title, it.body | ||
) | ||
} | ||
} | ||
|
||
override fun onNewToken(token: String) { | ||
super.onNewToken(token) | ||
Util.log("New token: $token") | ||
} | ||
|
||
private fun showNotification( | ||
title: String?, | ||
body: String? | ||
) { | ||
notificationHelper = NotificationHelper(this) | ||
notificationHelper.createNotificationChannel(channelId, channelName) | ||
|
||
if (title != null && body != null) { | ||
val builder = createNotificationBuilder(title, body) | ||
notificationHelper.showNotification(0, builder) | ||
} | ||
Logger.debugLog("Firebase Token: $token") | ||
FirebaseMessaging.getInstance().subscribeToTopic(APP_UPDATES) | ||
} | ||
|
||
private fun createNotificationBuilder( | ||
title: String, | ||
message: String | ||
): NotificationCompat.Builder { | ||
// Create an Intent for the notification action | ||
val intent = Intent(this, MainActivity::class.java).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
private fun sendNotification(messageTitle: String?, messageBody: String?) { | ||
val intent = Intent(this, MainActivity::class.java) | ||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
val pendingIntent = PendingIntent.getActivity( | ||
this, | ||
0, | ||
intent, | ||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT | ||
this, 0 /* Request code */, intent, | ||
PendingIntent.FLAG_IMMUTABLE | ||
) | ||
|
||
// Create a NotificationCompat.Builder object | ||
return NotificationCompat.Builder(this, channelId) | ||
.setSmallIcon(R.drawable.notification_icon) | ||
.setContentTitle(title) | ||
.setContentText(message) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
val rawBitmap = BitmapFactory.decodeResource( | ||
resources, | ||
R.drawable.logo_light | ||
) | ||
val channelId = resources.getString(R.string.default_notification_channel_id) | ||
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | ||
val notificationBuilder = NotificationCompat.Builder(this, channelId) | ||
.setSmallIcon(R.drawable.logo_light) | ||
.setLargeIcon(rawBitmap) | ||
.setColor(Color.parseColor("#f83d3a")) | ||
.setSound(defaultSoundUri) | ||
.setContentTitle(messageTitle) | ||
.setContentText(messageBody) | ||
.setAutoCancel(true) | ||
.setContentIntent(pendingIntent) | ||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val channel = NotificationChannel( | ||
channelId, | ||
resources.getString(R.string.default_notification_channel_id), | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
notificationManager.notify(0 , notificationBuilder.build()) | ||
} | ||
|
||
companion object { | ||
private const val APP_UPDATES = "app-updates" | ||
} | ||
} |
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
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