Notifications v2 -- Badge Icons

Hi,

I am a bit confused on the process of updating a badge for iOS using push notifications. I see in the firebase portal that when I send out a notification, there is an option to assign a badge number but when I open my app the badge does not disappear and when I send multiple notifications, the badge number does not add up. I am using firebase cloud messaging and here is my code which is in the main.lua:

local function notificationListener( event ) if ( event.type == "local" ) then -- Handle the local notification local badgeNum = native.getProperty( "applicationIconBadgeNumber" ) if ( badgeNum \> 0 ) then badgeNum = badgeNum - 1 native.setProperty( "applicationIconBadgeNumber", badgeNum ) end elseif ( event.type == "remoteRegistration" ) then -- Code to register your device with the service local deviceToken = event.token elseif ( event.type == "remote" ) then -- Handle the push notification if ( event.badge and event.badge \> 0 ) then native.setProperty( "applicationIconBadgeNumber", event.badge - 1 ) end end end Runtime:addEventListener( "notification", notificationListener ) local launchArgs = ... if ( launchArgs and launchArgs.notification ) then notificationListener( launchArgs.notification ) notificationListener( launchArgs.badge ) end

Hi @rdvitali98,

I suppose let me ask the “super obvious” question first: did you set up everything for badges as noted here?

https://docs.coronalabs.com/guide/events/appNotification/index.html#ios-setup

Best regards,

Brent

If you are talking about config.lua, yes. I have updated my code here and as of right now neither when the app is in the background or it is closed when I click on a notification the badge icon does not decrease. Also, the badge icon does not go up by a value of one if there are more notifications. I may be confused but is it only possible to determine the badge number based on what the message in firebase defines it as or should the app be able to add a value of one to every new notification? This is my new code I updated it a bit. This is all in my main.lua.

local function notificationListener( event ) if ( event.type == "local" ) then -- Handle the local notification local badgeNum = native.getProperty( "applicationIconBadgeNumber" ) if ( badgeNum \> 0 ) then badgeNum = badgeNum - 1 native.setProperty( "applicationIconBadgeNumber", badgeNum ) end elseif ( event.type == "remoteRegistration" ) then -- Code to register your device with the service local deviceToken = event.token elseif ( event.type == "remote" ) then -- Handle the push notification if ( event.badge and event.badge \> 0 ) then badgeNum = badgeNum - event.badge native.setProperty( "applicationIconBadgeNumber", badgeNum ) end end end Runtime:addEventListener( "notification", notificationListener ) local launchArgs = ... if ( launchArgs and launchArgs.notification ) then notificationListener( launchArgs.notification ) notificationListener( launchArgs.badge ) end

Hi @rdvitali98,

Do you mind posting your config.lua content here as well?

Thanks,

Brent

Sure. Here it is. I am just confused about why the badge number is not going down when I click on a notification and why when I get multiple notifications it does not sum up. 

-- -- For more information on config.lua see the Corona SDK Project Configuration Guide at: -- https://docs.coronalabs.com/guide/basics/configSettings -- application = { content = { scale = "letterBox", width = display.pixelWidth, height = display.pixelHeight, fps = 60, --[[imageSuffix = { ["@2x"] = 2, ["@4x"] = 4, }, --]] }, notification = { iphone = { types = { "badge", "sound", "alert" } }, } }

Hi again,

I assume you called “notifications.registerForPushNotifications()” and included the “useFCM” parameter with a value of true?

Best regards,

Brent

Yes. I am able to receive notifications. That is not the issue. My issue is pertaining to badge number on iOS. The badge number is not decreasing when I click on the notification and the badge number is not increasing by a value of 1 when I get a new notification.

Hi @rdvitali98,

Apologies for asking for yet more details, but can you post your “build.settings” here as well?

Thanks,

Brent

build.settings:

