Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): support targetSdkVersion >= 31 (Android 12) #185

Merged
merged 6 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service android:name="com.adobe.phonegap.push.PushInstanceIDListenerService" android:exported="true" >
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</config-file>

<config-file target="config.xml" parent="/*">
Expand All @@ -69,7 +63,7 @@
</config-file>

<preference name="ANDROIDX_CORE_VERSION" default="1.6.+"/>
<preference name="FCM_VERSION" default="18.+"/>
<preference name="FCM_VERSION" default="23.+"/>

<framework src="androidx.core:core:$ANDROIDX_CORE_VERSION" />
<framework src="me.leolin:ShortcutBadger:1.1.22@aar"/>
Expand All @@ -79,7 +73,6 @@
<source-file src="src/android/com/adobe/phonegap/push/PushConstants.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushHandlerActivity.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/BackgroundHandlerActivity.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushInstanceIDListenerService.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushPlugin.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/BackgroundActionButtonHandler.kt" target-dir="java/com/adobe/phonegap/push/"/>
<source-file src="src/android/com/adobe/phonegap/push/PushDismissedHandler.kt" target-dir="java/com/adobe/phonegap/push/"/>
Expand Down
36 changes: 30 additions & 6 deletions src/android/com/adobe/phonegap/push/FCMService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class FCMService : FirebaseMessagingService() {

private val messageMap = HashMap<Int, ArrayList<String?>>()

private val FLAG_MUTABLE = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_MUTABLE
} else {
0
}
private val FLAG_IMMUTABLE = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE
} else {
0
}

/**
* Get the Application Name from Label
*/
Expand All @@ -62,6 +73,19 @@ class FCMService : FirebaseMessagingService() {
MODE_PRIVATE
)

/**
* Called when a new token is generated, after app install or token changes.
*
* @param token
*/
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d(TAG, "Refreshed token: $token")

// TODO: Implement this method to send any registration to your app's servers.
//sendRegistrationToServer(token);
}

/**
* Set Notification
* If message is empty or null, the message list is cleared.
Expand Down Expand Up @@ -448,7 +472,7 @@ class FCMService : FirebaseMessagingService() {
this,
requestCode,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
)
val dismissedNotificationIntent = Intent(
this,
Expand All @@ -467,7 +491,7 @@ class FCMService : FirebaseMessagingService() {
this,
requestCode,
dismissedNotificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT
PendingIntent.FLAG_CANCEL_CURRENT or FLAG_IMMUTABLE
)

val mBuilder: NotificationCompat.Builder =
Expand Down Expand Up @@ -676,7 +700,7 @@ class FCMService : FirebaseMessagingService() {
this,
uniquePendingIntentRequestCode,
intent,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or FLAG_MUTABLE
)
} else {
Log.d(TAG, "push receiver for notId $notId")
Expand All @@ -685,7 +709,7 @@ class FCMService : FirebaseMessagingService() {
this,
uniquePendingIntentRequestCode,
intent,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or FLAG_MUTABLE
)
}
}
Expand All @@ -696,7 +720,7 @@ class FCMService : FirebaseMessagingService() {
pIntent = PendingIntent.getActivity(
this, uniquePendingIntentRequestCode,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
)
}

Expand All @@ -706,7 +730,7 @@ class FCMService : FirebaseMessagingService() {
pIntent = PendingIntent.getBroadcast(
this, uniquePendingIntentRequestCode,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
)
}
}
Expand Down

This file was deleted.

36 changes: 23 additions & 13 deletions src/android/com/adobe/phonegap/push/PushPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import android.provider.Settings
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.google.firebase.iid.FirebaseInstanceId
import com.google.android.gms.tasks.Tasks
import com.google.firebase.messaging.FirebaseMessaging
import me.leolin.shortcutbadger.ShortcutBadger
import org.apache.cordova.*
Expand All @@ -25,6 +25,7 @@ import org.json.JSONException
import org.json.JSONObject
import java.io.IOException
import java.util.*
import java.util.concurrent.ExecutionException

/**
* Cordova Plugin Push
Expand Down Expand Up @@ -442,7 +443,6 @@ class PushPlugin : CordovaPlugin() {
Context.MODE_PRIVATE
)
var jo: JSONObject? = null
var token: String? = null
var senderID: String? = null

try {
Expand All @@ -461,18 +461,21 @@ class PushPlugin : CordovaPlugin() {
Log.v(TAG, formatLogMessage("JSONObject=$jo"))
Log.v(TAG, formatLogMessage("senderID=$senderID"))

try {
token = FirebaseInstanceId.getInstance().token
} catch (e: IllegalStateException) {
Log.e(TAG, formatLogMessage("Firebase Token Exception ${e.message}"))
}

if (token == null) {
val token = try {
try {
token = FirebaseInstanceId.getInstance().getToken(senderID, PushConstants.FCM)
} catch (e: IllegalStateException) {
Log.e(TAG, formatLogMessage("Firebase Token Exception ${e.message}"))
Tasks.await(FirebaseMessaging.getInstance().token)
} catch (e: ExecutionException) {
throw e.cause ?: e
}
} catch (e: IllegalStateException) {
Log.e(TAG, formatLogMessage("Firebase Token Exception ${e.message}"))
null
} catch (e: ExecutionException) {
Log.e(TAG, formatLogMessage("Firebase Token Exception ${e.message}"))
null
} catch (e: InterruptedException) {
Log.e(TAG, formatLogMessage("Firebase Token Exception ${e.message}"))
null
}

if (token != "") {
Expand Down Expand Up @@ -612,7 +615,11 @@ class PushPlugin : CordovaPlugin() {
if (topics != null) {
unsubscribeFromTopics(topics)
} else {
FirebaseInstanceId.getInstance().deleteInstanceId()
try {
Tasks.await(FirebaseMessaging.getInstance().deleteToken())
} catch (e: ExecutionException) {
throw e.cause ?: e
}
Log.v(TAG, formatLogMessage("UNREGISTER"))

/**
Expand Down Expand Up @@ -640,6 +647,9 @@ class PushPlugin : CordovaPlugin() {
} catch (e: IOException) {
Log.e(TAG, formatLogMessage("IO Exception ${e.message}"))
callbackContext.error(e.message)
} catch (e: InterruptedException) {
Log.e(TAG, formatLogMessage("Interrupted ${e.message}"))
callbackContext.error(e.message)
}
}
}
Expand Down