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.basic4ppc.com/forum/libr...ns/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: 1,833
  • CustomNotification.zip
    7.7 KB · Views: 1,868
Last edited:

Sinan Tuzcu

Well-Known Member
Licensed User
Helo,

i have the same error like melamound.
What can i do for this Problem?
can someone help me?

B4X:
Parsing code.                           0.06
Compiling code.                         Error
Error compiling program.
Error description: Cannot access activity object from sub Process_Globals.
Occurred on line: 16
Dim Notification2 As CustomNotification
Word: customnotification

greet
sinan
 

TheGerman

Member
Licensed User
How to install ?

Hi

I would like to install your Libary - but i do not know were my 'Objects' folder is.
i am using Windoiws 7 and unter the path c:\Program Files (x86)\Anywhere Software\Basic4android\ ther is no 'Objects' folder ...

an "Additional_Libraries" Folder i have created and set the Path into "Tools / Configurate Paths" but i think that it is not so easy - or ?

Thanks in advance!
 

barx

Well-Known Member
Licensed User
It is in the directory where your projects is stored.

Thanks
 

ferya

Member
Licensed User
Custom Font

Hi,
This is very useful library.
Is it possible to use custom typeface in notification text?
 

barx

Well-Known Member
Licensed User
Hi,
This is very useful library.
Is it possible to use custom typeface in notification text?

I don't believe it is. Will do a little research and see.
 

ferya

Member
Licensed User
Custom font

Hi Barx ,
I found a program that used custom typeface and got that it's possible.
But I don't know how to do it.
Please add this to your lib if have time.
best regards
 

barx

Well-Known Member
Licensed User
I don't intend working on this library any more. Any new features will be added to my new library.

Though the new library doesn't currently support custom layouts. Are you using custom layouts?

When I get a few minutes i will look into the fonts.

thanks
 

barx

Well-Known Member
Licensed User
Hey don't be sorry man, just letting people know that there is now a new library. Do you use a custom layout? or are you using this lib for the other advanced features?
 

driesvp

Member
Licensed User
Wonderfull library! Works fine. One question: when clicking on the message showed in the notification bar, the program isn't loaded. How can this be done?

Sincerely
 

barx

Well-Known Member
Licensed User
If you check the documentation in the first post you will see mention of settig the intent. That should solve your issue. I would advise that you now use the notification builder libs. It supports more recent features ;-)
 

driesvp

Member
Licensed User
If you check the documentation in the first post you will see mention of settig the intent. That should solve your issue. I would advise that you now use the notification builder libs. It supports more recent features ;-)

Thanx! I missed it while reading...
 

barx

Well-Known Member
Licensed User
No worries
 

merlin2049er

Well-Known Member
Licensed User
Great. I think I'll be able to use this.
I've got timers in an app, I need to trigger them even if the app is running in the background.
 

walterf25

Expert
Licensed User
Is there a way to have a notification which would display multiple lines of text, so far i haven't figured out how to accomplish this, does any one know how to do this?

Thanks,
Walter
 

khosrwb

Active Member
Licensed User
hi
when I run this app
basic4android show this error

what is the problem?
 

Attachments

  • Capture2.PNG
    Capture2.PNG
    184.8 KB · Views: 99
  • 1.zip
    108.3 KB · Views: 84

barx

Well-Known Member
Licensed User
hi
when I run this app
basic4android show this error

what is the problem?

I tried the project out and it worked just fine. Installed to a S4 and all the Notifications show.

All I can suggest is, try downloading the library again, and clean your project (in B4A click Tools > Clean Project.

What feature of this library are you wanting to use as this library is now old news. You should now use Notification Builder library. It is A LOT more powerful.

Thanks
 
Top