DEVELOPER NOTES:
******************************* Notification *****************************
My physical smartphone: Samsung Galaxy 6 (Android version: 7.0)
My Android Virtual Device: Pixel 2 (Note: Advanced Config: Boot option: Cold boot)
Providers -
- Expo’s Push Notification Service <-----<<< Easiest without a lot of prep
- One Signal
- Firebase Cloud Messaging
- Amazon’s Simple Notification Service (SNS)
INSTALL Expo Notifications -
$ expo install expo-notifications
added 8 packages from 6 contributors and audited 1098 packages in 4.433s
UPDATE -
app.json:
“android”: {
“useNextNotificationsApi”: true,
:
-Push Notification Tool — Expo
Tool to send test push notifications
******************* Mosh’s Notifications NOT working ***************
He is using:
import Notifications from ‘expo’;
It should be:
import * as Notifications from ‘expo-notifications’;
I used code from the following URLs to make Notification work SUCCESSFULLY.
-Notifications - Expo Documentation
-Push Notifications Overview - Expo Documentation … follow guide from here
-Sending Notifications with Expo's Push API - Expo Documentation
:
-Receiving Notifications - Expo Documentation
:
Tool to send a test Notification:
-Push Notification Tool — Expo
CODE: useNotifications.js
import { useEffect, useRef, useState } from "react";
import * as Notifications from 'expo-notifications';
import expoPushTokensApi from "../api/expoPushTokens";
// Required
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
export default useNotifications = () => {
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
registerForPushNotifications();
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response.notification.request.content.body);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
const registerForPushNotifications = async () => {
try {
const permissions = await Notifications.getPermissionsAsync();
if (! permissions.granted) {
const finalPermissions = await Notifications.requestPermissionsAsync();
if (! finalPermissions.granted) {
console.log("permissions NOT granted!");
return;
}
}
console.log("permissions granted!");
const token = await Notifications.getExpoPushTokenAsync();
expoPushTokensApi.register(token.data);
} catch (error) {
console.log("Error getting a push token", error);
}
};
}