Cannot send push notification using EXPO DeviceNotRegistered

#1

I am trying to send a push notification using EXPO to my react native application but for some reason the phone is not receiving any notification.

I am trying to send notification thru https://expo.io/dashboard/notifications for testing purpose and suddently I read this: Note: Expo push notifications are supported only in standard Expo projects. They are not supported in detached apps nor with create-react-native-app.

I was wondering if push notification of EXPO does not work for detached and apps created using create-react-native-app(in my case) what would this expo would work for?

Code:

import React from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
import {StackNavigator} from 'react-navigation';
import Checklist from "./src/components/Checklist/Checklist";
import Main from "./src/components/Main/Main";
import Product from "./src/components/Product/Product";
import {Notifications,Permissions} from 'expo';

export default class AppContainer extends React.Component {
    state = {
        notification: {},
    };

    componentDidMount() {
        registerForPushNotificationsAsync();

        // Handle notifications that are received or selected while the app
        // is open. If the app was closed and then opened by tapping the
        // notification (rather than just tapping the app icon to open it),
        // this function will fire on the next tick after the app starts
        // with the notification data.
        this._notificationSubscription = Notifications.addListener(this._handleNotification);
    }

    _handleNotification = (notification) => {
        this.setState({notification: notification});
    };

    render() {
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <Text>Origin: {this.state.notification.origin}</Text>
                <Text>Data: {JSON.stringify(this.state.notification.data)}</Text>
            </View>
        );
    }
}

const PUSH_ENDPOINT = 'https://your-server.com/users/push-token';

async function registerForPushNotificationsAsync() {
    const { status: existingStatus } = await Permissions.getAsync(
        Permissions.NOTIFICATIONS
    );
    let finalStatus = existingStatus;

    // only ask if permissions have not already been determined, because
    // iOS won't necessarily prompt the user a second time.
    if (existingStatus !== 'granted') {
        // Android remote notification permissions are granted during the app
        // install, so this will only ask on iOS
        const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
        finalStatus = status;
    }

    // Stop here if the user did not grant permissions
    if (finalStatus !== 'granted') {
        return;
    }

    // Get the token that uniquely identifies this device
    let token = await Notifications.getExpoPushTokenAsync();
    console.log("-------------------------------------------------------------------------------");
    console.log(token);

    // POST the token to your backend server from where you can retrieve it to send push notifications.
    return fetch(PUSH_ENDPOINT, {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            token: {
                value: token,
            },
            user: {
                username: 'Brent',
            },
        }),
    });
}
#2

I also tryting to print out finalStatus and it is granted, so it should not be permissions issue!

#3

Hi-

Sorry, I’m a tiny bit confused. Is your project detached or just a regular Expo project?

If a regular Expo project, are you testing in the iOS simulator or on Genymotion? They can’t receive push notifications (not because of Expo, just in general they can’t). Also, if you have an iOS app open in the foreground, it won’t show push notifications for that app – the app will just get a message about the push notification.

closed #4

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.