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,311
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
The library contains a typo.

B4X:
an.Icon("NotificationIcon")

Should be in the format of

B4X:
an.Icon = "NotificationIcon"

I will update the tooltip text when I get a couple of minutes. The functionality is working fine.

Thanks @NJDude
 

barx

Well-Known Member
Licensed User
Longtime User
Updated to V1.2

See First Post

Thanks
 

barx

Well-Known Member
Licensed User
Longtime User
As usual, you are right. I'm really slacking at this these days lol.

Should be sorted now.
 

Amalkotey

Active Member
Licensed User
Longtime User
@barx:

Both the icon as also is the text not displayed. The library version is specified when B4A with 1.11. Where can I find the version 1.2 because?

B4X:
If AdvNotification.IsInitialized = False Then
   AdvNotification.Initialize
   AdvNotification.Icon = "icon"
   AdvNotification.SetInfo2("Test", Global.cMessage, "True", Main)
   AdvNotification.Sound = False
   AdvNotification.Vibrate = False
End If
 

Amalkotey

Active Member
Licensed User
Longtime User
The Lib is attached to the first post.

The zip file in post 1 contains the version 1.0 and in post 6, you can find the version 1.11. This shows me in B4A.
 

barx

Well-Known Member
Licensed User
Longtime User
The Zip in post #1 originally contained v1.0, but I edited the post and updated it to latest v1.20.

I have just downloaded the new zip and can confirm it is v1.2
 

cmweb

Active Member
Licensed User
Longtime User
Hi,

your library is very interesting...

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"
Would it be possible to add a feature to not only use images from objects/res/drawable folder, but also to use custom images during runtime as icon for the notification?

That would be a *great* feature...

Best regards,

Carsten
 

Amalkotey

Active Member
Licensed User
Longtime User
The Zip in post #1 originally contained v1.0, but I edited the post and updated it to latest v1.20.

I have just downloaded the new zip and can confirm it is v1.2

@barx:

Ok, now, I could download the version 1.2
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Hi,

your library is very interesting...


Would it be possible to add a feature to not only use images from objects/res/drawable folder, but also to use custom images during runtime as icon for the notification?

That would be a *great* feature...

Best regards,

Carsten

Thats what I came here to ask for.

Also, what about progress bars?
 

barx

Well-Known Member
Licensed User
Longtime User
Unfortunately the requested image feature is not supported and there is no way to implement it.

As for progress bars, the original model for notifications doesn't support progress bars. This is one reason the Custom Notification library was made.

Later versions of android support a new model called Notification Builder. This DOES support progress bars but only in the later versions. HoneyComb onwards I think.

I, personally am working on a non android project at the moment. Once complete I will probably have a go at a Notification Builder implementation. Or maybe someone else will do it before I get round to it...
 

Inman

Well-Known Member
Licensed User
Longtime User
2. The second issue is with the actual notification. I didn't create any of my own styles and used your basic layout 1 (two text fields plus icon). I set n.setIcon correctly as my notification icon is different from app icon. Now when the notification happens, I see the correct icon on the status bar. But when I pull down the status bar to take a look at the notification, I see my app icon on the left side of the notification text, instead of notification icon. How can I fix it?

Remember this post of mine from Custom Notification Library thread? Now it is happening to Advanced Notification as well.

Did you make any changes regarding this in version 1.2?
 

Inman

Well-Known Member
Licensed User
Longtime User
Remember this post of mine from Custom Notification Library thread? Now it is happening to Advanced Notification as well.

Did you make any changes regarding this in version 1.2?

With the help of barx I figured out this issue. It was because I used SetIcon() after SetInfo(). I just changed the order of the statements and now it works great.

I think it will be better to give SetInfo right before Notify(), just to be on the safe side.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Problem with insistent?

Thanks for a great library but I've encountered a couple of problems, the first was that I was unable to get the notification to show at all but I've solved this myself as it was due to me not assiging an icon. As per the default B4A notifier object, the icon is NOT optional and must be set, maybe this could be given a mention on you're first post?

Now with my notification working I have been unable to make it insistent. Thats not to say that I haven't had it work at all, as I have, but it seems very intermittent as to whether the notification continues to vibrate the phone and make a sound until opened. I've tried declaring the an.Insistent=True at different lines in relation to the other settings but it appears to make no diffference.

This needs to work reliably as my application is designed to run as a service that monitors incoming messages, if a message contains the word "Emergency" then the phone needs to notify the user immediately as they will be required to react ASAP.

I attempted to attach my work in progress but our company policy is preventing me from doing so, but if required then I can do so from home later. I will continue to work toward a solution using another service module. I've tested on two phones up to now, an LG Optimus GT540 (android 2.1) and a HTC Wildfire S (android 2.3.5). My app will be used as a "Lone Working" solution for the company I work for, it will not be released to market.

Kind regards,
RandomCoder
 

SiriusDG

Member
Licensed User
Longtime User
A few questions

Hi, great library, just started using it this morning. I have a few questions. First, I set icon="icon", and in the notification bar it is good, but in the drop down window pane, the icon is not scaling, and since the base image is larger than the actual display view, I end up seeing about 1/4 of the icon very up close. Is there something I am supposed to do to fix this, or do I have to create a different sized icon?

Second (and this may not be your issue at all) but when it plays the custom sound, the volume is very faint... does the library itself have any way to scale the volume for it's notifications, or is that all Android OS level?

Thanx!!

David
 

barx

Well-Known Member
Licensed User
Longtime User
Thanks to everyone that has used this lib and found it useful.

This library has now been deprecated and all things notification will now happen with my new library.
 

MarcWinter

New Member
Licensed User
Longtime User
Is there any way to access a sound-file that has been included with the project via the files-tab and thus been placed in the android_asset path?

an.SetCustomSound("file:///android_asset/et-voila.mp3")​

doesn't work (no sound played).

Thanks,

Marc
 
Top