@@ -17,6 +17,8 @@ export const registerForPushNotificationsAsync = async () => {
1717 await Notifications . setNotificationChannelAsync ( "default" , {
1818 name : "default" ,
1919 importance : Notifications . AndroidImportance . MAX ,
20+ vibrationPattern : [ 0 , 250 , 250 , 250 ] , // Optional: Add vibration
21+ sound : "default" , // Enable sound
2022 } ) ;
2123 }
2224
@@ -65,21 +67,50 @@ export const handleNotification = async (
6567 } ) ;
6668} ;
6769
70+ // Handle foreground notifications
6871export const handleForegroundNotification = ( ) => {
69- return messaging ( ) . onMessage ( async ( remoteMessage ) => {
70- await handleNotification ( remoteMessage ) ;
72+ const unsubscribe = messaging ( ) . onMessage ( async ( remoteMessage ) => {
73+ console . log ( "Foreground message received:" , remoteMessage ) ;
74+
75+ // (Optional) Show an in-app alert:
76+ // Alert.alert(
77+ // "New notification",
78+ // JSON.stringify(remoteMessage.notification)
79+ // );
80+
81+ // Show a *local* system notification so the user sees a banner or heads-up:
82+ await Notifications . scheduleNotificationAsync ( {
83+ content : {
84+ title : remoteMessage . notification ?. title ?? "New Notification" ,
85+ body : remoteMessage . notification ?. body ?? "You received a new message" ,
86+ // You can include other fields like sound, badge, data, etc.
87+ } ,
88+ trigger : null , // null = show now (immediately)
89+ } ) ;
7190 } ) ;
91+ return unsubscribe ;
7292} ;
7393
94+ // DEPRACATED
95+ // export const handleForegroundNotification = () => {
96+ // return messaging().onMessage(async (remoteMessage) => {
97+ // await handleNotification(remoteMessage);
98+ // });
99+ // };
100+
101+ // Handle background & quit-state notifications
74102export const handleBackgroundNotifications = ( ) => {
103+ // Handle background notifications handler.
75104 messaging ( ) . setBackgroundMessageHandler ( async ( remoteMessage ) => {
76105 console . log ( "Background message received:" , remoteMessage ) ;
77106 } ) ;
78107
108+ // Handle notification opened when app is in background
79109 messaging ( ) . onNotificationOpenedApp ( ( remoteMessage ) => {
80110 console . log ( "Notification opened from background:" , remoteMessage ) ;
81111 } ) ;
82112
113+ // Handle notification opened when app is in quit state
83114 messaging ( )
84115 . getInitialNotification ( )
85116 . then ( ( remoteMessage ) => {
@@ -396,12 +427,3 @@ export async function triggerLocalSampleNotification() {
396427// };
397428// }, []);
398429// }
399-
400- // // Set a global notification handler
401- // Notifications.setNotificationHandler({
402- // handleNotification: async () => ({
403- // shouldShowAlert: true,
404- // shouldPlaySound: true,
405- // shouldSetBadge: false,
406- // }),
407- // });
0 commit comments