Skip to content

Commit

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

---------

Co-authored-by: Daniel Zupan <daniel.zupan@uniquare.com>
  • Loading branch information
uniquare and Daniel Zupan committed Sep 28, 2023
1 parent 9169e81 commit 66138f2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 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!!)
}
}
}
9 changes: 5 additions & 4 deletions www/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

/*!
* Module dependencies.
*/
Expand Down

0 comments on commit 66138f2

Please sign in to comment.