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

Commit

Permalink
feat: Firebase setup - Added Android native method for requesting per…
Browse files Browse the repository at this point in the history
…missions and fetching device tokens.
  • Loading branch information
vijaygojiya committed Feb 15, 2024
1 parent d16cd21 commit ee72d6c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 10 deletions.
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,7 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation(platform("com.google.firebase:firebase-bom:32.7.2"))
implementation "com.google.firebase:firebase-messaging"
}

15 changes: 13 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.candlefinance.push">
</manifest>
package="com.candlefinance.push">
<application>
<service
android:name=".PushFirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
</application>
</manifest>
67 changes: 66 additions & 1 deletion android/src/main/java/com/candlefinance/push/PushModule.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.candlefinance.push

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.modules.core.PermissionAwareActivity
import com.facebook.react.modules.core.PermissionListener
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging


class PushModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
Expand All @@ -19,6 +29,61 @@ class PushModule(reactContext: ReactApplicationContext) :
promise.resolve(a * b)
}



@ReactMethod
fun requestPermissions(promise: Promise) {
val context = reactApplicationContext.baseContext

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) ==
PackageManager.PERMISSION_GRANTED
) {
promise
.resolve(true)
} else {
val activity = reactApplicationContext.currentActivity

if (activity is PermissionAwareActivity) {
val currentRequestCode = 83834

val listener = PermissionListener { requestCode: Int, _: Array<String>, grantResults: IntArray ->
if (requestCode == currentRequestCode) {
val permissionStatus = if (grantResults.isNotEmpty()) grantResults[0] else PackageManager.PERMISSION_DENIED
promise.resolve(permissionStatus== PackageManager.PERMISSION_GRANTED)
return@PermissionListener true
}
return@PermissionListener false
}

// Replace this with the appropriate permission for push notifications
activity.requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), currentRequestCode, listener)
} else {
promise.reject("NO_ACTIVITY", "No PermissionAwareActivity was found! Make sure the app has launched before calling this function.")
}
}

} else {
promise.resolve(true)
}
}

@ReactMethod
fun getToken(promise: Promise) {
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w("getTokenError", "Fetching FCM registration token failed", task.exception)
promise.reject(task.exception)
return@OnCompleteListener
}

// Get new FCM registration token
val token = task.result
promise.resolve(token)
})
}

companion object {
const val NAME = "Push"
}
Expand Down
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
apply plugin: "com.google.gms.google-services"

/**
* This is the configuration block to customize your React Native Android app.
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:name=".MainApplication"
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">PushExample</string>
<string name="default_notification_channel_id">PushRemoteNotification</string>
</resources>
1 change: 1 addition & 0 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ buildscript {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
classpath("com.google.gms:google-services:4.4.1")
}
}

Expand Down
13 changes: 13 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ export default function App() {
title="Register for Token"
onPress={() => push.registerForToken()}
/>
<Button
title="Get FCM Token"
onPress={() =>
push
.getToken()
.then((token) => {
console.log('Token :', token);
})
.catch((e) => {
console.log('Error while fetching deivce FCM token', e);
})
}
/>
</View>
);
}
Expand Down
21 changes: 14 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ class Push {
private bridge?: NativeEventEmitter;

public constructor() {
const module = NativeModules.Push;
this.module = module;
if (Platform.OS === 'ios') {
const module = NativeModules.Push;
this.module = module;
this.bridge = new NativeEventEmitter(module);
}
}

public async requestPermissions(): Promise<boolean> {
if (Platform.OS === 'ios') {
const result = await this.module.requestPermissions();
return result;
}
return false;
// if (Platform.OS === 'ios') {
const result = await this.module.requestPermissions();
return result;
// }
// return false;
}

public async registerForToken(): Promise<boolean> {
Expand All @@ -70,6 +70,13 @@ class Push {
return false;
}

public async getToken(): Promise<boolean> {
if (Platform.OS === 'android') {
return this.module.getToken();
}
return false;
}

public async isRegisteredForRemoteNotifications(): Promise<boolean> {
if (Platform.OS === 'ios') {
return this.module.isRegisteredForRemoteNotifications();
Expand Down

0 comments on commit ee72d6c

Please sign in to comment.