Android Question Target sdk 33 and firebase messaging confusion

tufanv

Expert
Licensed User
Longtime User
Hello,

With target sdk 33, we need to get permission for notifications as I understand. But also @Erel said that it is not needed for foreground service notification.
I am confused here. I am using new receivers for firebasemessaging module. I send notification with php using firebase cloud messaging. Do I need to implement to request permission?
@asales said:
Only if you will show a notification.
If you don't have permission, still receive the push notification, without show the notification in the statusbar.

So it means we need to implement this if we need to show it in status bar. What I need to achieve is, I need the notification to be shown to user when the app is on background because it will be a price alert notification to user when the app is on background.

So, my .notify code is in a receiver module called firebasemessaging. Should I implement the code in this receiver module ? If I implement the code in receiver module, It is not possible to ask for permission when the app is on background so I am totally lost here. Like in the ios, when user first opened the app, I need to request permission. After that, I should be able to send messages before earlier like target sdk 31.
 
Last edited:

tufanv

Expert
Licensed User
Longtime User
If you want to show a notification then you do need to request the new permission. It cannot be done from a receiver.
Thank you. Is this a correct way to implement?
1) I created a boolean like "permissionstatus" in process global. After the haspermission recieved from the resumeable sub I set the permissionstatus = haspermission
2) In reciever module, If permissonstatus is true n.notify code.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
No. It will not be set if the app starts from the receiver.

You can use KVS to store the state.

With this method I am always getting :
Attempt to invoke virtual method 'java.lang.String anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(java.lang.String, java.lang.String[])' on a null object reference

and every user is crashing.

In fm receiver module I have:

B4X:
    If Starter.kvs.ContainsKey("haspermission") Then
        If Starter.kvs.Get("haspermission")="1" Then
            Log("Message arrived")
            Log($"Message data: ${Message.GetData}"$)
            Dim n As Notification
            n.Initialize
            n.Icon = "icon"
            n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
            n.Notify(1)
        End If
    Else
        Log("Message arrived")
        Log($"Message data: ${Message.GetData}"$)
        Dim n As Notification
        n.Initialize
        n.Icon = "icon"
        n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
        n.Notify(1)
    End If
End Sub

when i send the message, I get thousnads of reports for crashing.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I get thousnads of reports for crashing.
and you are HIDING the full error for all of them.
It is not helpful to get help
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
and you are HIDING the full error for all of them.
It is not helpful to get help
Sorry.


B4X:
Fatal Exception: java.lang.RuntimeException
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(java.lang.String, java.lang.String[])' on a null object reference
Caused by java.lang.NullPointerException
Attempt to invoke virtual method 'java.lang.String anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(java.lang.String, java.lang.String[])' on a null object reference
ct.dovizfx.keyvaluestore._vvvv5 (keyvaluestore.java:53)
ct.dovizfx.firebasemessaging._fm_messagearrived (firebasemessaging.java:57)
java.lang.reflect.Method.invoke (Method.java)

com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067)
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Sorry.


B4X:
Fatal Exception: java.lang.RuntimeException
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(java.lang.String, java.lang.String[])' on a null object reference
Caused by java.lang.NullPointerException
Attempt to invoke virtual method 'java.lang.String anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(java.lang.String, java.lang.String[])' on a null object reference
ct.dovizfx.keyvaluestore._vvvv5 (keyvaluestore.java:53)
ct.dovizfx.firebasemessaging._fm_messagearrived (firebasemessaging.java:57)
java.lang.reflect.Method.invoke (Method.java)

com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067)
This is the error for all the crashes
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(java.lang.String, java.lang.String[])' on a null object reference

Try to Log the value

B4X:
Log(Starter.kvs.Get("haspermission"))

or you may use
B4X:
Starter.kvs.GetDefault("haspermission", "0")

Edit: Are you sure KVS is initialized properly?
 
Last edited:
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
Correct me if i am wrong, but if you initialize KVS at starter service, then it is not sure that starter service will be initialized before a reciever "recieves" something. I think that is the problem.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Try to Log the value

B4X:
Log(Starter.kvs.Get("haspermission"))

or you may use
B4X:
Starter.kvs.GetDefault("haspermission", "0")

Edit: Are you sure KVS is initialized properly?
Yes I am sure. It is initialized at starter service.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Correct me if i am wrong, but if you initialize KVS at starter service, then it is not sure that starter service will be initialized before a reciever "recieves" something. I think that is the problem.
You may be right so what is the correct way to handle showing notifications on receiver module?
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
You may be right so what is the correct way to handle showing notifications on receiver module?
I think initializing an instance of KVS at reciever
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The starter-service will not be started if the app is started by a receiver.

See https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/

Notes

  • The starter service will not start before the receiver when a process starts from a receiver.
  • The CancelScheduledService keyword, which cancels services scheduled with StartServiceAt, also works with receivers scheduled with StartReceiverAt.
  • B4A services are actually made of a receiver + service. These automatic receivers are named <service>_BR. Not too important but you might encountered those in the past.
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
BTW if you don't check if hasPersmission (KVS) then no error should be displayed. If it does not has permission, no notification will be displayed. As i see your code, both circumstances do the same thing.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It doesn't have enough time to run any reading from the KVS.
You probably are right.

I for myself would store the status in a textfile. Maybe in a serialized MAP.
Should be fast enough to read the serialized map, deserialize it an get the status while a receiver is running.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
this is so complicated to show a notification.
I am still confused about this. İn iOS it asks for permission and when user dont allow notification, even if you send the notification, it doesn't arrive. İt is so simple. Here, with sdk33, we need to check the permission every time we show a notification. As I understand and Erel stated, we mustn't use the checkpermission sub in receiver module.

what if user doesn't allow notifications and we still show the notification withouy checking permission status?

- if it still arrives, system can be abused easily. This doesn't make sense.
- if it doesn't arrive, it makes sense but then why are we checking permission status everytime we show a notification?

sorry, I don't get the full picture here.
 
Upvote 0
Top