Android Question Extract Title & Body from System Notification.

mangojack

Expert
Licensed User
Longtime User
Searching with no success ..

Clicking on Notification starts an Activity .. Can I get / extract the Notification Title and Body string. ?

I am aware and do not have a problem adding / extracting Notification_Tag.

Many Thanks.
 

DonManfred

Expert
Licensed User
Longtime User
Clicking on Notification starts an Activity .. Can I get / extract the Notification Title and Body string. ?
what is the content of the startingintent? Add some logs to see the content of the intent

You can get the starting intent in the starterservice
B4X:
Sub Service_Start (StartingIntent As Intent)
End Sub
 
Last edited:
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
I'm going to take a chance here. If you are using Firebase Messaging, then you should have a sub somewhere (FirebaseMessaging Service perhaps) such as:

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)

If this is the case then, it's quite simple actually.

B4X:
Message.GetData.Get("title")
Message.GetData.Get("body")
Message.GetData.Get("sound")
Message.GetData.Get("priority")

So if you run some logs on them - you should see what you are looking for.

If it's not the case, then I am sorry I cannot help.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thanks Bill ... I am Using FCM but that is Not the problem.. It seems so trivial and simple but cannot for the life of me find a solution.

1. Firebase receives incoming message.
2. The Title and Body of Incoming message are presented to user as Notification.
3. User clicks Notification .. that then starts Activity.

How do I then extract system Notification Title and Body from starting intent (setting / getting Tag extra is not an issue)

Cheers and Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How do I then extract system Notification Title and Body from starting intent
What is the content of the intent?
3. User clicks Notification .. that then starts Activity.
B4X:
  Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in <> OldIntent Then
        OldIntent = in
        Dim intentExtra As String
        If in.HasExtra("Notification_Tag") Then
            intentExtra = in.GetExtra("Notification_Tag")
            log("XTRA="&intentExtra)
            jp.Initialize(intentExtra)
            Dim root As Map = jp.NextObject
            Dim Title As String = root.Get("title")
            Dim Body As String = root.Get("body")
        End If
    End If
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Thank Don ... But still no luck with this , Getting a Jason error .

B4X:
Dim in As Intent = Activity.GetStartingIntent
    Log("Activity.GetStartingIntent =  " & in)   'Activity.GetStartingIntent =  (Intent) Intent { flg=0x10020000 cmp=net.icyg.remotemessenger/.main (has extras) }
    Log("Intent.GetData =  " & in.GetData)       'Intent.GetData =  null
  
      
        If in.HasExtra("Notification_Tag") Then
            Dim intentExtra As String
            intentExtra = in.GetExtra("Notification_Tag")
          
        Log("Extras  :  " & intentExtra)        'Extras  :  "Testing"      ... OK & Correct
          
            jp.Initialize(intentExtra)
            Dim root As Map = jp.NextObject     '<---------- Error
            Dim Title As String = root.Get("title")
            Dim Body As String = root.Get("body")
        End If

Error ...
** Activity (main) Resume **
Activity.GetStartingIntent = (Intent) Intent { flg=0x10020000 cmp=net.icyg.remotemessenger/.main (has extras) }
Intent.GetData = null
Extras : Testing
main_activity_resume (java line: 424)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:50)
at net.icyg.remotemessenger.main._activity_resume(main.java:424)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)

The part I don't understand with the above code .. If I use n.SetInfo2 to include a Notification_Tag .. why is the Title and Body included in the GetExtra only then.

If I create Notification with n.SetInfo (Forget Extra Tag parameter) ... How do I extract Notification Title & Body.

Thanks Again.
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Yes .. Correct .. The Phone Device sends a FireBase message to a Tablet. The Tablet receives that Message and parses it to extract the data (Title and Body).

That Data is then presented as System Notification ... and when clicked starts an Activity. How does that Activity get the Notification Title & Body ( or any included data .. Not extra Tag.

ie: Forget FireBase .. If I was to present a Notification to the User ..

B4X:
Dim n As Notification 
n.Initialize 
n.Icon = "icon" 
n.SetInfo("This is the title", "and this is the body.", Main)
n.Notify(1)

when Main Activity starts how do I extract / Get Title & Body .. "This is the title", "and this is the body."

or am I really missing something here .. :confused:

Thanks again
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Tip: Use Notificationbuilder Class

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private OldIntent As Intent
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Simple_Notification
End Sub

Sub Activity_Resume
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in <> OldIntent Then
        OldIntent = in
        Dim intentExtra As String
        Log("Intent="&in)
        Log("Intent="&in.GetData)
        Log("Intent="&in.ExtrasToString)
        'Log("Intent="&in)
            
        If in.HasExtra("Notification_Tag") Then
            intentExtra = in.GetExtra("Notification_Tag")
            Log("XTRA="&intentExtra)
            Dim jp As JSONParser
            jp.Initialize(intentExtra)
            Dim root As Map = jp.NextObject
            Dim Title As String = root.Get("title")
            Dim Body As String = root.Get("body")
            Dim other As String = root.Get("other")
            Log("The Title is: "&Title)
            Log("and the Body: "&Body)
            Log("and the other value: "&other)
        End If
    End If
End Sub
Sub Simple_Notification
    Private smiley As Bitmap
    smiley = LoadBitmapResize(File.DirAssets, "smiley.png", 24dip, 24dip, False)

    Dim m As Map = CreateMap("title":"Title here", "body": "Body here","other":"Another value") ' Define the map you want to get from the notification....
    
    Dim jg As JSONGenerator
    jg.Initialize(m) ' Add the map to the jsogenerator
    
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(smiley)
    n.Build("This is the title or what", "Content is here", jg.ToString, Me).Notify(4) ' Note the jg.tostring as TAG
End Sub
 

Attachments

  • mangojack.zip
    21.1 KB · Views: 153
Upvote 0
Top