5

I'm trying to implement an app with a local notification system. The system should cancel some unnecessary notifications. System.scheduleNotification works fine (it creates notifications and they work fine) but it returns nil (It supposed to return an ID). So I'm not able to cancel any notifications by notification id.

Actually the code I use is very simple. Any help would be helpful...

local nextRefreshTime = 60 -- Not always 60, its just an example
local options = {
    alert = "Some text here.",
    badge = ( native.getProperty( "applicationIconBadgeNumber" ) or 0 ) + 1,
}

notifications[#notifications+1] = system.scheduleNotification( nextRefreshTime, options )
print(notifications[#notifications]) -- Prints nil !?!
-- Another example (test)
print( system.scheduleNotification( nextRefreshTime, options ) ) -- Also prints nil !?!

p.s: I also tried system.scheduleNotification with utcTime argument.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Doğancan Arabacı
  • 3,934
  • 2
  • 17
  • 25

2 Answers2

3

Are you building the app for corona simulator? Then it won't work. Build it for Xcode simulator for testing local notifications. A sample project(from corona Sample Code) output image is shown below:

enter image description here

And the code is:

local options = {
   alert = "Wake up!",
   badge = 1,
   sound = "alarm.caf",
   custom = { msg = "bar" }
}

notificationID = system.scheduleNotification( time, options )

local displayText = "Notification using Time: " .. tostring( notificationID )
print( displayText ) -- It will print the user data

Keep Coding.............. :)

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
2

You didn't post all of your code so I have no idea what your code is doing. Make sure that in options your alert is a string. It should look something like this:

local options = {
    alert = "Wake up!",
    badge = 2,
}

Keep in mind your code is saying your system notication is adding 1 to the notification table. Right now system.scheduleNotification isn't a string, it's a table, so when your trying to print(notifications[#notification]) it would make sense that it prints nil. I think you would have to print out notification[alert] but I'm not sure. Check out this link: http://lua-users.org/wiki/TablesTutorial

Jordan Schuetz
  • 1,036
  • 1
  • 10
  • 19