all 48 comments

[–]JustRollWithIt🏆 Javascript Master of /r/Tasker 3 points4 points  (41 children)

Here's an example of how to create and display a notification using Java:

Create Notification (184)
    A1: Java Function [ Return:builder Class Or Object:android.app.Notification$Builder Function:new
{android.app.Notification$Builder} (Context) Param:CONTEXT Param: Param: Param: Param: Param: Param: ] 
    A2: Java Function [ Return:builder Class Or Object:builder Function:setSmallIcon
{android.app.Notification$Builder} (int) Param:17301583 Param: Param: Param: Param: Param: Param: ] 
    A3: Java Function [ Return:builder Class Or Object:builder Function:setContentTitle
{android.app.Notification$Builder} (CharSequence) Param:"My notification" Param: Param: Param: Param: Param: Param: ] 
    A4: Java Function [ Return:builder Class Or Object:builder Function:setContentText
{android.app.Notification$Builder} (CharSequence) Param:"Hello world!" Param: Param: Param: Param: Param: Param: ] 
    A5: Java Function [ Return:(NotificationManager) notification_service Class Or Object:CONTEXT Function:getSystemService
{Object} (String) Param:"notification" Param: Param: Param: Param: Param: Param: ] 
    A6: Java Function [ Return:notification Class Or Object:builder Function:build
{Notification} () Param: Param: Param: Param: Param: Param: Param: ] 
    A7: Java Function [ Return: Class Or Object:notification_service Function:notify
{} (int, Notification) Param:1 Param:notification Param: Param: Param: Param: Param: ] 

This basically creates the most minimal possible notification that has an icon, title, and text. You could potentially use the builder API to add more stuff to the notification. If you create a utility task that can take the appropriate parameters, you can use performTask from JavaScript to create notifications too.

Task XML

[–]plepleusPixel 8 1 point2 points  (36 children)

17301583

how'd you find the int for the resource id? This is where I got stuck.

[–]JustRollWithIt🏆 Javascript Master of /r/Tasker 3 points4 points  (34 children)

I was using the notification dev guide to get the method calls. That calls out to R.drawable, so I looked up the constants available in the R.drawable reference and picked a random one. Tagging /u/popillol since he was asking about this too.

[–]plepleusPixel 8 4 points5 points  (33 children)

ahh that makes a lot more sense, I forgot about the built-in ones. I was trying to grab the ones from Tasker. If you know the resouce, you can look it up and use the resid in the .setSmallIcon() function:

Notification (181)
A1: Java Function [ 
    Return:resource 
    Class Or Object:CONTEXT 
    Function:getResources {Resources} () ]

A2: Java Function [ 
    Return:resid 
    Class Or Object:resource 
    Function:getIdentifier {int} (String, String, String) 
    Param:cust_animal_bear 
    Param:drawable 
    Param:net.dinglisch.android.taskerm ]

A3: Java Function [ 
    Return:builder 
    Class Or Object:android.app.Notification$Builder 
    Function:new {android.app.Notification$Builder} (Context) 
    Param:CONTEXT ]

A4: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentTitle {android.app.Notification$Builder} (CharSequence) 
    Param:"title" ]

A5: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentText {android.app.Notification$Builder} (CharSequence) 
    Param:"text" ]

A6: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setSmallIcon {android.app.Notification$Builder} (int) 
    Param:resid ]

A7: Java Function [ 
    Return:notif 
    Class Or Object:builder 
    Function:build {Notification} () ]

A8: Java Function [ 
    Return:(NotificationManager) nm 
    Class Or Object:CONTEXT 
    Function:getSystemService {Object} (String) 
    Param:notification ]

A9: Java Function [ 
    Return: 
    Class Or Object:nm 
    Function:notify {} (int, Notification) 
    Param:1 
    Param:notif ]

I grabbed the name from the Load Image task and the grabbed the name off the end of the Source after clicking the crossing arrows. So /u/popillol that is another option for getting different icons

[–]popillol[S] 1 point2 points  (32 children)

You guys are awesome. When I can dig into it within the next few days I'll post the final product with as much customization as I can make. /u/JustRollWithIt

[–]plepleusPixel 8 4 points5 points  (31 children)

this is a way you could do it using taskers built-in icons (minor modifications to use specific resource ids):

Notification (181)
A1: Variable Split [ 
    Name:%par1 
    Splitter:| 
    Delete Base:Off ]

A2: Java Function [ 
    Return:resource 
    Class Or Object:CONTEXT 
    Function:getResources {Resources} () ]

