I’ve implemented remote notifications using react-native-pusher-push-notifications
and I’m able to receive the payload when the app is opened already but not when it is in quit state.
When I get a notification and the app is in quit state, clicking on the notification opens the app but I cannot see the payload.
I’ve followed the installation steps as described in the ReadMe: https://github.com/b8ne/react-native-pusher-push-notifications (i.e For android they said to skip autolink and all other installation-related settings for iOS and Android)
Then I have a pusher.ts
file in which I’m initializing it as:
export const initPusher = async (): Promise<void> => {
RNPusherPushNotifications.setInstanceId(Config.PUSHER_INSTANCE_ID)
RNPusherPushNotifications.on("registered", () => {
const callSetUserId = (): void => {
const userId = useAuthStore.getState().user.id
const pusherToken = useAuthStore.getState().pusherToken
RNPusherPushNotifications.setUserId(
userId.toString(),
pusherToken,
(error: any) => {
console.log("pusher setUserId error", JSON.stringify(error))
RNPusherPushNotifications.clearAllState()
if (retryCount < 3) {
retryCount++
callSetUserId()
}
},
() => {
console.log(
"pusher setUserId success:",
JSON.stringify({ userId, pusherToken }),
)
},
)
}
callSetUserId()
})
}
THen in my Main Navigator file which is loaded from App.tsx
I have:
useEffect(() => {
initPusher()
useNotificationHandler()
}, [])
The useNotificationHandler
is where I have a use effect, which gets triggered only when the app is opened and not when it’s in background or killed state.
const handleNotification = (notification: any) => {
console.log("notification: ", notification)
// iOS app specific handling
if (Platform.OS === "ios") {
switch (notification.appState) {
case "inactive":
// inactive: App came in foreground by clicking on notification.
// Use notification.userInfo for redirecting to specific view controller
case "background":
// background: App is in background and notification is received.
// You can fetch required data here don't do anything with UI
case "active":
// App is foreground and notification is received. Show a alert or something.
default:
break
}
} else {
console.log("android handled notification...")
}
}
useEffect(() => {
RNPusherPushNotifications.on("notification", handleNotification)
}, [])
It’s not working and there is no error, so I’m stuck at this point.