Android Question [SOLVED] Displaying the body text in a notification.

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Can you check my coding and let me know where I made mistakes?

I'm trying to display some text in the body area of SetInfo. In the example, the text "This is my app name." is being displayed in both the "Title" and the "Body" area of the notification when I drag down the notification drawer. In the notification, I'm looking to display the text "This is the body text." in the body area.

My coding is in Sub Service_Start of the Starter service.

B4X:
Sub Service_Start (StartingIntent As Intent)

   
    SetupDefaultSettingsDatabase
    InitializeObjects
   
    nNotify.Initialize2(nNotify.IMPORTANCE_LOW)
    nNotify.SetInfo("This is my app name.", "This is the body text.", "")
    Service.AutomaticForegroundNotification = nNotify

    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    wakeState.PartialLock

    StartTheTimer
End Sub

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
You should set the automatic notification in Service_Create.
Hi Erel,

Thanks for the reply.

I moved the coding into Service_Create but the notification still shows the same text in the title and body.

Maybe I need to add something into the manifest editor?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I just tried to add the following coding to make it look like this but it crashes the app.

B4X:
Sub Service_Start (StartingIntent As Intent)

    SetupDefaultSettingsDatabase
    InitializeObjects
 
    nNotify.Initialize2(nNotify.IMPORTANCE_LOW)
    nNotify.SetInfo("This is my app name.", "This is the body text.", "")
    Service.AutomaticForegroundNotification = nNotify
    nNotify.Notify(1)


    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    wakeState.PartialLock

    StartTheTimer
End Sub

The logs show this:

B4X:
Logger connected to:  samsung SM-G950U1
--------- beginning of main
** Receiver (servicewidget) OnReceive **
** Service (servicewidget) Start **
Partial wakeLock already held.
** Receiver (servicewidget) OnReceive **
*** Service (starter) Create ***
starter_service_create (java line: 505)
java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(channel=channel_2 pri=0 contentView=null vibrate=default sound=default defaults=0xffffffff flags=0x0 color=0x00000000 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
    at android.app.NotificationManager.notifyAsUser(NotificationManager.java:333)
    at android.app.NotificationManager.notify(NotificationManager.java:297)
    at android.app.NotificationManager.notify(NotificationManager.java:281)
    at anywheresoftware.b4a.objects.NotificationWrapper.Notify(NotificationWrapper.java:281)
    at b4a.example.starter._service_create(starter.java:505)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at b4a.example.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3542)
    at android.app.ActivityThread.-wrap4(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
*** Service (starter) Create ***
starter_service_create (java line: 505)
java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(channel=channel_2 pri=0 contentView=null vibrate=default sound=default defaults=0xffffffff flags=0x0 color=0x00000000 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
    at android.app.NotificationManager.notifyAsUser(NotificationManager.java:333)
    at android.app.NotificationManager.notify(NotificationManager.java:297)
    at android.app.NotificationManager.notify(NotificationManager.java:281)
    at anywheresoftware.b4a.objects.NotificationWrapper.Notify(NotificationWrapper.java:281)
    at b4a.example.starter._service_create(starter.java:505)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at b4a.example.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3542)
    at android.app.ActivityThread.-wrap4(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
*** Service (starter) Create ***
starter_service_create (java line: 505)
java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(channel=channel_2 pri=0 contentView=null vibrate=default sound=default defaults=0xffffffff flags=0x0 color=0x00000000 number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
    at android.app.NotificationManager.notifyAsUser(NotificationManager.java:333)
    at android.app.NotificationManager.notify(NotificationManager.java:297)
    at android.app.NotificationManager.notify(NotificationManager.java:281)
    at anywheresoftware.b4a.objects.NotificationWrapper.Notify(NotificationWrapper.java:281)
    at b4a.example.starter._service_create(starter.java:505)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at b4a.example.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3542)
    at android.app.ActivityThread.-wrap4(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
** Receiver (servicewidget) OnReceive **
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

Here's what I just discovered. This only works if I compile the app under debug mode.

First I had to change the coding to look like this:

B4X:
    Sub Service_Start (StartingIntent As Intent)

    SetupDefaultSettingsDatabase
    InitializeObjects
 
    nNotify.Initialize2(nNotify.IMPORTANCE_LOW)
    nNotify.Icon = "icon"
    nNotify.SetInfo("This is my app name.", "This is the body text.", "")
    Service.AutomaticForegroundNotification = nNotify

    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    wakeState.PartialLock

    StartTheTimer
End Sub

In debug mode I get the correct text in the body. If I recompile the app in release mode, I get the duplicate text in the title and body. I'm not sure why debug and release mode made the difference, but I hope you can help me get it working for release mode.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Good news everyone.

I discovered if you change from debug mode to release mode you must completely uninstall the app and recompile it to install it and the notification body and text will be correct. So I will mark this thread as solved.
 
Upvote 0
Top