Android Question Retrive the notification id when app is closed

uniplan

Active Member
Licensed User
Longtime User
Hi, i'm working with push notification.

I setting the redirct activity when i click on the notify, and i pass the notify id.
In this activity i retrive the notify from my db and show this.
My problem is when the app is closed, my app crash and close.
How i can get the notification id ? where i can get this ? Into Main Activity resume?

Thank you for help.
 

KMatle

Expert
Licensed User
Longtime User
My problem is when the app is closed, my app crash and close.

What does that mean? Please post error messages (logs).

How i can get the notification id ?

Do you set the notification by code or is it set by FCM automatically?

Are you talkind about the notification id or some other id you've created or do you mean the FCM message id?
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
Sometimes I do not have any error messages because my application just hangs in release mode, not debugging. I think the problem is how I can handle the notification when the application is closed. I do not know the application cycle when it is closed and when it's in the background. Now my notification goes from the FirebaseMessaging service to Main ... and the main redirection to a specified activity .... (I refer to the other id that I passed in notification)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I think the problem is how I can handle the notification when the application is closed

It's handled automatically but please check if your app is not stopped. Do you have a Huawei phone? Please set your app to "protected" in energy savings. Even WhatsApp was stopped and did not receive FCM messages when I got the phone 2 years ago. Took me several hours to check that.

Please show the code (without credentials) how you send the message.
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
no, i have samsung phone.
Now... when i make ExitApplication (from activity where i read Push message), the application return on the same page and not close (Only in release mode)


my code push is this:

Sub Process_Globals
Private const API_KEY As String = "my_key"
End Sub

Sub AppStart (Args() As String)
SendMessage("general", "Benvenuto2", "Hello2!!!!")

StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
Dim Job As HttpJob
Job.Initialize("fcm", Me)
Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
Dim data As Map = CreateMap("title": Title, "body": Body,"id_notifica":"120") // 120 is my id
m.Put("data": data)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
Job.GetRequest.SetContentType("application/json")
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub


Private Sub SendMessageToSingleDevice(Devtoken As String, datastring As String)

Dim Job As HttpJob
Job.Initialize("SendMessage", Me)

Dim m As Map = CreateMap("to": $"${Devtoken}"$)
Dim noti As Map = CreateMap("body":"Notification ", "title":"New message!")
Dim data As Map = CreateMap("data": datastring)
m.Put("notification", noti)
m.Put("data", data)
m.Put("content_available": True)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.Tag=jg
Log(jg.ToString)
Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
Job.GetRequest.SetContentType("application/json;charset=UTF-8")
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)

End Sub

Sub JobDone(job As HttpJob)
Log(job)
If job.Success Then
Log(job.GetString)
End If
job.Release
StopMessageLoop '<-- non ui app only
End Sub
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
my cicle of notification is this:

when i receive the message in firebaseMessaging make this:


Dim titolo_push As String = Message.GetData.Get("title")
Dim messaggio_push As String =Message.GetData.Get("body")
Dim id_notifica As String = Message.GetData.Get("id_notifica")

n.SetInfo2(titolo_push,messaggio_push,id_notifica,Main)
n.Notify(id_notifica)
n.Vibrate=True
n.AutoCancel=True

//Save on sqlite the message
Try

If Main.SQL1.IsInitialized=False Then
Main.SQL1.Initialize(app_cfg.percorso_db,app_cfg.nome_db,True)
End If

Dim str_sql As String = "insert into notifiche_push (id_notifica,titolo,messaggio,data_ricezione) values (?,?,?,?)"
Main.SQL1.ExecNonQuery2(str_sql,Array As String(id_notifica,titolo_push,messaggio_push,DateTime.Now))

Catch
Log(LastException)
fp.scrivi_log(LastException.Message,"FIREBASEMESSAGING","MESSAGE_ARRIVED")
End Try


and this is the activity resume of main:


Sub Activity_Resume


Try
Dim in As Intent
in = Activity.GetStartingIntent
Dim id_notifica As Int= 0

If in.HasExtra("Notification_Tag") Then
id_notifica = in.GetExtra("Notification_Tag")

Dim cursor1 As Cursor
Dim str_sql As String = "select * from notifiche_push where id_notifica=?"

If SQL1.IsInitialized=False Then
SQL1.Initialize(percorso_db,nome_db,True)
End If

cursor1 =SQL1.ExecQuery2(str_sql,Array As String(id_notifica))

Dim message_push As String = ""
Dim title_push As String = ""

If cursor1.RowCount > 0 Then

cursor1.Position = 0
title_push=cursor1.GetString("titolo") & ""
message_push=cursor1.GetString("messaggio") & ""

//here save in local my message
app_cfg.title_push = title_push
app_cfg.message_push = message_push
app_cfg.id_notifica = id_notifica



'------------------------------------------
'------------------------------------------

StartActivity(frmNotifichePush)

End If

End If





Catch
fg.scrivi_in_file(File.DirRootExternal,"eccezione0.txt",LastException)
End Try
End Sub

And finally in the activity of notification (frmNotifichePush):

Sub Activity_Resume

Try
Dim message_push As String = app_cfg.message_push
Dim title_push As String = app_cfg.title_push
Dim id_notifica As Int = app_cfg.id_notifica


If message_push=="" And title_push=="" Then
LabelMessaggioPush.Text="In questa sezione riceverai i messaggi informativi ricevuti tramite notifiche push."
Else

LabelMessaggioPush.Text=message_push
LabelTestoPush.Text=title_push
Dim n As Notification
n.Initialize
n.Cancel(id_notifica)
End If




Catch
fp.scrivi_log("dopo cancel notifica","Notifiche",LastException)
End Try
End Sub


Now....how i can close this page and the application from this page?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
my app crash and close

You did not post any error messages.

Please use code-tags (otherwise the code is hard to read)

Now....how i can close this page and the application from this page?

I don't understand what you need here. If you receive a message, you throw a notification. If the user clicks on it, the MAIN activity is started and via intent you get the notification's content. So far so good.

A good choice to "close" an activity is just start another and leave it as it is. You can save the status of your app in Process_Globals and control your app (or as you did in a file or db).

Please post more details like

notification received -> user clicks on it -> activity starts -> ?
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I solved my problem...i think not finish main activity when go on push page activity..
now i want to ask you how i can manage when i push on group of notify...

for example my samsung s6, when i receive more notification from same app, my phone group the notification.
When i click on group, my app open, but i don't know what he do...

you can help me?
thank you
 
Upvote 0
Top