B4A Library Custom Notifcations

This library is now in a usable condition and may evolve as requirements increase.

License:
You are free to use this library in commercial/non-commericial projects. If you use it for something cool, it would be nice if you could post a screenshot of your app using this library on the forum (probably Share Your Creations).

You may use the source code and add new functions to it. If you do so, you are required to post the improvements/additions in this thread.

Usage:
1. The Layout of the Notification
This is defined in an xml layout file. Create your layout, name it to "cm.xml" and copy it to your projects Objects/res/Layout folder. Mark it as read-only. See XML Resources for further information.
Make sure you give an id to each view in the xml, because in order to set these views we will use the same id.
B4X:
<TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
If you have trouble with the xml file (like I did), do Clean Project. Make the xml file in utf-8. warwound used dreamweaver for mine.


2. Add the Library to your project
Right hand pane, contains a library tab. Select "CustomNotifications"

3. B4A Code
Create a notification object as you normally would.
Then to customize it, do:

B4X:
Dim cn as CustomNotification
Dim n As Notification 
n.Initialize 
n.Icon = "icon"
n.SetInfo("Hello","hello","")
cn.setCustomView(n)
cn.setText(n,"title","NewTitle")
cn.setText(n,"text","NewText")
cn.setTextColor(n,"text",Colors.Red)
cn.setTextSize(n,"title",20)
cn.setImage(n,"image",c.Bitmap)

The id used here to set the properties, is the same id defined in your xml layout.

Still having problems?
Before posting a question, check and double check your xml file.
Most errors happen due to your xml file not being added as a resource. Look in the Objects/gen/<packagename>/R.java file to see it the xml file is being added into it.
Then read this link carefully and thoroughly: Custom Notifcation Layouts
Also dont try to put a listview or a scrollview in a notification. It only supports buttons, labels, progressbars, imageviews and panels. There may be other restrictions on what it supports.


XML Resources:
Learn about xml layouts and styles. Each ROM has a different style so it could cause your text to be invisible.
Custom Notifcation Layouts
LinearLayout


Shortcomings:
Events based on individual view clicks. This requires pending intents, so far I havent been able to make it work. Please let me know if you have a solution.

Credits:
There are too many people who deserve credits for this.
Erel (the platform), Barx(the enlightenment), corwin42(the functions), warwound(the xml file), NJDude(the banter), XverhelstX(the nagging), JonPM(the initial inspiration) and many others I may have forgot to mention.

___________________________________________
Important Notice:
This library is depreciated and is left here as an archive/legacy reasons. This has been superseded by this library: Custom Notification Library: Barx
 

Attachments

  • ExampleLayout.zip
    433 bytes · Views: 780
  • CMExample.zip
    4.9 KB · Views: 758
  • CNLibrary.zip
    4.3 KB · Views: 831
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
Thank you for sharing this library!

I've created a wiki user for you, so you should be able to add it to the list of libraries.
Thanks Erel. I have added it to the wiki.

Thank you thedesolatesoul.
The library works great!
This is very useful!
Yes, I hope people use it and make their lives better :)
Good to know it worked for you.
 

barx

Well-Known Member
Licensed User
Longtime User
Darn you beat me to it. lol

Well done!!
 

walterf25

Expert
Licensed User
Longtime User
Changing title color

Great library, however i have a question, is there a way to change the text color of the first line in the notification, i only see how to set the second line's text color, on my Motorola photon the first line is grey and i have changed the second line to blue.

notification.jpg

thanks,
Walter
 

JonPM

Well-Known Member
Licensed User
Longtime User
Awesome! I bow down to you good Sir! Can't wait to try it out

Sent from my DROIDX
 

thedesolatesoul

Expert
Licensed User
Longtime User
Darn you beat me to it. lol
Well done!!
Lots of room for improvement, you still have a chance.

Great library, however i have a question, is there a way to change the text color of the first line in the notification, i only see how to set the second line's text color, on my Motorola photon the first line is grey and i have changed the second line to blue.

View attachment 11195

thanks,
Walter
Walter, the first line is called 'title' and the second line is 'text'.
To change the color of the first line in the sample add:
B4X:
cn.setTextColor(n,"title",Colors.Red)