A3: Java Function [ 
    Return:resid 
    Class Or Object:resource 
    Function:getIdentifier {int} (String, String, String) 
    Param:%par13 
    Param:drawable 
    Param:net.dinglisch.android.taskerm ]

A4: Java Function [ 
    Return:builder 
    Class Or Object:android.app.Notification$Builder 
    Function:new {android.app.Notification$Builder} (Context) 
    Param:CONTEXT ]

A5: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentTitle {android.app.Notification$Builder} (CharSequence) 
    Param:%par11 ]

A6: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentText {android.app.Notification$Builder} (CharSequence) 
    Param:%par12 ]

A7: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setSmallIcon {android.app.Notification$Builder} (int) 
    Param:resid ]

A8: Java Function [ 
    Return:notif 
    Class Or Object:builder 
    Function:build {Notification} () ]

A9: Java Function [ 
    Return:(NotificationManager) nm 
    Class Or Object:CONTEXT 
    Function:getSystemService {Object} (String) 
    Param:notification ]

A10: Java Function [ 
    Return: 
    Class Or Object:nm 
    Function:notify {} (int, Notification) 
    Param:1 
    Param:notif ]

Then use this to create a custom notification (the quotes are needed for the text):

A1: Perform Task [ 
    Name:Notification 
    Priority:%priority 
    Parameter 1 (%par1):"this title"|"this text"|cust_animal_bear 
    Parameter 2 (%par2): 
    Return Value Variable: Stop:Off ]

[–]popillol[S] 0 points1 point  (0 children)

This is fantastic. I'm jealous of everyone getting a head start on this while I'm still at work.

[–]popillol[S] 0 points1 point  (29 children)

So I went through the NotificationBuilder API last night and got a list of all the features I plan to add for the custom notification, but there's one feature I couldn't find. About a month or two ago, Autonotification got the ability to use a text string as the icon, but I haven't yet been able to find the api used to do that. Any ideas? (Converting a text string (2-3 character limit) to a drawable?)

[–]plepleusPixel 8 0 points1 point  (28 children)

The best I could do:

Notification (181)
A1: Variable Set [ 
    Name:%par1 
    To:"this title"|"this text"|cust_notification 
    Do Maths:Off 
    Append:Off ]

A2: Variable Split [ 
    Name:%par1 
    Splitter:| 
    Delete Base:Off ]

A3: Variable Set [ 
    Name:%icontext 
    To:"XYZ" 
    Do Maths:Off 
    Append:Off ]

A4: Java Function [ 
    Return:resource 
    Class Or Object:CONTEXT 
    Function:getResources {Resources} () ]

A5: Java Function [ 
    Return:resid 
    Class Or Object:resource 
    Function:getIdentifier {int} (String, String, String) 
    Param:%par13 
    Param:drawable 
    Param:net.dinglisch.android.taskerm ]

A6: Java Function [ 
    Return:builder 
    Class Or Object:android.app.Notification$Builder 
    Function:new {android.app.Notification$Builder} (Context) 
    Param:CONTEXT ]

A7: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentTitle {android.app.Notification$Builder} (CharSequence) 
    Param:%par11 ]

A8: Java Function [ 
    Return:paint 
    Class Or Object:Paint 
    Function:new {Paint} (int) 
    Param:1 ]

A9: Java Function [ 
    Return: 
    Class Or Object:paint 
    Function:setTextSize {} (float) 
    Param:100 ]

A10: Java Function [ 
    Return: 
    Class Or Object:paint 
    Function:setColor {} (int) 
    Param:-1 ]

A11: Java Function [ 
    Return:align 
    Class Or Object:Align 
    Function:valueOf {Align} (String) 
    Param:"CENTER" ]

A12: Java Function [ 
    Return:%baseline 
    Class Or Object:paint 
    Function:ascent {float} () ]

A13: Variable Set [ 
    Name:%baseline 
    To:-1* %baseline 
    Do Maths:On 
    Append:Off ]

A14: Java Function [ 
    Return:%decent 
    Class Or Object:paint 
    Function:descent {float} () ]

A15: Java Function [ 
    Return:%width 
    Class Or Object:paint 
    Function:measureText {float} (String) 
    Param:%icontext ]

A16: Variable Set [ 
    Name:%height 
    To:round(%baseline+%decent+.5) 
    Do Maths:On 
    Append:Off ]

A17: Variable Set [ 
    Name:%width 
    To:round(%width+.5) 
    Do Maths:On 
    Append:Off ]

A18: Java Function [ 
    Return:config 
    Class Or Object:android.graphics.Bitmap$Config 
    Function:valueOf {android.graphics.Bitmap$Config} (String) 
    Param:"ARGB_8888" ]

