Set Icon Badge while Notification - please help !!

Hi,

I would like to change the Icon Badge when the actual day did changed, even when the app is
running in the background!

I tried to work with the NotificationListener and scheduleNotification()
It works in the foreground and I could do some calculations in the NotificationListener Function,
but it does not work when the app is in the background.

Also I tried a thread like Runtime:addEventListener( “enterFrame”, gameLoop ).
But that is also not executed when the app is in the background.

Any Ideas?

in my main.lua i have:

local launchArgs = ...  
  
local options = {  
 alert = "Remind me !",  
 custom = { anythingYouWant = "Corona Rocks" }  
 }  
  
local testval = 0  
local function notificationListener( event )  
 print( "main.lua: " .. event.name ) -- "notification"  
 print( "main.lua: " .. event.custom.anythingYouWant ) -- "Corona Rocks!"  
  
 testval = testval + 1  
 native.setProperty( "applicationIconBadgeNumber",testval)  
  
 myNotification = system.scheduleNotification( 5,options) -- recall notifaction in 5 secs  
end  
Runtime:addEventListener( "notification", notificationListener )  
  
if launchArgs and launchArgs.notification then  
 notificationListener( launchArgs.notification )  
end  
  
myNotification = system.scheduleNotification( 5,options) -- first start  

For now it would be just fine, should the above code increase every 5 Seconds the Badge number,
if the app is in the foreground and also in the background. The rest I can figure out myself :slight_smile:
thx
chris
[import]uid: 4795 topic_id: 18978 reply_id: 318978[/import]

Hey, Chris, I am a newbie, and I don’t understand your code much, but I wondered if you could use system time to measure the time and make necessary changes.

http://developer.anscamobile.com/reference/os-library

Again, this is just a thought, and I may be totally clueless and may not be understanding what you’re trying to achieve… if so, please pardon me.

Naomi

Edit: Woops, I read your other post, and I see you are trying to have your app active while it’s in the background and trigger a notification. That, I have no idea how to do. [import]uid: 67217 topic_id: 18978 reply_id: 73098[/import]

@Naomi,
thanks for your post.
I use already system time to get the actual time and date.

but the point as you saw is, i don’t get any notification listener option
when the app is in the background. The Notification just appear, but without
any option to interfere (for example starting the next schedule, changing my badge etc.)

thx
chris
[import]uid: 4795 topic_id: 18978 reply_id: 73100[/import]

@peach pellen.

thanks for answer. so that feature is like
“We have a Button… but when you press it, nothing will happen”

I know Mail, Skype, Facebook… lots of app using Icon BADGE Numbers
to display information about what that app collected in the background.
If that function only works while its in the Foreground I see little options
in it to make full use of it.

Please, do something here.
Kind a thread in my app, that would always look if a notification happened
regardless if its in the fore- or background.

Till than I have to put 3 Days of Work into the box … :frowning: my fault, i really
expected thats what that feature is for and did several preparations in my
app to get the needed data I was interested to display. Seems I was bit to fast.

thx
chris

p.s. you wrote “not in such a way” … can you let me know ANY other way to
access my app while running in the background?
[import]uid: 4795 topic_id: 18978 reply_id: 73209[/import]

Argh! I accidentally deleted that post (my last one). Sorry - I meant to edit out a typo.

I actually think this may be doable now - give me a little while to check this out and I will get back to you here shortly.

Peach :slight_smile: [import]uid: 52491 topic_id: 18978 reply_id: 73219[/import]

OK, I believed there was an issue with this but it turns out it has been resolved - you CAN do this.

Try running the following code, it will show both a proper alert and the little number badge on the icon;

[lua]display.setStatusBar (display.HiddenStatusBar)

– Options for iOS
local options = {
alert = “Wake up!”,
badge = 2,
custom = { foo = “bar” }
}

– schedule using seconds from now
local notification = system.scheduleNotification( 60, options )

– schedule using UTC (Coordinated Universal Time)
local utcTime = os.date( “!*t”, os.time() + 60 )
local notification = system.scheduleNotification( utcTime, options )

local listener = function( event )
print( event.name ) – ==> “notification”
print( event.custom.foo ) – ==> “bar”
end

Runtime:addEventListener( “notification”, listener )[/lua]

Peach :slight_smile:

PS - link; http://developer.anscamobile.com/reference/index/systemschedulenotification [import]uid: 52491 topic_id: 18978 reply_id: 73221[/import]

@peach pellen.
thanks…

but with full respect. thats the same code i used and its not working in the background.
I could set a notification in the future, lets say 60 seconds.

scenario a.
when it happens and the app is in the foreground, I could for example let a alert box
appear in the local listener = function (event)

scenario b.
when it happens and the app is in the background the User will just get a Notification ,
and thats it.
I can’t for example say: Notification happened while the app was in the background,
now make a new Notification in 10 Hours and set the Badge to Value Y.

