[Android] TypeError: Cannot read property "then" of undefined
Closed this issue · 4 comments
Hello,
I can receive notifications on my Android after following the setup instructions on pushy.me, but once the notification is received, the app will crash with "TypeError: Cannot read property "then" of undefined.
My code right now are as follows:
Pushy.listen();
Pushy.setNotificationListener(data => {
console.log('pushy receive', data);
const { Title = '', Message = '', Action, ID } = data;
Pushy.notify(Title, Message, { Action, ID });
});
Pushy.setNotificationClickListener(data => {
console.log('pushy clikc', data);
});
Hi @thebluewall2,
We'd be glad to assist.
You are currently invoking this method inside a Component
. Please move the Pushy.setNotificationListener((data) => {})
invocation outside the Component
class (right after imports):
// Please place this code in App.js,
// After the import statements, and before the Component class
Pushy.setNotificationListener(async (data) => {
// Print notification payload data
console.log('Received notification: ' + JSON.stringify(data));
// Notification title
let notificationTitle = 'MyApp';
// Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
let notificationText = data.message || 'Test notification';
// Display basic system notification
Pushy.notify(notificationTitle, notificationText, data);
// Clear iOS badge count
Pushy.setBadge && Pushy.setBadge(0);
});
Hi, sorry, but we're already calling this from there.
App.js
...
import Config from './App/Config/Config';
import Navigator from './App/Navigation/Navigator';
import Store from './App/Store/store';
import { setNavigator } from './App/Services/Navigation/Navigation';
const AppContainer = createAppContainer(Navigator);
Pushy.setNotificationListener(data => {
console.log('pushy receive', data);
const { Title = '', Message = '', Action, ID } = data;
Pushy.notify(Title, Message, { Action, ID });
});
Pushy.setNotificationClickListener(data => {
console.log('pushy clikc', data);
});
class App extends React.PureComponent {
_renderApp = () => {
return (
<SafeAreaProvider>
<Provider store={Store}>
...
Hi @thebluewall2,
Please temporarily try running with the unmodified sample callback implementation:
Pushy.setNotificationListener(async (data) => {
// Print notification payload data
console.log('Received notification: ' + JSON.stringify(data));
// Notification title
let notificationTitle = 'MyApp';
// Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
let notificationText = data.message || 'Test notification';
// Display basic system notification
Pushy.notify(notificationTitle, notificationText, data);
// Clear iOS badge count
Pushy.setBadge && Pushy.setBadge(0);
});
Does the error manifest itself then?
Ah, great, it's working now. It might have something to do with the async
, because I removed it. Once I put it back, the error went away. Thanks!