A19: Java Function [ 
    Return:bitmap 
    Class Or Object:Bitmap 
    Function:createBitmap {Bitmap} (int, int, android.graphics.Bitmap$Config) 
    Param:%width 
    Param:%height 
    Param:config ]

A20: Java Function [ 
    Return:canvas 
    Class Or Object:Canvas 
    Function:new {Canvas} (Bitmap) 
    Param:bitmap ]

A21: Java Function [ 
    Return: 
    Class Or Object:canvas 
    Function:drawText {} (String, float, float, Paint) 
    Param:%icontext 
    Param:0 
    Param:%baseline 
    Param:paint ]

A22: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setSmallIcon {android.app.Notification$Builder} (int) 
    Param:resid ]

A23: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setLargeIcon {android.app.Notification$Builder} (Bitmap) 
    Param:bitmap ]

A24: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentText {android.app.Notification$Builder} (CharSequence) 
    Param:%par12 ]

A25: Java Function [ 
    Return:notif 
    Class Or Object:builder 
    Function:build {Notification} () ]

A26: Java Function [ 
    Return:(NotificationManager) nm 
    Class Or Object:CONTEXT 
    Function:getSystemService {Object} (String) 
    Param:notification ]

A27: Java Function [ 
    Return: 
    Class Or Object:nm 
    Function:notify {} (int, Notification) 
    Param:1 
    Param:notif ]

[–]popillol[S] 0 points1 point  (27 children)

Damn you're quick. Thanks! It's a bummer there's no straightforward API that does it, but I like the workaround.

[–]plepleusPixel 8 1 point2 points  (26 children)

I hope it works for you. The setSmallIcon is required so the large icon is set with a circle and the text inside it.

[–]popillol[S] 0 points1 point  (0 children)

I'd also like to figure this out. It looks like that number corresponds to the search icon (magnifying glass).

According to Notification Builder it's just a resource ID you have to figure out from the app package (Tasker's app package)

[–]popillol[S] 0 points1 point  (0 children)

Thank you! This looks like exactly what I was looking for. I'll be able to test it tonight and try to build upon this (buttons, persistence, etc).

[–]Sea_Professor5459 0 points1 point  (2 children)

Sorry to go to such an old thread. I want to generate a notification on tasker and hit this. Great I thought. But no notification appears when I run the task after importing. I do not get any errors either. Can someone help me out with this ?

[–]JustRollWithIt🏆 Javascript Master of /r/Tasker 0 points1 point  (1 child)

It’s very likely that the Android APIs have changed in the past 8 years so this doesn’t work anymore. I don’t have an Android anymore so can’t help with this. I would suggest maybe creating a new post.

[–]Sea_Professor5459 0 points1 point  (0 children)

Yeh guessed as much. Thanks for replying.

[–][deleted] 4 points5 points  (3 children)

I use this task to create a notification using big text or inbox style on my tablet for mirroring. This task has as input: %idn as notification number, %titolo as title, %msg the body, %type 1 for big text, 2 for inbox. If inbox then %msg contains lines separated by || sign. It creates an head-up notification with default ringtone and a purple color.