I can’t do any calculation in the moment the notification happens.
So I would have to set 300 or whatever Notifications for the Future, should I like the badge numbers counts up every day after 0 AM.

Or just think I would like to do an app like Skype, an email App whatever if there is no background functionality at all.

The Badge has to change every day to a different number, endless time, without creating x predefinitions for the next x days.

Also as it looks, I thought so much about, still don’t see any other solution, I may have to go on that :frowning: :slight_smile:

Thanks for your Time. Should u have a Multitasking Solution like for example Skype and eMail, to gather information in the background and depending on that to come able changing the Badge, let me know.

Sounds simple, seems not.

Talked a lot, thought too much. Now doing a break with corona coding. Practicing off-corona life.
Back in a few days.

Greets
Chris [import]uid: 4795 topic_id: 18978 reply_id: 73401[/import]

Hey,

This does work when the application is in the background. I tested it on an iPhone running iOS5 using the latest stable build of Corona and the badge DID appear, as did the alert.

Is that the exact same code? Is your app setup to make sure it doesn’t exit (restart) on suspend? (I’m not certain but this may have an impact.)

Peach [import]uid: 52491 topic_id: 18978 reply_id: 73419[/import]

Hi Peach,

just to clarify.

you say its working even the app is in the background.
so what function is called when the event happen?

local listener = function( event )  
 print( event.name ) -- ==\> "notification"  
 print( event.custom.foo ) -- ==\> "bar"  
end  

?? !!!

than I would also get a console print
even the app is running in the background.

For now I only got a console output from that function when
the notification happened and the app was in the foreground.

If I could do a console print while app is in background… than for
sure I also can do more calculations and work. (exactly what I would need)

please can you confirm the Listener Function is also called when
the app is in the background!

Than I would have to investigate its a problem on my side.
For now I can’t get it work.

thx
chris
[import]uid: 4795 topic_id: 18978 reply_id: 73539[/import]

Hey Guruk,

I am getting the print outs from the app in the background - yes. I just rebuilt to confirm and they’re right there in the console.

I’m on Xcode 4.2 and using an iPhone 4 running iOS5.

The code I’m using is the exact code I posted above. (Well actually I shortened the time from 60 to 20 but that’s it.)

Peach :slight_smile: [import]uid: 52491 topic_id: 18978 reply_id: 73662[/import]

HI Peach,

ok, maybe we come close, still I have some hope we mean the same.
You does say even the app is in the background you get a console output from the notification.

Right now i did a test app. (with 6 Secs Notification Delay)

a.Compiled with the 2011.704 Corona Nightly Build
b.run on a iPhone 4 V 5.0.1

  1. I opened the test app and closed it after 5 seconds.
  2. After 20 Seconds the Badge does change.
    -> Without any output in my Console.

Hmmm, it really looks for me you do some magic tricks :slight_smile:
Here is my Source Code.

io.output():setvbuf("no")  
display.setStatusBar( display.HiddenStatusBar )  
print ("---- Simple Notification")   
-- Options for iOS   
local options = {  
 alert = "Wake up!",  
 badge = 2,  
 custom = { foo = "bar" }  
}  
   
-- schedule using seconds from now  
-- here the listener comes only called when the Notification happens.  
local notification = system.scheduleNotification( 10, options )  
   
-- schedule using UTC (Coordinated Universal Time)  
-- here, the Listener comes already called after 3 seconds, even the  
-- Notification itself does happen 7 seconds later (10 seconds defined)  
  
--local utcTime = os.date( "!\*t", os.time() + 10 )  
--local notification = system.scheduleNotification( utcTime, options )  
   
local listener = function( event )  
 print( event.name ) -- ==\> "notification"  
 print( event.custom.foo ) -- ==\> "bar"  
end  
   
Runtime:addEventListener( "notification", listener )  

what I saw is when the scheduleNotification is called with a pre time in seconds.
The listener (and so console output) does happen as expected 10 seconds after start.

While instead when a os.date time is predefined, the listener comes already called
3 seconds after start (even the app is in the background, right)
and the final notification 7 seconds later (10 after start)
Thats what you may have misinterpreted as “I see something in the Console”.
I made another example;

  
io.output():setvbuf("no")  
display.setStatusBar( display.HiddenStatusBar )  
   
-- When the app is in the Foreground it does  
-- create a new notification when one happend.  
-- that does not work when the app is in the background.  
-- Options for iOS   
-- Badge not used, as it come set at times  
-- badge Nr. is right now unknown  
local options = {  
 alert = "First created Notification",  
 custom = { foo = "bar" }  
}  
   
math.randomseed(1)  
  
local myvariable = 0  
   
