Skip to content

Commit

Permalink
fix(android): Ask for POST_NOTIFICATIONS permission if necessary (hav…
Browse files Browse the repository at this point in the history
…esource#238)

* fix(android): Ask for POST_NOTIFICATIONS permission if necessary

---------

Co-authored-by: Daniel Zupan <daniel.zupan@uniquare.com>
  • Loading branch information
2 people authored and VedantS2000 committed Dec 27, 2023
1 parent 4cf3d8e commit b0e63a7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="${applicationId}.permission.PushHandlerActivity"/>
<permission android:name="${applicationId}.permission.PushHandlerActivity" android:protectionLevel="signature"></permission>
</config-file>
Expand Down
55 changes: 53 additions & 2 deletions src/android/com/adobe/phonegap/push/PushPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.adobe.phonegap.push

import android.Manifest
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ContentResolver
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources.NotFoundException
import android.media.AudioAttributes
import android.net.Uri
Expand Down Expand Up @@ -37,12 +39,15 @@ class PushPlugin : CordovaPlugin() {
const val PREFIX_TAG: String = "cordova-plugin-push"
private const val TAG: String = "$PREFIX_TAG (PushPlugin)"

private const val REQ_CODE_INITIALIZE_PLUGIN = 0

/**
* Is the WebView in the foreground?
*/
var isInForeground: Boolean = false

private var pushContext: CallbackContext? = null
private var pluginInitData: JSONArray? = null
private var gWebView: CordovaWebView? = null
private val gCachedExtras = Collections.synchronizedList(ArrayList<Bundle>())

Expand Down Expand Up @@ -433,11 +438,16 @@ class PushPlugin : CordovaPlugin() {
// Better Logging
fun formatLogMessage(msg: String): String = "Execute::Initialize: ($msg)"

pushContext = callbackContext
pluginInitData = data;

var hasPermission = checkForPostNotificationsPermission()
if (!hasPermission)
return

cordova.threadPool.execute(Runnable {
Log.v(TAG, formatLogMessage("Data=$data"))

pushContext = callbackContext

val sharedPref = applicationContext.getSharedPreferences(
PushConstants.COM_ADOBE_PHONEGAP_PUSH,
Context.MODE_PRIVATE
Expand Down Expand Up @@ -600,6 +610,22 @@ class PushPlugin : CordovaPlugin() {
})
}

private fun checkForPostNotificationsPermission(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!PermissionHelper.hasPermission(this, Manifest.permission.POST_NOTIFICATIONS))
{
PermissionHelper.requestPermission(
this,
REQ_CODE_INITIALIZE_PLUGIN,
Manifest.permission.POST_NOTIFICATIONS
)
return false
}
}

return true
}

private fun executeActionUnregister(data: JSONArray, callbackContext: CallbackContext) {
// Better Logging
fun formatLogMessage(msg: String): String = "Execute::Unregister: ($msg)"
Expand Down Expand Up @@ -872,4 +898,29 @@ class PushPlugin : CordovaPlugin() {
FirebaseMessaging.getInstance().unsubscribeFromTopic(it)
}
}

override fun onRequestPermissionResult(
requestCode: Int,
permissions: Array<out String>?,
grantResults: IntArray?
) {
super.onRequestPermissionResult(requestCode, permissions, grantResults)

for (r in grantResults!!) {
if (r == PackageManager.PERMISSION_DENIED) {
pushContext?.sendPluginResult(
PluginResult(
PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION,
"Permission to post notifications was denied by the user"
)
)
return
}
}

if (requestCode == REQ_CODE_INITIALIZE_PLUGIN)
{
executeActionInitialize(pluginInitData!!, pushContext!!)
}
}
}

0 comments on commit b0e63a7

Please sign in to comment.