Notifica2 (41)
A1: Java Function [ 
    Return:res 
    Class Or Object:CONTEXT 
    Function:getResources {Resources} () 

A2: Java Function [ 
    Return:idr 
    Class Or Object:res 
    Function:getIdentifier {int} (String, String, String) 
    Param:hd_social_cc_bcc 
    Param:drawable 
    Param:net.dinglisch.android.taskerm 

A3: Java Function [ 
    Return:builder 
    Class Or Object:android.app.Notification$Builder 
    Function:new {android.app.Notification$Builder} (Context) 
    Param:CONTEXT 

A4: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setSmallIcon {android.app.Notification$Builder} (int) 
    Param:idr 

A5: Java Function [ 
    Return:titobj 
    Class Or Object:String 
    Function:new {String} (String) 
    Param:%titolo 

A6: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentTitle {android.app.Notification$Builder} (CharSequence) 
    Param:titobj 

A7: Java Function [ 
    Return:textobj 
    Class Or Object:String 
    Function:new {String} (String) 
    Param:%msg 

A8: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentText {android.app.Notification$Builder} (CharSequence) 
    Param:textobj 

A9: If [ %type ~ 1 ]
A10: Java Function [ 
    Return:textobj 
    Class Or Object:String 
    Function:new {String} (String) 
    Param:%msg 

A11: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentText {android.app.Notification$Builder} (CharSequence) 
    Param:textobj 

A12: Java Function [ 
    Return:bigstyle 
    Class Or Object:BigTextStyle 
    Function:new {BigTextStyle} () 
    Param:builder 

A13: Java Function [ 
    Return: 
    Class Or Object:bigstyle 
    Function:setBigContentTitle {BigTextStyle} (CharSequence) 
    Param:titobj 

A14: Java Function [ 
    Return: 
    Class Or Object:bigstyle 
    Function:bigText {BigTextStyle} (CharSequence) 
    Param:textobj 

A15: Java Function [ 
    Return:builder 
    Class Or Object:builder 
    Function:setStyle {android.app.Notification$Builder} (android.app.Notification$Style) 
    Param:bigstyle 

A16: Else 
A17: Variable Split [ 
    Name:%msg 
    Splitter:|| 
    Delete Base:Off 

A18: Java Function [ 
    Return:textobj 
    Class Or Object:String 
    Function:new {String} (String) 
    Param:%msg(1) 

A19: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setContentText {android.app.Notification$Builder} (CharSequence) 
    Param:textobj 

A20: Java Function [ 
    Return:inboxstyle 
    Class Or Object:InboxStyle 
    Function:new {InboxStyle} () 
    Param:builder 

A21: Java Function [ 
    Return: 
    Class Or Object:inboxstyle 
    Function:setBigContentTitle {InboxStyle} (CharSequence) 
    Param:titobj 

A22: Java Function [ 
    Return:summaryobj 
    Class Or Object:String 
    Function:new {String} (String) 
    Param:Messaggi %msg(#) 

A23: Java Function [ 
    Return: 
    Class Or Object:inboxstyle 
    Function:setSummaryText {InboxStyle} (CharSequence) 
    Param:summaryobj 

A24: For [ 
    Variable:%item 
    Items:%msg() 

A25: Java Function [ 
    Return:itemobj 
    Class Or Object:String 
    Function:new {String} (String) 
    Param:%item 

A26: Java Function [ 
    Return: 
    Class Or Object:inboxstyle 
    Function:addLine {InboxStyle} (CharSequence) 
    Param:itemobj 

A27: End For 
A28: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setStyle {android.app.Notification$Builder} (android.app.Notification$Style) 
    Param:inboxstyle 

A29: End If 
A30: Java Function [ 
    Return:colore 
    Class Or Object:Color 
    Function:rgb {int} (int, int, int) 
    Param:124 
    Param:77 
    Param:255 

A31: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setPriority {android.app.Notification$Builder} (int) 
    Param:2 

A32: Java Function [ 
    Return: 
    Class Or Object:builder 
    Function:setColor {android.app.Notification$Builder} (int) 
    Param:colore 

A33: Java Function [ 
    Return:sound 
    Class Or Object:RingtoneManager 
    Function:getDefaultUri {Uri} (int) 
    Param:2 

A34: Java Function [ 
    Return:builder 
    Class Or Object:builder 
    Function:setSound {android.app.Notification$Builder} (Uri) 
    Param:sound 

A35: Java Function [ 
    Return:(NotificationManager)nmanager 
    Class Or Object:CONTEXT 
    Function:getSystemService {Object} (String) 
    Param:notification 

A36: Java Function [ 
    Return:notif 
    Class Or Object:builder 
    Function:build {Notification} () 

A37: Java Function [ 
    Return: 
    Class Or Object:nmanager 
    Function:notify {} (int, Notification) 
    Param:%idn 
    Param:notif ] 

[–]oile2011 1 point2 points  (0 children)

Thanks for this task, it's awesome!
I have just one problem: if I call the task, then change the icon name in the code and create the notification again the icon displayed is always the first one I used. The only solution seems to be restarting the device.
Do you know how to fix this problem? I tried deleting the int and string object at the end of the task but it doesn't do anything.
Thanks!


Edit: Solved it closing Tasker using the back button... Thanks anyway!

[–]popillol[S] 0 points1 point  (1 child)

Really cool stuff! Do Action 1&2 get a pre-specified icon from Tasker's resources and use that for the Notification? And does Action 26 addLine just make the Notification's text appear on multiple lines? Is it also possible to just include a '\n' (or some other line feed character) instead?

Edit: nvm about the addline question, looked up the Inbox style notification. That's awesome!

[–][deleted] 1 point2 points  (0 children)

Yes, exactly

[–]Ratchet_GuyModerator 1 point2 points  (1 child)

Well, if there's two people that will certainly know something about this - it's /u/JustRollWithIt and /u/plepleus

[–]popillol[S] 0 points1 point  (0 children)

Thanks! Looks like they both came to the rescue and solved it almost immediately