-- -- For more information on build.settings see the Corona SDK Build Guide at: -- https://docs.coronalabs.com/guide/distribution/buildSettings -- settings = { orientation = { -- Supported values for orientation: -- portrait, portraitUpsideDown, landscapeLeft, landscapeRight default = "portrait", supported = { "portrait", }, }, plugins = { ["plugin.notifications.v2"] = { publisherId = "com.coronalabs" }, ["shared.google.play.services.gcm"] = { publisherId = "com.coronalabs", supportedPlatforms = { android=true } }, ["plugin.firebase"] = { publisherId = "tech.scotth", }, ["plugin.quickAction"] = { publisherId = "tech.scotth", supportedPlatforms = { iphone=true }, }, --[[["plugin.firebaseAnalytics"] = { publisherId = "tech.scotth", },]] ["plugin.tapticEngine"] = { publisherId = "tech.scotth", supportedPlatforms = { iphone=true }, }, }, excludeFiles = { -- Include only the necessary icon files on each platform iphone = { "Icon-\*dpi.png", }, android = { "Icon.png", "Icon-Small-\*.png", "Icon\*@2x.png", }, }, -- -- iOS Section -- iphone = { plist = { NSAppTransportSecurity = { NSExceptionDomains = { ["barcodes4.me"] = { NSIncludesSubdomains = true, NSExceptionAllowsInsecureHTTPLoads = true, }, ["www.packer.edu"] = { NSIncludesSubdomains = true, NSExceptionAllowsInsecureHTTPLoads = true, }, ["connect.packer.edu"] = { NSIncludesSubdomains = true, NSExceptionAllowsInsecureHTTPLoads = true, }, }, }, UIStatusBarHidden = false, UIPrerenderedIcon = true, -- set to false for "shine" overlay --UIApplicationExitsOnSuspend = true, -- uncomment to quit app on suspend UIBackgroundModes = {"remote-notification"}, FirebaseAppDelegateProxyEnabled = false, NSCameraUsageDescription = "This app would like to access the camera.", NSPhotoLibraryUsageDescription = "This app would like to access the photo library.", NSCalendarsUsageDescription = "Not used", CFBundleIconFiles = { "IconCopy.png", "Icon@2x.png", "Icon-167.png", "Icon-60.png", "Icon-60@2x.png", "Icon-60@3x.png", "Icon-72.png", "Icon-72@2x.png", "Icon-76.png", "Icon-76@2x.png", "Icon-Small.png", "Icon-Small@2x.png", "Icon-Small@3x.png", "Icon-Small-40.png", "Icon-Small-40@2x.png", "Icon-Small-50.png", "Icon-Small-50@2x.png", }, -- Note: -- The iPhone 3, 4, 5 and 6 models (including "s" variants") do not use -- landscape launch images. Only the "plus" family uses landscape launch -- images (6 plus, 6s plus). A bug in iOS required setting landscape -- options for the iPhone models. This seems resolved in iOS 9, but since -- there are still a measurable number of iOS 8 devices, continue to -- include the extra settings. -- UILaunchImages = { { -- iPhone 4 Portrait ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default", ["UILaunchImageOrientation"] = "Portrait", ["UILaunchImageSize"] = "{320, 480}" }, { -- iPhone 4 LandscapeLeft ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Landscape", ["UILaunchImageOrientation"] = "LandscapeLeft", ["UILaunchImageSize"] = "{320, 480}" }, { -- iPhone 4 LandscapeRight ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Landscape", ["UILaunchImageOrientation"] = "LandscapeRight", ["UILaunchImageSize"] = "{320, 480}" }, { -- iPhone 5 Portrait ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-568h", ["UILaunchImageOrientation"] = "Portrait", ["UILaunchImageSize"] = "{320, 568}" }, { -- iPhone 5 LandscapeLeft ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Landscape-568h", ["UILaunchImageOrientation"] = "LandscapeLeft", ["UILaunchImageSize"] = "{320, 568}" }, { -- iPhone 5 LandscapeRight ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Landscape-568h", ["UILaunchImageOrientation"] = "LandscapeRight", ["UILaunchImageSize"] = "{320, 568}" }, { -- iPad Portrait ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Portrait", ["UILaunchImageOrientation"] = "Portrait", ["UILaunchImageSize"] = "{768, 1024}" }, { -- iPad LandscapeLeft ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Landscape", ["UILaunchImageOrientation"] = "LandscapeLeft", ["UILaunchImageSize"] = "{768, 1024}" }, { -- iPad LandscapeRight ["UILaunchImageMinimumOSVersion"] = "7.0", ["UILaunchImageName"] = "Default-Landscape", ["UILaunchImageOrientation"] = "LandscapeRight", ["UILaunchImageSize"] = "{768, 1024}" }, { -- iPhone 6 Portrait ["UILaunchImageMinimumOSVersion"] = "8.0", ["UILaunchImageName"] = "Default-667h", ["UILaunchImageOrientation"] = "Portrait", ["UILaunchImageSize"] = "{375, 667}" }, { -- iPhone 6 LandscapeLeft ["UILaunchImageMinimumOSVersion"] = "8.0", ["UILaunchImageName"] = "Default-Landscape-667h", ["UILaunchImageOrientation"] = "LandscapeLeft", ["UILaunchImageSize"] = "{375, 667}" }, { -- iPhone 6 LandscapeRight ["UILaunchImageMinimumOSVersion"] = "8.0", ["UILaunchImageName"] = "Default-Landscape-667h", ["UILaunchImageOrientation"] = "LandscapeRight", ["UILaunchImageSize"] = "{375, 667}" }, { -- iPhone 6 Plus Portrait ["UILaunchImageMinimumOSVersion"] = "8.0", ["UILaunchImageName"] = "Default-736h", ["UILaunchImageOrientation"] = "Portrait", ["UILaunchImageSize"] = "{414, 736}" }, { -- iPhone 6 Plus LandscapeLeft ["UILaunchImageMinimumOSVersion"] = "8.0", ["UILaunchImageName"] = "Default-Landscape-736h", ["UILaunchImageOrientation"] = "LandscapeLeft", ["UILaunchImageSize"] = "{414, 736}" }, { -- iPhone 6 Plus LandscapeRight ["UILaunchImageMinimumOSVersion"] = "8.0", ["UILaunchImageName"] = "Default-Landscape-736h", ["UILaunchImageOrientation"] = "LandscapeRight", ["UILaunchImageSize"] = "{414, 736}" }, { -- iPad Pro Portrait ["UILaunchImageMinimumOSVersion"] = "9.0", ["UILaunchImageName"] = "Default-Portrait-1366", ["UILaunchImageOrientation"] = "Portrait", ["UILaunchImageSize"] = "{1024, 1366}" }, { -- iPad Pro Landscape Right ["UILaunchImageMinimumOSVersion"] = "9.0", ["UILaunchImageName"] = "Default-Landscape-1366", ["UILaunchImageOrientation"] = "LandscapeRight", ["UILaunchImageSize"] = "{1024, 1366}" }, { -- iPad Pro Landscape Left ["UILaunchImageMinimumOSVersion"] = "9.0", ["UILaunchImageName"] = "Default-Landscape-1366", ["UILaunchImageOrientation"] = "LandscapeLeft", ["UILaunchImageSize"] = "{1024, 1366}" }, }, }, }, -- -- Android Section -- android = { useGoogleServicesJson = true, }, }

