Android Question Double notification

jvrh_1

Active Member
Licensed User
Hi, I need your help again. It is very simple but I do not know my error.

I have a ESP32 that send a notification when somebody press the ring of my house
B4X:
Private Sub SendMessage(Topic() As Byte, Title() As Byte, Body() As Byte)
    'Dim BC As ByteConverter
    HttpJob.Initialize(BC.StringFromBytes(Topic))
    Dim buffer(300) As Byte 'must be large enough to hold the message payload
    Dim raf As RandomAccessFile
    raf.Initialize(buffer, True)
    WriteBytes(raf, "{""data"":{""title"":""")
    WriteBytes(raf, Title)
    WriteBytes(raf, """,""body"":""")
    WriteBytes(raf, Body)
    WriteBytes(raf, """}")
    'end of data
    WriteBytes(raf, ",""to"":""\/topics\/")
    WriteBytes(raf, Topic)
    WriteBytes(raf, """")
    WriteBytes(raf, ",""priority"": 10")
    If BC.StartsWith(Topic, "ios_") Then
        WriteBytes(raf, ",""notification"": {""title"": """)
        WriteBytes(raf, Title)
        WriteBytes(raf, """,""body"":""")
        WriteBytes(raf, Body)
        WriteBytes(raf, """, ""sound"": ""default""}")
    End If
    WriteBytes(raf, "}")
    HttpJob.AddHeader("Authorization", JoinBytes(Array("key=".GetBytes, API_KEY)))
    HttpJob.AddHeader("Content-Type", "application/json")
    Log("stack: ", StackBufferUsage, ", buffer size:", raf.CurrentPosition)
    HttpJob.Post("https://fcm.googleapis.com/fcm/send", BC.SubString2(buffer, 0, raf.CurrentPosition))
End Sub

After that, my B4A app recive the notification with this code (Firebasemessaging):

B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general")
End Sub
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    'Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub


Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
   
    Dim now As Long
    Dim HORA,FECHA As String
    DateTime.TimeFormat = "hh:mm"
    DateTime.DateFormat = "dd MMM yyyy"
    FECHA = DateTime.Date(DateTime.now)
    HORA = DateTime.Time(DateTime.Now)
   
   
    'FUNCIONA
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_DEFAULT)
    n2.Icon = "icon"
    n2.SetInfo("Llaman al timbre", "A las " & HORA & " del " & FECHA,Main )
    n2.Notify(4)
   
End Sub

Sub fm_TokenRefresh (Token As String)
    Log("TokenRefresh: " & Token)
End Sub

The problem is that I recive two notification like the attached file.
I only want one notification (the notification that shows the hour). Any idea? What is happening?
 

Attachments

  • WhatsApp Image 2023-08-26 at 14.08.44 (2).jpeg
    WhatsApp Image 2023-08-26 at 14.08.44 (2).jpeg
    122 KB · Views: 120

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

jvrh_1

Active Member
Licensed User
Looks like the automatic foreground notification. This is a thing of the past.

1. You should switch to a receiver: https://www.b4x.com/android/forum/t...s-firebase-cloud-messaging-fcm.67716/#content
2. Next year it will no longer be possible to send notifications like this. You will need to create a B4J server that receives the message from B4R and sends the notification: https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/
Hi, Erel. Thanks for you help.

1. I think that I am doing it. Look my previous code. I have a service module called FirebaseMessaging with the same code that appears in your link.

2. I want to do it but, when I open B4J app, appears this error (attached image). Finally, I do not know If I understand ok...

  • a) I have not to change anything in the B4R. Is it correct?
  • b) In B4A I have to download the new google-services.json (after setting console) and put it in the project folder. I have reviewed the code and it is very similar, but in the old app FirebaseMessaging is a service and in the new app it is a class module, but the rest is equal. Is it correct?
  • c) So, I have to found a hosting for my B4J (I have not got a hosting now) and run it. Is it correct?
Thanks again.
 

Attachments

  • error.png
    error.png
    71.1 KB · Views: 117
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi, Erel. Thanks for you help.

1. I think that I am doing it. Look my previous code. I have a service module called FirebaseMessaging with the same code that appears in your link.

2. I want to do it but, when I open B4J app, appears this error (attached image). Finally, I do not know If I understand ok...

  • a) I have not to change anything in the B4R. Is it correct?
  • b) In B4A I have to download the new google-services.json (after setting console) and put it in the project folder. I have reviewed the code and it is very similar, but in the old app FirebaseMessaging is a service and in the new app it is a class module, but the rest is equal. Is it correct?
  • c) So, I have to found a hosting for my B4J (I have not got a hosting now) and run it. Is it correct?
Thanks again.
The POST_NOTIFICATIONS permission is only available in Android 13+ (targetSDK 33).

- Colin.
 
Upvote 0
Top