Skip to content

Commit

Permalink
fcm class update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Idrees committed Aug 2, 2022
1 parent ab62b61 commit a003653
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Do not forget to add internet permission in manifest if already not present
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
Create Firebase project, add you application ID init, download your google-services.json file and place it in app folder.
### For FCM Notifications, initialize it in onCreate() Method of Application class :
```kotlin
val fcm = FirebaseFCM(this)
Expand Down
130 changes: 123 additions & 7 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,149 @@
{
"project_info": {
"project_number": "816941791543",
"project_id": "newone-33895",
"storage_bucket": "newone-33895.appspot.com"
"project_number": "744390342276",
"project_id": "fir-e967c",
"storage_bucket": "fir-e967c.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:816941791543:android:57ade9d363823c4c2902b2",
"mobilesdk_app_id": "1:744390342276:android:ac69594aa48e365aee34d1",
"android_client_info": {
"package_name": "ai.bom.firebase"
}
},
"oauth_client": [
{
"client_id": "816941791543-mv4km02uaquvd4t4n424h5mbcr8smkla.apps.googleusercontent.com",
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyB09QIqgLt95O5uQjyIk0duflBS_IwisUM"
"current_key": "AIzaSyCIpEz4fDK_LI2FCj6eJvTY2jQme1dzzws"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "816941791543-mv4km02uaquvd4t4n424h5mbcr8smkla.apps.googleusercontent.com",
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:744390342276:android:d471528021ee286dee34d1",
"android_client_info": {
"package_name": "com.all.video.downloader.fast.downloading.video.saver.browser"
}
},
"oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCIpEz4fDK_LI2FCj6eJvTY2jQme1dzzws"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:744390342276:android:dbe485ec01889a00ee34d1",
"android_client_info": {
"package_name": "com.app.remoteconfigdemo"
}
},
"oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCIpEz4fDK_LI2FCj6eJvTY2jQme1dzzws"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:744390342276:android:e2fc5fdc69783cf9ee34d1",
"android_client_info": {
"package_name": "com.app.remoteconfigdemo.debug"
}
},
"oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCIpEz4fDK_LI2FCj6eJvTY2jQme1dzzws"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:744390342276:android:785eb52588d59bc7ee34d1",
"android_client_info": {
"package_name": "smart.app.lock.master.locker.private.pin.pattern.safe.secure"
}
},
"oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCIpEz4fDK_LI2FCj6eJvTY2jQme1dzzws"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "744390342276-juc53gc7jmnb4tvnturfoh9mveh0oacl.apps.googleusercontent.com",
"client_type": 3
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class FirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.i(TAG, "onMessageReceived: ")


//Data available
if (remoteMessage.data.isNotEmpty()) {
Log.d(TAG, "Message data payload: ${remoteMessage.data}")
Expand All @@ -45,12 +44,12 @@ class FirebaseMessagingService : FirebaseMessagingService() {
val packageName = remoteMessage.data["app_url"]?.split("=")?.get(1)

//Check already installed app
if (packageName != null) {
val alreadyInstalled = isAppInstalled(packageName, this)
if (alreadyInstalled) return
if (!packageName.isNullOrEmpty()) {
// val alreadyInstalled = isAppInstalled(packageName)
// if (alreadyInstalled) return

//send notification
if (icon == null || title == null || shortDesc == null) {
if (icon.isNullOrEmpty() || title.isNullOrEmpty() || shortDesc.isNullOrEmpty()) {
return
} else {
Handler(this.mainLooper).post {
Expand All @@ -70,26 +69,27 @@ class FirebaseMessagingService : FirebaseMessagingService() {
longDesc: String?,
storePackage: String
) {
val alreadyInstalled = isAppInstalled(storePackage)
Log.i(TAG, "alreadyInstalled: $alreadyInstalled, $longDesc")

//Open PlayStore
val intent = try {
val intent = if (alreadyInstalled) {
packageManager.getLaunchIntentForPackage(storePackage)
} else try {
Intent(Intent.ACTION_VIEW, Uri.parse("market:https://details?id=$storePackage"))
} catch (e: ActivityNotFoundException) {
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$storePackage")
)
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
} else {
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Log.i(TAG, "longDesc: $longDesc")

//Make Remote Views For text
//Remote Views
val remoteViews = RemoteViews(packageName, R.layout.notification_view)
remoteViews.setTextViewText(R.id.tv_title, title)
remoteViews.setTextViewText(R.id.tv_short_desc, shortDesc)
Expand Down Expand Up @@ -126,27 +126,23 @@ class FirebaseMessagingService : FirebaseMessagingService() {
//Set Images into remoteViews
Picasso.get().load(icon)
.into(remoteViews, R.id.iv_icon, notificationID, notificationBuilder.build())
if (image != null) {
if (!image.isNullOrEmpty()) {
remoteViews.setViewVisibility(R.id.iv_feature, View.VISIBLE)
Picasso.get().load(image)
.into(remoteViews, R.id.iv_feature, notificationID, notificationBuilder.build())
}
}

private fun isAppInstalled(uri: String, context: Context): Boolean {
val pm = context.packageManager
private fun isAppInstalled(packageName: String): Boolean {
return try {
val applicationInfo = pm.getApplicationInfo(uri, 0)
val applicationInfo = packageManager.getApplicationInfo(packageName, 0)
applicationInfo.enabled
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
private val seed = AtomicInteger()

private fun getNextInt(): Int {
return seed.incrementAndGet()
}
private fun getNextInt() = AtomicInteger().incrementAndGet()

override fun onNewToken(p0: String) {

Expand Down

0 comments on commit a003653

Please sign in to comment.