Let me know if you need any additional code.

Hi @rdvitali98,

For a quick test, can you disable (comment out) Scott’s “plugin.firebase” plugin? It’s a possibility, however slim, that it’s causing some conflict in the notifications aspect and badges.

Thanks,

Brent

Also, can I see the line in your code (main.lua probably) where you call “notifications.registerForPushNotifications()”? Thanks!

I do not call the register for push in my main.lua because I only am allowing people to register for push after a certain point in my app, but this is the line of code I use:

notifications.registerForPushNotifications( { useFCM=true } )

I do not think that the firebase plugin is interfering with the badge icons, I think that I do not have the right code to handle it or that I did not write the code correctly so that when I click on a notification, it removed the badge number. Can you send a sample code that does this? 

Also just as an fyi I think there is a bigger issue the notifications v2, I have opened a post about it at this link so if you could look at this and also possibly respond to it that would be great. 

https://forums.coronalabs.com/topic/68761-notifications-v2-subscriptions-not-working/

So I made a temporary fix to the issue. As you will see in my code below, I have removed the if statement regarding if event.badge > 0 as well as the set property that was decreasing the value of the badge. Right now I have it just set the badge to 0 when a notification is clicked and it works. I am still confused on why i cannot decrease it by 1 however. 

local function notificationListener( event ) if ( event.type == "local" ) then -- Handle the local notification local badgeNum = native.getProperty( "applicationIconBadgeNumber" ) if ( badgeNum \> 0 ) then badgeNum = badgeNum - 1 native.setProperty( "applicationIconBadgeNumber", badgeNum ) end elseif ( event.type == "remoteRegistration" ) then -- Code to register your device with the service firebase.updateUserData({token = event.token}, uploadUserDataFunction) elseif ( event.type == "remote" ) then -- Handle the push notification -- native.setProperty( "applicationIconBadgeNumber", 0 ) --if ( event.badge and event.badge \> 0 ) then -- badgeNum = badgeNum - 1 native.setProperty( "applicationIconBadgeNumber", 0 ) if (event.custom ~= nil) then eventData = event.custom sceneVariable = eventData.SceneName end -- end end end Runtime:addEventListener( "notification", notificationListener )

