Expo Notifications Do Not Work On Android Standalone App

  1. SDK Version: 40
  2. Platforms: Android

I am using latest implementation of expo-notifications from this Expo Docs example. It works fine in development and production on iOS, as well as in Android development mode (I generate token and receive notifications all good). However, I can’t get a token when the Android app is in production (standalone app). By looking at the log, nothing returns from

const token = await Notifications.getExpoPushTokenAsync();

which is weird. What am I doing wrong?

app.json:

{
	"expo": {
		"name": "myApp",
		"slug": "my-app",
		"version": "1.0.5",
		"orientation": "portrait",
		"icon": "./app/assets/icon.png",
		"splash": {
			"image": "./app/assets/splash.png",
			"resizeMode": "cover",
			"backgroundColor": "#ffffff"
		},
		"updates": {
			"enabled": true,
			"fallbackToCacheTimeout": 0
		},
		"assetBundlePatterns": ["**/*"],
		"ios": {
			"supportsTablet": false,
			"bundleIdentifier": "PACKAGENAME",
			"buildNumber": "1.0.5"
		},
		"android": {
			"adaptiveIcon": {
				"foregroundImage": "./app/assets/icon.png",
				"backgroundColor": "#FFFFFF"
			},
			"package": "PACKAGENAME",
			"versionCode": 9,
			"permissions": [],
			"useNextNotificationsApi": true
		},
		"web": {
			"favicon": "./app/assets/favicon.png"
		},
		"notification": {
			"iosDisplayInForeground": true,
			"icon": "./app/assets/sycretIcon.png"
		}
	}
}

registerToken (from docs):

import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import React, { useState, useEffect, useRef } from 'react';
import { Text, View, Button, Platform } from 'react-native';

...

async function registerForPushNotificationsAsync() {
	let token;
	if (Constants.isDevice) {
		const {
			status: existingStatus,
		} = await Notifications.getPermissionsAsync();
		let finalStatus = existingStatus;
		if (existingStatus !== "granted") {
			const { status } = await Notifications.requestPermissionsAsync();
			finalStatus = status;
		}
		if (finalStatus !== "granted") {
			alert("Failed to get push token for push notification!");
			return;
		}

        //this doesn't return anything in Android standalone app
		token = (await Notifications.getExpoPushTokenAsync()).data;
		console.log(token);
	} else {
		alert("Must use physical device for Push Notifications");
	}

	if (Platform.OS === "android") {
		Notifications.setNotificationChannelAsync("default", {
			name: "default",
			importance: Notifications.AndroidImportance.MAX,
			vibrationPattern: [0, 250, 250, 250],
			lightColor: "#FF231F7C",
		});
	}

	return token;
}

I also do not get prompt window from the app to grant permissions if the notifications permission is off. Shouldn’t the prompt be called when permission is not granted?

forgot google-json file. Works now.

1 Like

Thanks for sharing what was wrong, @denistepp! Is there anything you think we could’ve done to help avoid you missing/forgetting about the google services json file?

Cheers,
Adam

Not really, it’s just I wasn’t paying close attention to the docs. Completely my fault.

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