B4A Library Notification Builder Library.

barx

Well-Known Member
Licensed User
Longtime User
I think you are probably looking at a Custom Layout.
 

hongbii khaw

Member
Licensed User
Longtime User
Hello Barx,
First of all thanks for the library =D
May I know how to set the notification to always repeat until the user click on it?
Like the one in the normal notification:
B4X:
n.Insistent = true
Thank you.
 

Tasyo28

Member
Licensed User
Hi Barx Im new here in B4a and i saw your liblary so interesting but when i tried to run a sample code this error comes in the log.
Please advise maybe i did something wrong.

B4X:
Sub Globals
 
    Dim nb As NotificationBuilder
   
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")
 
    nb.Initialize
    nb.SmallIcon = "icon"
    nb.ContentTitle = "Heads UP!"
    nb.ContentText = "B4A is about."
    nb.Priority = 1
    nb.setActivity(Me)
    nb.AddAction("arrow", "Reply", "Reply", Me) 'gfx are just some I had to hand, not much time to find others
    nb.AddAction("circle", "Dismiss", "Dismiss", Me)
 
    nb.Notify(1)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    nb.Cancel(1)
End Sub

this is the error log
 

DonManfred

Expert
Licensed User
Longtime User
Add the following line in the manifest editor
As this command should not be inside the manifest-editor: i suggest to change the text to

Add the following line to your Main activity Code.
B4X:
#additionaljar: com.android.support:support-v4
 

barx

Well-Known Member
Licensed User
Longtime User
I would appreciate peoples thoughts on this update.
This is just a test/beta release, hence it is not on the first post.
Alos, this is my current take on things, I may have some bits wrong, things may change.

I have finally got around to making some updates, mainly spurred on by googles recent mess that basically breaks existing Notification methods. The big change is the requirement for Notification Channels. I'm not going to go into what a channel is, you can read that here.

Due to the way these channels work, this IS a breaking update. i.e. to use this library update, you will have to edit your existing code. The signature for the Initialize method has changed to specify the channel to post the notification to.

A new object has been added called NotificationChannelBuilder. You create channels with this. The Channels are actually seperate to the notification. You create a Channel and then post a notification to the channel. Here is a simple example of the new setup:

B4X:
Dim NB As NotificationBuilder
Dim Channel As NotificationChannelBuilder 'New object
Dim ChannelID as String = "Channel_1"
Dim ChannelName As String = "b4a News"
        
'Build a Channel
    
If Channel.ChannelsSupported Then
    Channel.Initialize(ChannelID, ChannelName, Channel.IMPORTANCE_DEFAULT)
    Channel.Build
End If

nb.Initialize(ChannelID) '<--Note the new parameter

...'rest of existing NotificationBuilder code

As you can see in the code, we have to check if the device supports Channels. Google, in their infinite wisdom haven't made this backwards compatible so trying to create a Channel on a device older than API 26 Oreo will cause an error. The method ChannelsSupported simply returns whether the device API is >= 26. Having the ChannelID parameter in the .Initialize method does not cause any issue on older APIs.

The new lib beta version is attached below for people to try. You will have to copy all 3 files (XML, JAR, AAR) to your additional libs folder and I also advise you clean any existing projects once code is updated (not sure if needed but I had a few errors whilst developing that went away with a clean).

You shouldn't need any #additionalJar references anymore, they have been added to the lib. But you will need to make sure you have the latest support libs installed from google.

Please let me know your thoughts on the implementation. I won't make this official until I get some feedback. I haven't got a Oreo device so this has only been tested on AVD's.

p.s the version number is set to 3.50, it's been that long since I touched this code, I cannot remember where I'm at lol.

p.p.s I will be hopefully added other missing updates to NotificationBuilder once I know this is working.

Cheers for your time.
 

Attachments

  • NotificationBuilder350.zip
    27.9 KB · Views: 738

Peter Simpson

Expert
Licensed User
Longtime User

A quick test, notifications appear to be working perfect here in services @barx

Android Oreo 8.1.0...
 

Peter Simpson

