B4A Library Custom Notification Library: Barx

Hello guys and girls.

Note: this library has been superceded by Notification Builder Library

Here is my first library. It allows you to create custom notifications and has more settings for the notifications than the b4a Notification Object.

Additional settings include:
  • Setting Ticker Text
  • Setting custom patterns for LED and Vibration
  • Setting custom Notification alert tone
  • Setting the color and size of text elements
  • Plus more...

You can create custom Layouts for the notifications (XML knowledge required) or use one of the built in Presets. These include a progress bar layout to use for thing like showing file download progress.

There is a bug in Android 4.0.3 that shows Custom Notifications with the wrong background color. See this post. and the link in the post
http://www.b4x.com/forum/libraries-developers-questions/16916-ics-notification-background-bug.html
I have created a set of layout files just for this version of Android that hard code the background colors to the stock theme. These are automatically loaded with this version is used and no action is required from you to set them. This seems to work well. If a better solution is found I will update accordingly.

Some screen shots of the library in action:

twotextplusimage.png

onetextplusimageplusprogress.png

Documentation
  • CustomNotification
    Permissions:
    • android.permission.VIBRATE
    Properties:
    • AlertOnce As
      Sets whether the notification sound and/or vibration should sound each time the notification is sent, even if it has not been cancelled before.
    • AutoCancel As
      Sets whether the notification will be cancelled automatically when the user click on it
    • DefaultLED As
      Sets whether the notification will Flash the LED (if available) using the Default settings.

      NOTE: Setting to True will over rule the CustomLED

      Example:
      n.DefaultLED = False
    • DefaultSound As
      Sets whether the notification will play a sound using the default settings
      Example:
      n.DefaultSound = True
    • DefaultVibrate As
      Sets whether the notification will vibrate using the default settings

      NOTE: Setting to True will overrule the CustomVibrate

      Example:
      n.DefaultVibrate = True
    • Insistent As
      Sets whether the sound will play repeatedly until the user opens the notification screen
    • NoClear As
      Sets whether the notification should NOT be cancelled when the user clicks the Clear All button.
      Example:

      Dim cn as CustomNotification

      cn.NoClear = True 'Notification will not clear.
    • Number As Int
      Gets or Sets a number that will be displayed over the icon. This is useful to represent multiple events in a single notification.
    • OnGoingEvent As
      Set if the notification is in reference to an ongoing event, e.g. a phone call.
      Do not set for notifications that are in reference to something that happened at a particular point in time.
    • TickerText As
      Sets the Ticker Text which shows along side the status bar icon on new notification.

      Example:
      cn.TickerText = "New notification"
    • When As Long
      <B>Note: Experimental Feature</B>
      The offical description of this method is - <i>The timestamp for the notification.</i>

      Setting this to the max Value for a long (9,223,372,036,854,775,807) pushes the Notification Icon to the right.
    Methods:
    • Cancel (Id As Int)
      Cancels the notification with the given Id
    • Initialize (Layout As Int)
      Initializes the notification setting default values of:
      LED = Enabled
      Sound = Enabled
      Vibration = Enabled
      Layout - Sets the layout to be used.
      Options:
      1 = Default notification layout (Two Text fields plus Icon)
      2 = Two Text fields plus One Image
      3 = One Text field plus One Image plus One Progress Bar
      4 = Two Text fields ONLY
      5 = User Defined. Add your xml file to the res/layout. Name it mylayout.xml and make it read only.

      Example:
      cn.Initialize(2) 'Initializes the notification and sets the layout to Two Text fields plus One Image
    • IsInitialized As Boolean
    • Notify (Id As Int)
      Displays the notification.
      Id: - The notification Id. This can be used later to update the notification (by calling notify again with the same Id) or cancel it.
    • SetCustomSound (path As String)
      Sets a custom sound to be played on new notification

      Example:
      cn.CustomSound("file:///sdcard/notification/ringer.mp3")
    • SetCustomVibrate (Values As Long())
      Sets A custom vibrate sequence for notification Vibration
      The Array of values can be as long as you wish.
      The First Value is the pause before vibration starts, then it's ON, OFF, ON, OFF, etc.

      NOTE: DefaultVibrate must be set to False

      Example:
      Dim cn as CustomNotification
      Dim v() as Long

      v = Array as Long(0, 100, 200, 300, 400)
      cn.DefaultVibrate = False
      cn.CustomVibrate(v)
    • SetImage (id As String, Image As Bitmap)
      Sets image used in a notification. Only use with "twotextsplusimage" and "onetextplusimageplusprogress" Presets or if defined as part of a user defined layout.
      For Preset layout the id is "image".
    • SetIntent (Activity As Object)
      Sets the Activity that is called when the user clicks the notification
      Example:
      cn.Activity(Main)
    • SetProgress (id As String, maxProgress As Int, progress As Int, indeterminate As Boolean)
      Sets the parameters for a progress bar used in a notification. Only use with "onetextplusprogress" Preset or if defined as part of a user defined layout.
      For Preset layout the id is "progress".

      maxProgress - the upper limit of the progress bar. A good value is 100
      progress - the current level of progress to be shown. Must be <= maxProgress.
    • SetTag (Tag As String)
      The Tag is a string that can be extracted later on Activity_Resume.
      This can be used to determine which notification has been clicked by the user when multiple notifications exist.

      Example of extracting the Tag:
      Sub Activity_Resume
      Dim in as Intent
      Dim intentExtra as String

      in = Activity.GetStartingIntent
      If in.HasExtra("Notification_Tag") Then
      intentExtra = in.GetExtra("Notification_Tag")
      End If
      End Sub
    • SetText (id As String, text As String)
      Sets the content of text items for the notification
      For Preset Layouts the id's are "title" and "text".
      Example:

      cn.SetText("title", "New notification")
      cn.SetText("text", "You have a new notification")
    • SetTextColor (id As String, Color As Int)
      Sets the Color of a text item.
      For Preset Layouts the id's are "title" and "text".
      Example:

      cn.SetText("title", "New notification")
      cn.SetTextColor("title", Colors.Red)
    • SetTextSize (id As String, Value As Int)
      Sets the text size of a text item.
      For Preset Layouts the id's are "title" and "text".
      Example:

      cn.SetText("title", "New notification")
      cn.SetTextSize("title", 20)
    • setCustomLED (LightOn As Int, LightOff As Int)
      Set a custom timing for the notification Light.
      Timings are in milliseconds.

      NOTE: DefaultLED must be set to False.

      Example:
      Dim cn as CustomNotification

      cn.DefaultLED = False
      cn.CustomLED(300, 1000)
    • setIcon (FileName As String)
      Sets the icon to be displayed.
      The image file should manually copied to the Objects\res\drawable\ folder and set to read-only.
      The file name is case sensitive and should not contain the file extension.
      You can use "icon" to use the applications icon.

      Example:
      cn.Icon("icon")


