pushy / pushy-react-native

The official Pushy SDK for React Native apps.
Apache License 2.0
16 stars 20 forks source link

[Android] TypeError: Cannot read property "then" of undefined #54

Closed thebluewall2 closed 3 years ago

thebluewall2 commented 3 years ago

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.

Screenshot_1601375788

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);
        });
pushy commented 3 years ago

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);
});
thebluewall2 commented 3 years ago

Hi, sorry, but we're already calling this from there.

IMG_5864

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}>
...
pushy commented 3 years ago

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?

thebluewall2 commented 3 years ago

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!