Hi @rdvitali98,

I suppose let me ask the “super obvious” question first: did you set up everything for badges as noted here?

https://docs.coronalabs.com/guide/events/appNotification/index.html#ios-setup

Best regards,

Brent

If you are talking about config.lua, yes. I have updated my code here and as of right now neither when the app is in the background or it is closed when I click on a notification the badge icon does not decrease. Also, the badge icon does not go up by a value of one if there are more notifications. I may be confused but is it only possible to determine the badge number based on what the message in firebase defines it as or should the app be able to add a value of one to every new notification? This is my new code I updated it a bit. This is all in my main.lua.

local function notificationListener( event ) if ( event.type == "local" ) then -- Handle the local notification local badgeNum = native.getProperty( "applicationIconBadgeNumber" ) if ( badgeNum \> 0 ) then badgeNum = badgeNum - 1 native.setProperty( "applicationIconBadgeNumber", badgeNum ) end elseif ( event.type == "remoteRegistration" ) then -- Code to register your device with the service local deviceToken = event.token elseif ( event.type == "remote" ) then -- Handle the push notification if ( event.badge and event.badge \> 0 ) then badgeNum = badgeNum - event.badge native.setProperty( "applicationIconBadgeNumber", badgeNum ) end end end Runtime:addEventListener( "notification", notificationListener ) local launchArgs = ... if ( launchArgs and launchArgs.notification ) then notificationListener( launchArgs.notification ) notificationListener( launchArgs.badge ) end

Hi @rdvitali98,

Do you mind posting your config.lua content here as well?

Thanks,

Brent

Sure. Here it is. I am just confused about why the badge number is not going down when I click on a notification and why when I get multiple notifications it does not sum up. 

-- -- For more information on config.lua see the Corona SDK Project Configuration Guide at: -- https://docs.coronalabs.com/guide/basics/configSettings -- application = { content = { scale = "letterBox", width = display.pixelWidth, height = display.pixelHeight, fps = 60, --[[imageSuffix = { ["@2x"] = 2, ["@4x"] = 4, }, --]] }, notification = { iphone = { types = { "badge", "sound", "alert" } }, } }

Hi again,

I assume you called “notifications.registerForPushNotifications()” and included the “useFCM” parameter with a value of true?

Best regards,

Brent

Yes. I am able to receive notifications. That is not the issue. My issue is pertaining to badge number on iOS. The badge number is not decreasing when I click on the notification and the badge number is not increasing by a value of 1 when I get a new notification.