react-native-push-notification
react-native-push-notification copied to clipboard
When I am not hard coded the `PushNotification.subscribeToTopic` string value I won't be getting the notification Why is that?
Hey
I got a pushController File like this.
import React, {useEffect, useState} from 'react';
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import {useDispatch, useSelector} from 'react-redux';
import {getUserDetails} from '../ducks/modules/AuthRedux';
import * as RootNavigation from '../../RootNavigation';
// import {useNavigation} from '@react-navigation/native';
const RemotePushController = () => {
let userDetails = useSelector(state => state.authReducer.userDetails);
const [dataFromNoti, setData] = useState({});
const dispatch = useDispatch();
useEffect(() => {
async function getStorageData() {
try {
await dispatch(getUserDetails());
} catch (err) {
console.log(err);
} finally {
}
}
getStorageData();
}, []);
useEffect(() => {
// PushNotification.subscribeToTopic('all');
PushNotification.subscribeToTopic(`${userDetails?.customerNumber}`);
}, [userDetails]);
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token) {
console.log('TOKEN:', token);
},
// (required) Called when a remote or local notification is opened or received
onNotification: function (notification) {
setData(notification);
// console.log('Notificarion => ', notification);
if (notification.action === 'OKAY') {
console.log('dataFromNoti => ', dataFromNoti.data.orderId);
console.log('dataFromNoti => ', dataFromNoti.data.secretKey);
RootNavigation.navigate('OrderDetails', {
orderId: dataFromNoti.data.orderId,
orderKey: dataFromNoti.data.secretKey,
});
PushNotification.removeAllDeliveredNotifications();
}
PushNotification.createChannel(
{
channelId: 'abcdef', // (required)
channelName: 'My channel', // (required)
channelDescription: 'A channel to categorise your notifications', // (optional) default: undefined.
playSound: true, // (optional) default: true
},
created => console.log(`createChannel returned '${created}'`), // (optional) callback returns whether the channel was created, false means it already existed.
);
PushNotification.localNotification({
channelId: 'abcdef',
autoCancel: true,
subText: notification?.message,
title: notification?.title,
message: notification?.message,
vibrate: true,
vibration: 300,
playSound: true,
soundName: 'default',
actions: ['OKAY'],
});
},
onAction: function (notification) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
alert('hello');
// process the action
},
// Android only: GCM or FCM Sender ID
senderID: '123412341234',
popInitialNotification: true,
requestPermissions: true,
});
useEffect(() => {
const type = 'notification';
PushNotificationIOS.addEventListener(type, onRemoteNotification);
return () => {
PushNotificationIOS.removeEventListener(type);
};
});
const onRemoteNotification = notification => {
const actionIdentifier = notification.getActionIdentifier();
if (actionIdentifier === 'Yes') {
// Perform action based on open action
}
if (actionIdentifier === 'text') {
// Text that of user input.
const userText = notification.getUserText();
// Perform action based on textinput action
}
};
return null;
};
export default RemotePushController;
What I am doing is I am running this push controller in App.js like this
function App() {
return (
<>
<Provider store={store}>
<NavigationContainer ref={navigationRef}>
<RemotePushController />
<Stack.Navigator initialRouteName={'SplashScreen'}>
<Stack.Screen
options={{
headerShown: false,
}}
name="SplashScreen"
component={SplashScreen}
/>
<Stack.Screen
options={{
headerShown: false,
}}
name="Home"
component={Tabs}
/>
</NavigationContainer >
</Provider>
</>
The thing is that when I am not hard coded the PushNotification.subscribeToTopic string value I won't be getting the notification Why is that?