conditional plugins

Hello,
I am using notification plugins and when I build I comment appropriate lines to target the specified platform:

in main.lua :
– android notifications
–gNotifications = require (“plugin.notifications.v2”)

– ios plugon notifications
gNotifications = require (“plugin.notifications.v2.firebase”)

in build.settings:
–[“plugin.notifications.v2”] =
–{
– publisherId = “com.coronalabs”
–},

    ["plugin.notifications.v2.firebase"] =
    {
        publisherId = "com.coronalabs"
    },

Is there a better way to do this ?

There is a better way to do this but I just want to let you know there is no difference between the the Android notifications.v2 and notifications.v2.firebase plugin(they are only different on iOS). They are identical so in this specific case there is no need for setting Platforms.

But if you need to set different platforms for each plugin you can check this out

Side note you would still require “plugin.notifications.v2” in your .lua code if you are using notifications.v2 or notifications.v2.firebase plugin in your build.settings

The tidiest way I can think to do this is to use “supportedPlatforms” in build.settings to only include the relevant plugin:

["plugin.notifications.v2"] =
{
    publisherId = "com.coronalabs",
    supportedPlatforms = { android = true}
},
["plugin.notifications.v2.firebase"] =
{
    publisherId = "com.coronalabs",
    supportedPlatforms = { iphone = true}
},

And in main.lua just set the plugin to be loaded based on the current platform:

local notificationPluginName = system.getInfo("platform") == "android" and "plugin.notifications.v2" or "plugin.notifications.v2.firebase"
gNotifications = require(notificationPluginName)

Then there is no need to remember to comment things out between builds.

1 Like