Awesome! I bow down to you good Sir! Can't wait to try it out
I need you to persist for the events thing. We need to find a solution for that. :)

Great work thedesolatesoul!
I'm going to test it out soon ;)
Tomas
Thanks Tomas. Means a lot to me :)
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
The attached project demostrates on of the many uses of this new cool library, this sample will download a file and will update the ProgressBar on the CustomNotification as well as the one on the Activity.



NOTE: Sorry about the choice of the downloaded file but I needed something quick. :D
 

Attachments

  • CustomNotificationSample.zip
    11.4 KB · Views: 411
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
The attached project demostrates on of the many uses of this new cool library, this sample will download a file and will update the ProgressBar on the CustomNotification as well as the one on the Activity.

NOTE: Sorry about the choice of the downloaded file but I needed something quick. :D
Thanks NJDude. This will probably be the most example for this library as it is the most common use case. Also, thanks for figuring out the xml for the progressbar. :)

Nice TDS, and good sample NJDUDE :sign0142: :sign0098:
Thanks vb1992, now if I can just figure out the events, you will be able to turn on/off your wifi to your prefered network with this :)
 

barx

Well-Known Member
Licensed User
Longtime User
What does setAction do?
 

walterf25

Expert
Licensed User
Longtime User
Custom Notification

Hello all, i'm trying to set a custom notification from a service module, i'm getting an error when i run m app.

android.app.RemoteServiceException: Bad notification posted from package com.dandre.apps: Couldn't expand RemoteViews for: StatusBarNotification(package=com.dandre.apps id=1 tag=null notification=Notification(vibrate=default,sound=default,defaults=0xffffffff,flags=0x0))

Does anyone have any idea why this is, below is the code i'm using when the service module is called

B4X:
Sub Service_Create
'cn.setImage(n, "image", c.bitmap)
timerservice.Initialize("TimerService", 30000)
DoEvents
Dim cn As CustomNotification
Dim n As Notification
n.Initialize 
n.Icon = "icon"
n.SetInfo("hello", "hello","")
cn.setCustomView(n)
cn.setText(n, "Title", "Load File")
cn.setText(n, "text", "loaded")
cn.setTextColor(n, "text", Colors.Blue)
cn.setTextColor(n, "title", Colors.Red)
cn.setTextSize(n, "title", 20)
n.Notify(1)
End Sub


any help will be greatly appreciated guys, I do have the cm.xml file in the objects folder/res/layout and it is set to Read only.

Help anyone,
Walter
 

pfillion

Member
Licensed User
Longtime User
Hi,

I know this thread is old and that this library has been depreciated but...

I was working on a projet which involved notification. I wanted to put actions in my notification using a custom layout with images. I found this library but also found out that it did not support actions via pending intents.

I looked around and found out that BarX did an amazing job with his library Custom Notifciation and had also written a new Notification Builder Library. I wanted to use BarX libraries but found out that you need to support API 15 for Custom Notification and API 16+ for Notification Builder Library AddAction method.

Since I wanted to do actions but still be compatible with lower API versions, I contacted thedesolatesoul who sent me the source code of its Custom Notification library. I finally got the setAction to work using pending intents and was able call an activity with an image! I also added a SetAction2 method that allows you to call a service.

Here is version 1.01 attached.

I also renamed the library to CustomNotificationBasic to make sure it won't interfere with BarX library of the same name.

Let me know how it works for you and if it can be of use... any feedback is welcome.

I will try to send my code as an example that show how to add buttons in the notification and catch which button has been pressed in an activity or service using the intent extras...

-Pr
 

Attachments

  • CustomNotificationBasic.zip
    4.1 KB · Views: 253
Last edited:

pfillion

Member
Licensed User
Longtime User
Hi,

This example is to show you how to do custom layout in notification with selectable images using thedesolatesoul library that I fixed to support actions (pending intent).

This is a working solution if you want to support selectable images with actions in the notification while preserving compatibility with API4+.

Special thanks to thedesolatesoul who sent me the source code of his library.

-Pr
 

Attachments

  • 2013-10-13 20.06.37.png
    2013-10-13 20.06.37.png
    154.1 KB · Views: 356
  • CNExample1.01.zip
    35.7 KB · Views: 286
Last edited:
Top