I hope you find this library useful and enjoy using it. If there are any problems, let me know and I will fix ASAP.

Thanks...

Credits:
Without the help of some members of the b4a community, this library would never been created. I want to give extra special thanks to:
NJDude who has help alot especially with testing and listened to me go on about notifications every day for about 3 weeks. He has also assured me he will add a sample project to show you all how easy this library is to use.
thedesolatesoul who has helped a huge amount and even beat me to a release.
corwin42 and warwound for their coding skills (of which I spent many hours reading) and many others have played a part.

Installation:
Please note:
this library requires your b4a installation to use v15 of the Android.jar see This Post for details.

This library uses 'res' files as well as the standard b4a library files.

Extract the CustomNotification.zip
Copy 'CustomNotification.jar' & 'CustomNotification.xml' to your b4a library folder.
Extract res.zip
Copy the 'res' folder to your projects 'Objects' folder and make all files copied withing the res folder 'Read-Only'. This is important!
Then just add the library as usual and code away.

Update - V1.10
Fixes a bug. When using layout(1) and setting Icon to something other than "icon". The notification still used "icon" for the image. Thanks to Inman

Update - V1.01
This is not a big update and doesn't add any functionality. Whilst using the library I noticed some errors in the tooltips so decided to correct them. An example being, in the DefaultLED tooltip the example was cn.Light instead of cn.DefaultLED. This could have led to some confusion.

Updated original link.

Thanks

Files attached below:
 

Attachments

  • res.zip
    47.4 KB · Views: 2,060
  • CustomNotification.zip
    7.7 KB · Views: 2,071
Last edited:

Mrjoey

Active Member
Licensed User
Longtime User
hey , quick question , when calling setimage , the freezes a bit , is that normal?
 

dieterp

Active Member
Licensed User
Longtime User
With regards to the CustomSound functionality,when I directly copy a 'notification.ogg' file to the 'Notifications' folder on my device, it works fine when I use the code Notification2.CustomSound = "file:///sdcard/notifications/notification.ogg"

However, when I copy the 'notification.ogg' file to the File.DirDefaultExternal folder, I cannot seem to play it from that folder. I've tried several paths (using file:///) but i just can't seem to get it to play. Will it not work from the File.DirDefaultExternal folder?

Thanks in advance!
 

barx

Well-Known Member
Licensed User
Longtime User
With regards to the CustomSound functionality,when I directly copy a 'notification.ogg' file to the 'Notifications' folder on my device, it works fine when I use the code Notification2.CustomSound = "file:///sdcard/notifications/notification.ogg"

However, when I copy the 'notification.ogg' file to the File.DirDefaultExternal folder, I cannot seem to play it from that folder. I've tried several paths (using file:///) but i just can't seem to get it to play. Will it not work from the File.DirDefaultExternal folder?

Thanks in advance!

I'll be honest, I have no idea. Maybe a permissions thing? I really don't know.

Please note. This lib has now been superceded by notification builder. It is much better.
 

dieterp

Active Member
Licensed User
Longtime User
Thanks barx

I'll go through the Notification Builder forum again and see if I can pick up if anyone had the same query. Will re-post there to see if anyone can assist.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
sorry, but as you turn off the sound?
thanks
 

barx

Well-Known Member
Licensed User
Longtime User

Roberto P.

Well-Known Member
Licensed User
Longtime User
ok thanks. I try it right away.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
can I change the background color of the notification?
thanks
 

barx

Well-Known Member
Licensed User
Longtime User
can I change the background color of the notification?
thanks
You can change the color by creating a custom layout. I'm not sure about changing the standard layout color, maybe you can by creating a custom theme for your app.
 

delozoya

Member
Licensed User
Longtime User
Hello. I have this code for progress download huge files:

B4X:
Sub dd_Progress(Progress As Long, Total As Long)
   
    ProgressBar1.Progress = Progress / Total * 100
    Log(NumberFormat(Progress / 1024000, 0, 0))
    progreso_Actual=NumberFormat(Progress / 1024000, 0, 0)
    progreso_total=(NumberFormat(Total /1024000 , 0, 0))
   
    Label1.Text = NumberFormat(Progress / 1024000, 0, 0) & "MB / " & _
        NumberFormat(Total /1024000 , 0, 0) & "MB"
        aux=Label1.Text
        aux_total=Total
End Sub


How use cn.setProgress for download huge file?
 
Top