-
Notifications
You must be signed in to change notification settings - Fork 3
Android Support #1
Comments
I'm interested in contributing. Could you please assign this issue to me? |
@vijaygojiya, I appreciate the help! We were going to use FCM: https://firebase.google.com/docs/cloud-messaging I'd also ensure you don't use the legacy FCM; they depreciate that in a few months. Here's a basic class encapsulating the logic for push notifications, tailored for Firebase Cloud Messaging (FCM): class PushNotificationManager(private val context: Context) {
private val TAG = "PushNotificationManager"
// Replace with your Firebase project info
private val fcmSenderId = "YOUR_FCM_SENDER_ID"
// Channel IDs
private val generalChannelId = "general_notifications"
// Notification builder function
private fun buildNotification(title: String, body: String, data: Map<String, String>): NotificationCompat.Builder {
val channel = NotificationChannel(generalChannelId, "General Notifications", NotificationManager.IMPORTANCE_HIGH)
channel.description = "Notifications about app updates, news, and offers."
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.createNotificationChannel(channel)
return NotificationCompat.Builder(context, generalChannelId)
.setSmallIcon(R.drawable.ic_notification) // Replace with your app icon
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setData(Bundle(data)) // Add data for notification actions
// Add notification actions if needed (e.g., reply, open specific activity)
}
// Request permission for notifications
fun requestPermission() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECEIVE_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
context as Activity,
arrayOf(Manifest.permission.RECEIVE_NOTIFICATIONS),
PERMISSION_REQUEST_CODE
)
}
}
// Get the device token
fun getToken(): String? {
val token = FirebaseInstanceId.getInstance().token
// Send token to your server for storage and association with the user
Log.d(TAG, "Device token: $token")
return token
}
// Handle received notifications
fun handleNotification(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
val title = data["title"] ?: ""
val body = data["body"] ?: ""
val notification = buildNotification(title, body, data)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(NOTIFICATION_ID, notification.build())
}
// Handle notification actions (add methods as needed)
fun handleNotificationAction(intent: Intent) {
val extras = intent.extras
// Extract action data and handle accordingly
}
companion object {
private const val PERMISSION_REQUEST_CODE = 100
private const val NOTIFICATION_ID = 1
}
} If I can be helpful, let me know! |
@vijaygojiya We are adding Android support next week. Can you open a PR by Monday? I'm happy to review or use anything you started as the baseline. |
Alright, I've successfully completed the Firebase setup,request for notification permission, and get the FCM device token. |
@gtokman, I just crafted a draft PR. Kindly review it and inform me if I'm moving in the right direction. |
@vijaygojiya awesome! @liambutler-lawrence and I will take a look in a few hours. Appreciate it. |
Docs: https://developer.android.com/develop/ui/views/notifications
The text was updated successfully, but these errors were encountered: