I’m developing android application using flutter.
I want to get ‘default message applicaion permission’ for my app.
I tried many ways but I encountered below notifications
2024-07-23 15:22:04.278 25394-25394 Role com....android.permissioncontroller I com.test.testproject not qualified for android.app.role.SMS due to missing RequiredComponent{mIntentFilterData=IntentFilterData{mAction='android.provider.Telephony.WAP_PUSH_DELIVER', mCategories='[]', mDataScheme='null', mDataType='application/vnd.wap.mms-message'}, mMinTargetSdkVersion=1, mFlags='0', mPermission='android.permission.BROADCAST_WAP_PUSH', mQueryFlags=0, mMetaData=[]}
2024-07-23 15:22:04.278 25394-25394 RequestRoleActivity com....android.permissioncontroller W Application doesn't qualify for role, role: android.app.role.SMS, package: com.test.testproject
I don’t know what is the problem of this.
I already typed permission code in AndroidManifest.xml as below
(and I also attached my related code)
- AndroidManifext.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.BROADCAST_WAP_PUSH"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:label="testproject"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<!-- <category android:name="android.app.role.SMS"/>-->
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.SENDTO" />-->
<!-- <category android:name="android.intent.category.DEFAULT" />-->
<!-- <data android:scheme="smsto" />-->
<!-- </intent-filter>-->
</activity>
<!-- <activity-->
<!-- android:name=".PermissionHandler"-->
<!-- android:exported="true">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN"/>-->
<!-- <category android:name="android.intent.category.LAUNCHER"/>-->
<!-- <category android:name="android.app.role.SMS"/>-->
<!-- </intent-filter>-->
<!-- </activity>-->
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".RespondViaMessageService"
android:exported = "true"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data scheme="sms" />
<data scheme="smsto" />
<data scheme="mms" />
<data scheme="mmsto" />
</intent-filter>
</service>
<!-- BroadCast Receiver that listens for incoming SMS messages-->
<receiver android:name=".SMSReceiver"
android:exported = "true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_DELIVER"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".WapPushReceiver"
android:exported = "true"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER"/>
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"/>
<data mimeType="application/vnd.wap.mms-message"/>
</intent-filter>
</receiver>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
- MainActivity.kt
package com.test.testproject
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragmentActivity
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.test.testproject.PermissionHandler
class MainActivity: FlutterFragmentActivity() {
private val CHANNEL = "com.test.test"
private lateinit var permissionHandler: PermissionHandler
companion object {
const val REQUEST_CAMERA_PERMISSION = 101
const val REQUEST_READ_PHONE_STATE_PERMISSION = 102
const val REQUEST_DEFAULT_SMS_APP_PERMISSION = 103
const val REQUEST_READ_SMS_PERMISSION = 104
const val REQUEST_RECEIVE_SMS_PERMISSION = 105
const val REQUEST_SEND_SMS_PERMISSION = 106
const val REQUEST_READ_CONTACTS_PERMISSION = 107
const val REQUEST_RECEIVE_WAP_PUSH_PERMISSION = 108
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
permissionHandler = PermissionHandler(this)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"requestCameraPermission" -> permissionHandler.requestPermission(Manifest.permission.CAMERA, REQUEST_CAMERA_PERMISSION, result)
"requestReadPhoneStatePermission" -> permissionHandler.requestPermission(Manifest.permission.READ_PHONE_STATE, REQUEST_READ_PHONE_STATE_PERMISSION, result)
"requestDefaultSmsAppPermission" -> permissionHandler.requestDefaultSmsApp(result)
"request_READ_SMS" -> permissionHandler.requestPermission(Manifest.permission.READ_SMS, REQUEST_READ_SMS_PERMISSION, result)
"request_RECEIVE_SMS" -> permissionHandler.requestPermission(Manifest.permission.RECEIVE_SMS, REQUEST_RECEIVE_SMS_PERMISSION, result)
"request_SEND_SMS" -> permissionHandler.requestPermission(Manifest.permission.SEND_SMS, REQUEST_SEND_SMS_PERMISSION, result)
"request_READ_CONTACTS" -> permissionHandler.requestPermission(Manifest.permission.READ_CONTACTS, REQUEST_READ_CONTACTS_PERMISSION, result)
"request_RECEIVE_WAP_PUSH" ->permissionHandler.requestPermission(Manifest.permission.RECEIVE_WAP_PUSH, REQUEST_RECEIVE_WAP_PUSH_PERMISSION, result)
//T 스팸 필터링의 권한 -> 전화 걸고 관리, SMS 전송 보기, 알림 허용
else -> result.notImplemented()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permissionHandler.onRequestPermissionsResult(requestCode, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
permissionHandler.onActivityResult(requestCode, resultCode)
}
}
- PermissionHandler.kt
package com.test.testproject
import android.Manifest
import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.app.role.RoleManager
import android.os.Bundle
import android.content.pm.PackageManager
import android.provider.Telephony
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.embedding.android.FlutterFragmentActivity
class PermissionHandler(private val activity: FlutterFragmentActivity) {
private var result: MethodChannel.Result? = null
fun requestPermission(permission: String, requestCode: Int, result: MethodChannel.Result) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, arrayOf(permission), requestCode)
this.result = result
} else {
result.success(true)
}
}
fun requestDefaultSmsApp(result: MethodChannel.Result) {
this.result = result
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val roleManager = activity.getSystemService(RoleManager::class.java)
if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
Log.d("PermissionHandler", "Already the default SMS app")
result.success(true)
} else {
Log.d("PermissionHandler", "Requesting ROLE_SMS")
val roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS)
logIntentDetails(roleRequestIntent)
activity.startActivityForResult(roleRequestIntent, 103)
}
} else {
val defaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(activity)
if (defaultSmsPackage != activity.packageName) {
Log.d("PermissionHandler", "Requesting ACTION_CHANGE_DEFAULT")
val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, activity.packageName)
activity.startActivityForResult(intent, 103)
} else {
Log.d("PermissionHandler", "Already the default SMS app")
result.success(true)
}
}
}
fun logIntentDetails(intent: Intent) {
Log.d("IntentDetails", "Action: ${intent.action}")
Log.d("IntentDetails", "Data: ${intent.data}")
Log.d("IntentDetails", "Categories: ${intent.categories?.joinToString()}")
Log.d("IntentDetails", "Type: ${intent.type}")
Log.d("IntentDetails", "Scheme: ${intent.scheme}")
Log.d("IntentDetails", "Package: ${intent.`package`}")
Log.d("IntentDetails", "Component: ${intent.component}")
Log.d("IntentDetails", "Flags: ${intent.flags}")
Log.d("IntentDetails", "Extras: ${intent.extras}")
intent.extras?.let { extras ->
logBundleDetails("Extras", extras)
}
}
fun logBundleDetails(prefix: String, bundle: Bundle) {
for (key in bundle.keySet()) {
val value = bundle.get(key)
Log.d("IntentDetails", "value is " + value.toString());
when (value) {
is Bundle -> {
Log.d("IntentDetails", "$prefix[$key]: Bundle")
logBundleDetails("$prefix[$key]", value)
}
is Array<*> -> {
Log.d("IntentDetails", "$prefix[$key]: ${value.contentDeepToString()} (${value.javaClass.componentType?.name}[])")
}
is ArrayList<*> -> {
Log.d("IntentDetails", "$prefix[$key]: $value (ArrayList<${value.firstOrNull()?.javaClass?.name}>)")
}
else -> {
Log.d("IntentDetails", "$prefix[$key]: $value (${value?.javaClass?.name})")
}
}
}
}
fun onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
result?.success(true)
} else {
result?.success(false)
}
result = null // Reset result to avoid multiple calls
}
fun onActivityResult(requestCode: Int, resultCode: Int) {
Log.d("PermissionHandler", "onActivityResult: requestCode=$requestCode, resultCode=$resultCode")
if (requestCode == 103) {
if (resultCode == Activity.RESULT_OK) {
result?.success(true)
} else {
result?.success(false)
}
result = null // Reset result to avoid multiple calls
}
}
}
- RespondViaMessageService.kt
package com.test.testproject
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
class RespondViaMessageService : Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent != null && intent.action == "android.intent.action.RESPOND_VIA_MESSAGE") {
val uri = intent.data
Log.d("RespondViaMessageService", "Received RESPOND_VIA_MESSAGE for URI: $uri")
// Handle the respond via message action
}
return START_NOT_STICKY
}
}
5.SmsReceiver.kt
package com.test.testproject
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.SmsMessage
import android.util.Log
class SMSReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent != null && intent.action == "android.provider.Telephony.SMS_RECEIVED") {
val bundle = intent.extras
if (bundle != null) {
val pdus = bundle["pdus"] as Array<*>
for (pdu in pdus) {
val message = SmsMessage.createFromPdu(pdu as ByteArray)
val msg = message.displayMessageBody
val sender = message.displayOriginatingAddress
Log.d("SMSReceiver", "Received SMS from $sender: $msg")
}
}
}
}
}
6.WapPushReceiver.kt
package com.test.testproject
import android.content.BroadcastReceiver
import android.content.Context
import android.provider.Telephony
import android.content.Intent
import android.util.Log
class WapPushReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// if (Telephony.Sms.Intents.WAP_PUSH_DELIVER_ACTION == intent.action) {
// // MMS 수신 처리 로직 구현
// Log.d("MyWapPushReceiver", "WAP Push message received")
// // 추가 처리 로직을 여기에 작성하세요.
// }
if (intent != null && intent.action == "android.provider.Telephony.WAP_PUSH_DELIVER_ACTION") {
val mimeType = intent.type
if ("application/vnd.wap.mms-message" == mimeType) {
// Process the MMS message here
Log.d("WapPushReceiver", "Received WAP PUSH: $mimeType")
}
}
}
}
I want to get prompt for requesting default messanger app setting.
What is the problem in upper code?