Android Question Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE ...

yo3ggx

Active Member
Licensed User
Longtime User
Hello,

When I run the application I get the following error:
B4X:
notification_createnotification (java line: 204)
java.lang.IllegalArgumentException: ro.yo3ggx.rxtxe: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

I'm using B4A 12 with targetSDKversion="31"

Currently the code looks like that:
B4X:
Private Sub CreateNotification(Title As String, Content As String, Icon As String, TargetActivity As Object, _
    Sound As Boolean, Vibrate As Boolean) As Notification
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim nb As NotificationBuilder
        nb.Initialize
        nb.DefaultSound = Sound
        nb.DefaultVibrate = Vibrate
        nb.ContentTitle = Title
        nb.ContentText = Content
        nb.setActivity(TargetActivity)
        nb.SmallIcon = Icon
        If p.SdkVersion >= 26 Then
            Dim ctxt As JavaObject
            ctxt.InitializeContext
            Dim manager As JavaObject
            manager.InitializeStatic("android.app.NotificationManager")
            Dim Channel As JavaObject
            Dim importance As String
            If Sound Then importance = "IMPORTANCE_DEFAULT" Else importance = "IMPORTANCE_LOW"
            Dim ChannelVisibleName As String = Application.LabelName
            Channel.InitializeNewInstance("android.app.NotificationChannel", _
                   Array("YO3GGXChannelId1", ChannelVisibleName, manager.GetField(importance)))
            manager = ctxt.RunMethod("getSystemService", Array("notification"))
            manager.RunMethod("createNotificationChannel", Array(Channel))
            Dim jo As JavaObject = nb
            jo.RunMethod("setChannelId", Array("YO3GGXChannelId1"))
        End If
        Return  nb.GetNotification
    Else
        Dim n As Notification
        n.Initialize
        n.Sound = Sound
        n.Vibrate = Vibrate
        n.Icon = Icon
        n.SetInfo(Title, Content, TargetActivity)
        Return n
    End If
End Sub

Any hint on how to solve this?

Thank you.
 
Last edited:

yo3ggx

Active Member
Licensed User
Longtime User
Trying the NB6 example as it is, but changing targetSDKversion to 31, I get the same error when clicking on any notification type.
The error is in the line
B4X:
Dim PendingIntent As Object = PendingIntentStatic.RunMethod("getActivity", Array(ctxt, Rnd(0, 0x7fffffff), in, 0))

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
nb6_createreceiverpendingintent (java line: 238)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4a.example3.nb6._createreceiverpendingintent(nb6.java:238)
    at b4a.example3.nb6._addbuttonaction(nb6.java:49)
    at b4a.example3.main._notification_withactions(main.java:738)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1084)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:1031)
    at b4a.example3.main._clv_itemclick(main.java:561)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1084)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1047)
    at b4a.example3.customlistview$ResumableSub_PanelClickHandler.resume(customlistview.java:805)
    at b4a.example3.customlistview._panelclickhandler(customlistview.java:748)
    at b4a.example3.customlistview._panel_click(customlistview.java:735)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7449)
    at android.view.View.performClickInternal(View.java:7426)
    at android.view.View.access$3700(View.java:838)
    at android.view.View$PerformClick.run(View.java:28703)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7882)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
Caused by: java.lang.IllegalArgumentException: b4a.example3: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
    at android.app.PendingIntent.checkFlags(PendingIntent.java:378)
    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:648)
    at android.app.PendingIntent.getBroadcast(PendingIntent.java:635)
    ... 33 more

So I changed the line to:
B4X:
        If SdkLevel > 30 Then
            Dim PendingIntent As Object = PendingIntentStatic.RunMethod("getActivity", Array(ctxt, Rnd(0, 0x7fffffff), in, 0x04000000))            ' FLAG_IMMUTABLE
        Else
            Dim PendingIntent As Object = PendingIntentStatic.RunMethod("getActivity", Array(ctxt, Rnd(0, 0x7fffffff), in, 0))          
        End If

No issue if I'm using NB6 library.
 
Last edited:
Upvote 0
Top