Expert
Licensed User
Longtime User
Update:
Hello @barx,
I usually have the following lines of code set to 'False' for testing as I don't want to hear beeps or vibration noises every 60 seconds. Today I changed these lines from 'False' to 'True' and there appears to still be no vibration, sound or notification lights. I've tried different SDK Targets (23 to 27) and also different Channel.IMPORTANCE_xxx. The notifications themselves are fully working 100% though on the for mentioned SDK Targets

B4X:
'Like this
    Notification.DefaultVibrate = True
    Notification.DefaultSound = True
    Notification.DefaultLight = True

Thank you.
 

Peter Simpson

Expert
Licensed User
Longtime User
I experienced the same behavior, see my post here: https://www.b4x.com/android/forum/threads/version-safe-notification.87663/#post-555296
Seems that you have to use another channel, changing the importance of existing channels doesn't seem to work.

Thanks but I previously saw your post on Erels code feed and I tried the second channel and also changed the priority before making this post, it appeared to do the same for me...

Check this out https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels
 
Last edited:

barx

Well-Known Member
Licensed User
Longtime User

Interesting, thanks for the feedback. So no sound, vib, lights at all?

in your code, could you try
B4X:
Log(Channel.EnableLights)
Log(Channel.EnableVibration)

also try explicitly setting those to true. Control of these items seems to be shared now but the docs do not state which gets priority. On my AVD I deffo get sound. No way to test lights or vibration.

EDIT: On AVD Oreo I Don't get sound either, It was on Nougat I was getting sound. Let me look in more detail, see if I can get sound.

Cheers
 
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
If you long press on the notification, You will see some options. Then tap more settings. Then tap the channel name. You will see how the settings are for the channel. You can change these manually, but obviously we need to get these right in code. I'll come back shortly
 

barx

Well-Known Member
Licensed User
Longtime User
Another note, the settings cannot be changed by code on a channel once it is created, you have to either delete it or uninstall the app. then it will be re-created
 

Peter Simpson

Expert
Licensed User
Longtime User
Thank you @barx for your response. I had already tried the following before creating my previous post, I forgot to mention it, sorry.

I had already tried the.
B4X:
Channel.EnableLights = True
Channel.EnableVibration =True
I even added the following line to the manifest just to be sure.
B4X:
AddPermission("android.permission.VIBRATE")

If I add the logs as you stated above they both return 'true' to both channel lights and channel vibrate. I've tried variants with IMPORTANCE_DEFAULT, IMPORTANCE_HIGH and IMPORTANCE_MAX, still nothing. But as I mentioned previously, the notifications all work perfectly.

Thank you
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Another note, the settings cannot be changed by code on a channel once it is created, you have to either delete it or uninstall the app. then it will be re-created

Hmm really, I'll give it a quick try. I did notice that If I used low importance I had to uninstall it as the notification would then stop, even when I changed it back to high again lol

Give me 5 minutes...
 

barx

Well-Known Member
Licensed User
Longtime User
OOOOOOOOOOHH I got sound, It seems that sound only plays if the notification posts whilst the screen is off. Still digging
 

Peter Simpson

Expert
Licensed User
Longtime User
Okay, it's still the same @barx,
Everything is on, and in the settings you see the following, yes the default sound notification is working I tried it, I don't get sound even when the screen is off, also there's still no vibration.



 

barx

Well-Known Member
Licensed User
Longtime User
Something interesting happened here, AVD crashed, I had to cold boot it. Then sounds work with high importance, even whe screen is on.
 

Peter Simpson

Expert
Licensed User
Longtime User
STOOOOOOOOOOP - @barx you're going to kill me.

BOTH sound and vibrate are working, still no light though.

I was a bit baffled why the sound was working for you but not me, so after thinking long and hard about it I suddenly remembered that I have a watch upstairs that might be on and connected to my phone. I took the watch off earlier today after I came back from the shops, so I went upstairs and turned my watch off. I then ran the test app and there was the default sound and vibration on my phone, no light though

Oops sorry, I completely forgot that watches also receive notification and my phone must be set to not make a noise or vibrate if my phone is connected to my watch as my watch will vibrate and show the notification for me
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…