Search NITYAM TECH Blog

Tuesday, March 19, 2024

Notification alert for a React Native and Expo project





steps to create a notification alert for a React Native and Expo project:


1. Install the necessary libraries: You need to install the `expo-notifications` library. You can do this by running the following command in your terminal:


npx expo install expo-notifications


If you're installing this in a bare React Native app, you should also follow the additional installation instructions².


2. Set up the notification handler: You can set up the notification handler as follows:

javascript

Notifications.setNotificationHandler({

  handleNotification: async () => ({

    shouldShowAlert: true,

    shouldPlaySound: false,

    shouldSetBadge: false,

  }),

});



3. Register for push notifications: You can register for push notifications in your component’s `useEffect` hook. Here is an example of how you can do this:

javascript

useEffect(() => {

  registerForPushNotificationsAsync().then(token => setExpoPushToken(token));

  notificationListener.current = Notifications.addNotificationReceivedListener(notification => {

    setNotification(notification);

  });

  responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {

    console.log(response);

  });

  return () => {

    Notifications.removeNotificationSubscription(notificationListener.current);

    Notifications.removeNotificationSubscription(responseListener.current);

  };

}, []);



4. Schedule a notification: You can schedule a notification as follows:

javascript

async function schedulePushNotification() {

  await Notifications.scheduleNotificationAsync({

    content: {

      title: "You've got mail! 📧",

      body: 'Here is the notification body',

      data: { data: 'goes here' },

    },

    trigger: { seconds: 2 },

  });

}

```


5. **Handle incoming notifications**: You can handle incoming notifications using the `addNotificationReceivedListener` and `addNotificationResponseReceivedListener` methods².


Please note that push notifications don't work on emulators/simulators². Make sure to test it on a physical device.

Sunday, March 17, 2024

Developer useful extensions, shortcut keywords, and tricks for VS Code



Here are some top developer useful extensions, shortcut keywords, and tricks for Visual Studio Code (VS Code):


 Extensions:


1. ESLint: For JavaScript and TypeScript linting.


2. Prettier: Code formatter for consistent code style.


3. GitLens: Enhances Git integration with features like blame annotations and code lens.


4. Debugger for Chrome: Debug JavaScript code in Chrome directly from VS Code.


5. Live Server: Launches a live server for web development with auto-reload.




 Shortcut Keywords:


1. Ctrl + Shift + P: Open the Command Palette for quick access to commands.


2. Ctrl + B: Toggle the sidebar.


3. Ctrl + `: Open the integrated terminal.


4. Ctrl + Shift + E: Open the Explorer view.


5. Ctrl + Shift + F: Search across files in the workspace.


6. Ctrl + Shift + L: Select all occurrences of the current word.




 Tricks:


1. Multi-Cursor Editing: Use Alt + Click to add multiple cursors for simultaneous editing.


2. Code Snippets: Utilize built-in or custom code snippets for faster coding.


3. Zen Mode: Enter Zen mode (Ctrl + K Z) for distraction-free writing.


4. Custom Keybindings: Create custom keybindings in the settings for personalized shortcuts.


5. IntelliSense and Auto-Completion: VS Code provides IntelliSense for code suggestions and auto-completion.


6. Integrated Terminal: Run commands and scripts directly from the integrated terminal.


7. Extensions Marketplace: Explore and install extensions from the VS Code marketplace to enhance functionality.


These are just a few highlights of the many features and capabilities of Visual Studio Code. Exploring and customizing VS Code to suit your workflow can greatly improve productivity and efficiency in development tasks.

Featured Post

npx expo install @react-native-async-storage/async-storage

Understanding and Using AsyncStorage in React Native with Expo Introduction In the world of React Native, data persistence is a fundamental ...