local listener = function( event )  
 -- Console Output  
 print( event.name ) -- ==\> "notification"  
 print( event.custom.foo ) -- ==\> "bar;  
  
 -- Get Random Number  
 randomnum = math.random(0,999)  
 -- in Real each day 0am, the app does check how  
 -- many days left to a selected event in its database  
  
 -- set badge  
 native.setProperty( "applicationIconBadgeNumber",randomnum)  
  
 -- create next notification in 10 Seconds (in real next notification in 24 Hrs)  
 -- this is not called when app in Background !  
 options.alert = "Last Badge was Notification Nr: " .. randomnum  
 local notification = system.scheduleNotification( 10, options )  
  
  
end  
   
Runtime:addEventListener( "notification", listener )  
  
-- start First Notification after 10 Seconds  
local notification = system.scheduleNotification( 10, options )  

that would create anyhow every 10 seconds a new Notification.
Works fine when the app is in the foreground.
When the app goes just once into background it stops working and also do not
continue when app is in foreground again.
LAST TEST

io.output():setvbuf("no")  
display.setStatusBar( display.HiddenStatusBar )  
print ("---- Repeated Notification")   
-- Options for iOS   
local options = {  
 alert = "Wake up!",  
 badge = 2,  
 custom = { foo = "bar" }  
}  
   
local utcTime = os.date( "!\*t", os.time() + 10 )  
local notification = system.scheduleNotification( utcTime, options )  
   
local listener = function( event )  
 print( event.name ) -- ==\> "notification"  
 print( event.custom.foo ) -- ==\> "bar"  
 local utcTime = os.date( "!\*t", os.time() + 10 )  
 local notification = system.scheduleNotification( utcTime, options )  
  
end  
   
Runtime:addEventListener( "notification", listener )  

here I would expect, as I saw a Console Output, that every 10 Seconds
a new Notification is created. But instead, it does create every second a new notification,
while in Foreground, and while in Background, same as with the other tests… it stops working

Finally, I REALLY WISH and HOPE you are right :slight_smile: and its just a misunderstanding of mine,
make me understand please :slight_smile:

That API looks for me now still VERY buggy and should not be published like that.
Especially when worked with os.time it seems that the even listener is called immediately
and so there is anyhow no change to interfere when it really happens.

Should u able to do a simple app:
-App starts and every 10 Seconds the badge counter gets a random number.

  • Just the Badge Number, no need for an Alert
  • The Badge Number does change several time, while the app is in the Background.
    Without predefining 1 Mio Counters for the next years to count up.

THAN I will just go deep into my knees and say Thank you !

Looking forward to hear soon from you. I really need to have that running soon.

Greets
Chris
[import]uid: 4795 topic_id: 18978 reply_id: 73798[/import]

I am able to get the first alert printing however attempts to do similar to what you were doing (a new alert every 10 seconds) failed to print after the initial test, although I didn’t get any errors.

It looks as though it may be worth filing a bug report I’m afraid - preferably using the code from your last test.

Sorry :frowning:

Peach. [import]uid: 52491 topic_id: 18978 reply_id: 73863[/import]

I made a post in bug reports
http://developer.anscamobile.com/forum/2011/12/15/notification-listener-while-app-background-not-called

have a look and please keep on…

thx
chris
[import]uid: 4795 topic_id: 18978 reply_id: 73941[/import]

Hi Peach,

any news about that… the Badge Feature is useless… should I be unable to set a new value while app is running in the background… and not just setting the next value… its about when that value is set… i have to calculate in realtime whats the next value.

chris
[import]uid: 4795 topic_id: 18978 reply_id: 79750[/import]

I’m not involved with this bug, you’d want to check out fogbugz or any emails you might have received.

Peach [import]uid: 52491 topic_id: 18978 reply_id: 79800[/import]

Hey Chris,

Did you ever get a resolution to this? I have the exact same issue and the problem still appears to exist.

Cheers,

  • Will [import]uid: 61273 topic_id: 18978 reply_id: 96795[/import]

hi Peach!!!

any news about that

its really a needed feature!!

see also
http://developer.anscamobile.com/forum/2012/02/29/notification-badges-multitasking#comment-105766

http://developer.anscamobile.com/forum/2012/04/13/notification-not-working
thanks please… push that topic !!

chris
[import]uid: 4795 topic_id: 18978 reply_id: 105768[/import]

.hallloooooo
peach you said you would keep an eye on that
i even send you once the BUG Number

Never heard from you again :frowning:

[import]uid: 4795 topic_id: 18978 reply_id: 113821[/import]

What do you mean you never heard from me again? I can’t recall us being engaged in communications at any point - nor can I find any emails from your address.

You need to reread what I said in post #14 - I’m *not* involved with this bug. If you want to know the status of any bug simply give me the number and I will gladly look it up for you.

Keep in mind am not the only member of the team - you CAN respond to the email you received when you filed the bug. [import]uid: 52491 topic_id: 18978 reply_id: 113886[/import]

i answered you in the other thread.
sorry to bother you.
hope you can also understand my situation as that feature is announced as working sinces months.
[import]uid: 4795 topic_id: 18978 reply_id: 113894[/import]