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,193
  • CustomNotification.zip
    7.7 KB · Views: 2,182
Last edited:

monki

Active Member
Licensed User
Longtime User
hello,
thanks for the Lib
I tried to start the example of nDude unfortunately it does not work.
The following error message appears on compiler.
why?


Compiling code. 0.06
Compiling layouts code. 0.01
Generating R file. Error
res\values-v9\styles.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent'.
res\values-v14\styles.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent'.
res\values-v9\styles.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent.Title'.
res\values-v14\styles.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent.Title'.
 

giannimaione

Well-Known Member
Licensed User
Longtime User
another error
The following error message appears on compiler.

Compiling code. 0.16
Compiling layouts code. 0.02
Generating R file. Error
res\values-v9\styles.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent'.
res\values-v14\styles.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent'.
res\values-v9\styles.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent.Title'.
res\values-v14\styles.xml:4: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.StatusBar.EventContent.Title'.
 

giannimaione

Well-Known Member
Licensed User
Longtime User
@giannimaione and @monki

Try cleaning your project, from the IDE, click on Tools -> Clean Project, then re-compile.

sorry :( but not work :BangHead:
I tried to start the example of nDude unfortunately it does not work.
 

monki

Active Member
Licensed User
Longtime User
@ nJDude
I tried it with Clean Project but without success.
It appears the same error
 

monki

Active Member
Licensed User
Longtime User
@ nDude
I tried it with Clean Project but without success.
It appears the same error
 

barx

Well-Known Member
Licensed User
Longtime User
I am currently at work with no pc. I will download the files from the site myself when I get home and check. It was added late at night and I may have uploaded the wrong versions. Although I'm sure this isn't the case. You never know. As mentioned above. Any problems with missing resources etc are usually sorted by cleaning the project as this re-builds the r.java file that indexes the resources. Also, make sure you made all the files read only. I will edit the original post later to include a file list so people will know if anything is missing.

Thanks for trying it out and hopefully we'll get you sorted.

Sent from my HTC Vision using Tapatalk
 

barx

Well-Known Member
Licensed User
Longtime User
Well done and thank you for sharing this library. I know that this wasn't a simple task.

I've created a wiki account for you. You should receive an email with your password. So you can update this library in the wiki:
Libraries - Basic4android Wiki

Thank you Erel.

Sent from my HTC Vision using Tapatalk
 

barx

Well-Known Member
Licensed User
Longtime User
I think this has to do with TargetSDKVersion.
monki and giannimaione are probably compiling for 2.2 which is why they cannot use features for 2.3 /4.0 .
The compilation of R.java fails, which means it does not want to include the styles for higher sdk versions.

Target sdk set in b4a?

Sent from my HTC Vision using Tapatalk
 

barx

Well-Known Member
Licensed User
Longtime User
OK monki and giannimaione.

I agree with 'thedesolatesoul' as above on this. If you open b4a and click 'Tools' then 'Configure Paths'. What does it say at side of android.jar.

Something like

B4X:
C:\Android\android-sdk\platforms\android-8\android.jar

This means projects using features present in Android versions v.9(gingerbread) and above, but weren't present in v8 (froyo), will fail to compile.

What you need to do is follow this guide:
Adding SDK Packages | Android Developers

to install the sdk (and I would recommend the ARM EABI) for Android 4.0.3 (V15).

Once that is done open up b4a, go to 'Tools', 'Configure paths'. Click browse button and redirect it to the v15 Android.jar. So if you path was same as above then it will then become

B4X:
C:\Android\android-sdk\platforms\android-15\android.jar

This problem has happened because the custom layouts contain variants for higher versions of Android and use style elements (for text colors) not present in Froyo. In the earlier version of Android these styles had to be hard coded.

Hope this solution works and thanks for bringing it to my attention.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Which layout to load

Looking into the code but cannot find how each button knows which layout to load and to use.

There are several layouts in the res but how does the code know which one to pick?

Cheers,
 

barx

Well-Known Member
Licensed User
Longtime User
Looking into the code but cannot find how each button knows which layout to load and to use.

There are several layouts in the res but how does the code know which one to pick?

Cheers,

It is part of the initialize statement. The number in the parenthesis denotes which layout. The key is shown in the tooltip as you write xxxxx.initialize
 
Top