B4A Library [Library] Advanced Notification

Hello again Guys and girls (Yes there are some!)

Here is another Notification Library.

To clear up an confusion between this and my Custom Notification Library I will explain the difference between the 2 now.

IF you require the ability to create your own xml layouts for notifications use the Custom Notification Library. If you wish to use the standard layout but have the other extra options available then us this one. Hope I don't confuse people too much.

Because this library uses the standard Notification object it DOES NOT suffer the same 4.0.3 bug mentioned in the Custom Notification post.

Documentation:

  • AdvancedNotification
    Properties:
    • AlertOnce As Boolean
      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 Boolean
      Sets whether the notification will be cancelled automatically when the user clicks on it.

    • Icon As Boolean
      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:
      'NotificationIcon.png copied to Objects\res\drawable\ folder and set to read-only.
      an.Icon = "NotificationIcon"

    • Insistent As Boolean
      Sets whether the sound will play repeatedly until the user opens the notifications screen.

    • Light As Boolean
      Sets whether the notification will show a light.

      Example:
      an.Light = False

    • NoClear As
      Sets whether the notification should NOT be cancelled when the user clicks the Clear All button.

      Example:
      an.NoClear = True 'Notification will not clear.

    • Number As Int
      Gets or Sets the number that will be shown on the icon.
      This is useful to represent multiple events in a single notification.

    • OnGoingEvent As Boolean
      Sets whether the notification is an 'ongoing event'.
      The notification will be shown in the ongoing section and will not be cleared.

    • Sound As Boolean
      Sets whether the notification will play a sound.

      Example:
      an.Sound = False

    • TickerText As String
      Sets the Ticker Text which shows along side the status bar icon on new notification.

      Example:
      an.TickerText = "New notification"

    • Vibrate As Boolean
      Sets whether the notification will vibrate the device

      Example:
      an.Vibrate = False

    Methods:
    • Cancel (Id As Int)
      Cancels the notification with the given Id

    • Initialize
      Initializes the notification. By default the notification plays a sound, shows a light and vibrates the phone.

      Example:
      Dim an as AdvancedNotification

      an.Initialize()
      ...

    • IsInitialized As Boolean

    • Notify (Id As Int)
      Displays the notification.
      Id - The notification Id. This Id can be used later to update the notification (by calling Notify again with the same Id),
      or to cancel the notification

    • SetCustomSound (path As String)
      Sets a custom sound to be played on new notification.

      NOTE:
      Automatically sets 'an.Sound = False'
      This is required for this method to work, Do not set 'an.Sound = True' after setting a custom sound or the custom sound will no work.

      Example:
      an.SetCustomSound("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:
      Automatically sets 'an.Vibrate = False'
      This required for this method to work. Do not set an.Vibrate = True' after setting a custom vibration pattern or the custom pattern will not work.

      Example:
      Dim v() as Long

      v = Array as Long(0, 100, 200, 300, 400)
      an.SetCustomVibrate(v)

    • SetInfo (Title As String, Body As String, Activity As Object)
      Sets the message text and action.
      Title - The message title.
      Body - The message body.
      Activity - The activity to start when the user presses on the notification.
      Pass an empty string to start the current activity (when calling from an activity module).
      Example:
      an.SetInfo("Some title", "Some text", Main)

    • SetInfo2 (Title As String, Body As String, Tag As String, Activity As Object)
      Similar to SetInfo. Also sets a string that can be later extracted in Activity_Resume.
      Title - The message title.
      Body - The message body.
      Tag - An arbitrary string that can be later extract when the user clicks on the notification.
      Activity - The activity to start when the user presses on the notification.
      Pass an empty string to start the current activity (when calling from an activity module).
      Example of extracting the tag:
      Sub Activity_Resume
      Dim in As Intent
      in = Activity.GetStartingIntent
      If in.HasExtra("Notification_Tag") Then
      Log(in.GetExtra("Notification_Tag")) 'Will log the tag
      End If
      End Sub

Installation:

Extract the AdvancedNotification.zip
Copy 'AdvancedNotification.jar' & 'AdvancedNotification.xml' to your b4a library folder.
Then just add the library as usual and code away.

Update: V1.2
Due to rushing the release of this library I missing a very important part out. The permission for the Vibrations. Again, this is thanks to NJDude for spotting it. Now added. If you have used this library on any projects please update and recompile your app. Thanks and sorry for any inconvenience caused.

Hope you find this useful!
Thanks

Barx
 

Attachments

  • AdvancedNotification.zip
    5.4 KB · Views: 1,313
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
Please note: as mentioned above, this library has been deprecated / superseded by the Notification Builder library, it adds loads more functionality.

This very subject has been discussed in that library's thread too. Referencing sound files from the assets dir does not work. You first need to copy it from assets to somewhere like the SD card or internal memory

Take a look at this post onwards

http://www.b4x.com/android/forum/threads/notification-builder-library.27376/page-9#post-259731

Also, check the first post of that thread for the library itself.

Thanks
 

laviniut

Active Member
Licensed User
Longtime User
Can you add a SetInfo3 method to extract a tag in a service instead of an activity ? Can you give me an example ?
 

barx

Well-Known Member
Licensed User
Longtime User
Can you add a SetInfo3 method to extract a tag in a service instead of an activity ? Can you give me an example ?

I'm afraid this library is no longer maintained as it has been superceeded by the Notification Builder Library which is much more powerful.
 

laviniut

Active Member
Licensed User
Longtime User
Ok, but it needs API 16+. My application monitors battery voltage and i want to be used at wide range of api level.
 

barx

Well-Known Member
Licensed User
Longtime User
Ok, but it needs API 16+. My application monitors battery voltage and i want to be used at wide range of api level.

Who says it need API 16+

Certain features need higher API, true, but it also works on lower API's and the additional features don't work. No errors, etc.
 
Top