[SOLVED] - Adding Title setup to notification plugin

I’ve noted that the notifications have a title and a description, but Solar2D just include the alert (the description) setup. Solar2D assume that the name of the app as the title of the notification. Please, see the example below, the second notification is from my game and you will see that the title match the name of the app.

I was able to successfully use both the title and alert on my push notfications for both android and ios using Firebase and their push API.

The test code below should send the push via Firebase to one specific device.

local json = require("json")

local FCMkey = "key=YOUR_FIREBASE_KEY"
local deviceToken = "YOUR_DEVICE_TOKEN"

local title = "👀 👀 Keep your eyes peeled 👀 👀"
local alert = "There may be obstacles 💥 in the way!"			 

local pushUrl = "https://fcm.googleapis.com/fcm/send"
local headers = {}
local body = {}

headers = 
{
	["Content-Type"] = "application/json",
	["Authorization"] = FCMkey
}
	body = 
	{	
		["to"] = deviceToken,
		["notification"] = 
		{
			["title"] = title,
			["body"] =  alert
		},
	}	
-- end

local function pushResponse(event)
	print("pushResponse")
end

local params = {}
params.headers = headers
params.body = json.prettify(body)
network.request( pushUrl, "POST", pushResponse, params )

Then in your notification listener on that device, setup the native.showAlert for each OS. The title is buried in the payload which is different between android and ios.

local function notificationListener( event )
	if event.type == "remoteRegistration" then
		-- do registration stuff
	elseif event.type == "remote" then
		if system.getInfo( "platform" ) == "android" then
			native.showAlert( event.androidPayload.title, event.androidPayload.body, {"Ok"} )   -- Android
		else
			native.showAlert( event.alert.title, event.alert.body, {"Ok"} )      -- IOS
		end
	end
end
Runtime:addEventListener( "notification", notificationListener )
notifications.registerForPushNotifications( { useFCM=true })

Hope this works for you.
Scott

Thank you @gsglawson , I was referring to local notifications but this is helpful too :ok_hand:

I’d suggest opening an issue on GitHub for that. That should be an easy enhancement and a welcome one. - Issues · coronalabs/corona · GitHub

I’m agree with you … It’s a great idea.

You can already add a title, it’s undocumented and has been around for a bit on Android and I am going to add to iOS here in a minute but you can do the following for local notification

local options = {
            alert = {title="Cool Title Here", body="Cool Body Here"},--alert="Title Here"
            sound = "notification.wav",
            badge = 2,
            custom = { foo = "bar" }
        }
         
        localNotification = notifications.scheduleNotification( 10, options )
2 Likes

Thanks a lot @Scott_Harrison

Thanks! Useful info :slight_smile: