apns2 icon indicating copy to clipboard operation
apns2 copied to clipboard

Could be a problem with topic ? / defaultTopic ?

Open smhk opened this issue 1 year ago • 11 comments

"TypeError: Cannot read properties of undefined (reading 'topic')"

apnsClient, send error undefined
undefined
TypeError: Cannot read properties of undefined (reading 'topic')
    at ApnsClient._send (/home/ubuntu/main/node_modules/apns2/dist/apns.js:51:52)
    at ApnsClient.send (/home/ubuntu/main/node_modules/apns2/dist/apns.js:35:21)
    at sendNotifs (/home/ubuntu/main/main.js:355:20)
    at /home/ubuntu/main/main.js:315:4
    at Layer.handle [as handle_request] (/home/ubuntu/main/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/ubuntu/main/node_modules/express/lib/router/route.js:149:13)
    at /home/ubuntu/main/node_modules/body-parser/lib/read.js:137:5
    at AsyncResource.runInAsyncScope (node:async_hooks:206:9)
    at invokeCallback (/home/ubuntu/main/node_modules/raw-body/index.js:238:16)
    at done (/home/ubuntu/main/node_modules/raw-body/index.js:227:7)

same production or sandbox

const apnsClient = new ApnsClient({
	team: `BGP36TQ2W7`,
	keyId: `PL62KVXW34`,
	signingKey: p8key,
	defaultTopic: `softwaremoderna.com.phonedemostaging`
})

I tried both defaultTopic and topic at the end there ?

Tears!

My code to send just ..

async function sendNotifs() {

	var nts = []

	for (const aToken of global.known_apns_tokens) {
		const aTokenData = Buffer.from(aToken, 'base64')
		nts.push(
			new Notification(aTokenData, { alert: 'apns yoyo' })
		)
	}

	// go both apnsClient and apnsClientSandbox ...

	try {
		await apnsClient.send(nts)
		mpAdmin.devTell(`apnsClient, seemed to send ${nts.length}`)
	} catch (err) {
		mpAdmin.devTell(`apnsClient, send error ${err.reason}`)
		console.log(err.reason)
		console.log(err)
	}

	try {
		await apnsClientSandbox.send(nts)
		mpAdmin.devTell(`apnsClientSandbox, seemed to send ${nts.length}`)
	} catch (err) {
		mpAdmin.devTell(`apnsClientSandbox, send error ${err.reason}`)
		console.log(err.reason)
		console.log(err)
	}
}


smhk avatar Jul 10 '24 18:07 smhk

Interesting, looking into this. Are you using TypeScript or JavaScript? It seems like notification constructor is not working correctly, it should always have a default options property which is where we read topic from. The fact that is undefined is very suspect.

Is there more code that we're not seeing here? It seems like something is removing the options property from the notification. The code here should work totally fine.

AndrewBarba avatar Jul 10 '24 18:07 AndrewBarba

@AndrewBarba thank you so much, am using JavaScript. that is literally every line of code

I tried setting the topic "manually", things like

new Notification(aTokenData, { alert: 'apns yoyo', topic .. etc })

but I couldn't get it

more info ..

smhk avatar Jul 10 '24 19:07 smhk

I have used (only) apns2 in the past and it always worked. (Notice my long post here! https://stackoverflow.com/a/60550859/294884 ) I haven't done push for a yr or two and I can't access the previous client projects, so dunno.

I bring up exactly like this ..

Screenshot 2024-07-10 at 2 47 22 PM

(I deliberately made wrong the various other data items, to ensure that, was not the issue; ie I would incorrectly get "wrong key" etc from Apple ... all working great)

Here the current code for it sending, live on server now, (I also tried the pink alternates extensively),

Screenshot 2024-07-10 at 2 50 25 PM

result right now when I try it ..

Screenshot 2024-07-10 at 2 53 51 PM

hopefully just some dumb mistake on my part @AndrewBarba !

thanks !!!!!

smhk avatar Jul 10 '24 19:07 smhk

And you're using the latest version right? v11.7.0?

AndrewBarba avatar Jul 10 '24 20:07 AndrewBarba

Yes, latest version checked!

I looked through the source https://github.com/AndrewBarba/apns2/blob/main/src/apns.ts but couldn't find any clues. Could it relate to the default for topic or defaultTopic?

Am more than happy to console.log anything that could help

I noticed there's an email on your git account so I sent along the actual runnable file, you never know

Screenshot 2024-07-10 at 7 34 14 PM

smhk avatar Jul 11 '24 11:07 smhk

Could a problem be caused by

const { ApnsClient } = require('apns2')

rather than

import { ApnsClient } from 'apns2'

The import form doesn't work in my project.

smhk avatar Jul 11 '24 12:07 smhk

Yeah this is what I'm focused on right now since I've only used in TypeScript and ESM projects. I'll report back with what I find

AndrewBarba avatar Jul 11 '24 12:07 AndrewBarba

Yeah something is fishy passing in a buffer, can you try new Notification(aTokenData.toString('hex'), { alert: 'apns yoyo2' })

AndrewBarba avatar Jul 11 '24 13:07 AndrewBarba

I doubt the array version is actually sending the token. The array variant will resolve to an array of results, and if you log those results I bet you see that there was an error. Most likely need to format the token correctly. You are creating a buffer from a base64 string, so I think you then need to do .toString('hex') but im not 100% sure id need to see one of the tokens.

AndrewBarba avatar Jul 11 '24 14:07 AndrewBarba

No luck with this one guys ?!

smhk avatar Jul 20 '24 00:07 smhk

Did you trying calling toString(hex) on the buffer? You cannot pass in a buffer to the new Notification()

AndrewBarba avatar Jul 20 '24 00:07 AndrewBarba