Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Android Support #1

Closed
gtokman opened this issue Feb 11, 2024 · 6 comments
Closed

Android Support #1

gtokman opened this issue Feb 11, 2024 · 6 comments
Assignees

Comments

@gtokman
Copy link
Member

gtokman commented Feb 11, 2024

Docs: https://developer.android.com/develop/ui/views/notifications

@gtokman gtokman added the help wanted Extra attention is needed label Feb 11, 2024
@vijaygojiya
Copy link
Contributor

I'm interested in contributing. Could you please assign this issue to me?

@gtokman
Copy link
Member Author

gtokman commented Feb 13, 2024

@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!

@gtokman
Copy link
Member Author

gtokman commented Feb 15, 2024

@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.

@vijaygojiya
Copy link
Contributor

@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.

@vijaygojiya
Copy link
Contributor

@gtokman, I just crafted a draft PR. Kindly review it and inform me if I'm moving in the right direction.

@gtokman
Copy link
Member Author

gtokman commented Feb 15, 2024

@vijaygojiya awesome! @liambutler-lawrence and I will take a look in a few hours. Appreciate it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants