pushy / pushy-react-native

The official Pushy SDK for React Native apps.
Apache License 2.0
16 stars 20 forks source link

Remove notification programmatically #92

Closed franciscohanna92 closed 4 months ago

franciscohanna92 commented 4 months ago

Is it possible to remove a notification programmatically after creating it with Pushy.notify?

pushy commented 4 months ago

Hi @franciscohanna92, Thanks for reaching out. We'd be glad to assist.

Currently, the Pushy RN SDK only supports clearing all of your app's notifications by invoking the method Pushy.hideNotifications().

To clear individual notifications, consider using a plugin called react-native-push-notification instead of Pushy.notify(), which allows granular control of posting and clearing Android notifications.

Please install and import the plugin in your App.js file, and replace the invocation of Pushy.notify() with the relevant invocation of the plugin:

PushNotification.localNotification({
    id: '123',  <---------- Set a unique notification ID
    title: "My Notification Title",
    autoCancel: true, 
    message: "My Notification Message",
    bigText: "My long message goes here", 
    vibrate: true, // (optional) default: true
    vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
    playSound: false // (optional) default: true
});

You may later cancel a notification by invoking the following method and passing in the notification id param value:

PushNotification.cancelLocalNotification('123');

Please let us know if you have any questions or face any difficulties.

franciscohanna92 commented 4 months ago

@pushy Just for the record, I ended up using expo-notifications since I'm already using Expo to build the app.

This is how I accomplished it:

import * as Notifications from 'expo-notifications';

// First, set the handler that will cause the notification to show the alert
Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
});

// Second, call this method to create the notification instead of Pushy.notify
const pushId = await Notifications.scheduleNotificationAsync({
  content: {
    title: 'Look at that notification',
    body: "I'm so proud of myself!",
  },
  trigger: null,
});

// Last, dismiss the notification
Notifications.dismissNotificationAsync(pushId);

More information on the official docs for Expo Notifications

pushy commented 4 months ago

Hi @franciscohanna92, Thanks for letting us know! That is indeed the preferred solution for Expo apps.

Please let us know if there's anything else we can help with.

franciscohanna92 commented 3 months ago

@pushy Nothing else for the time beign.Thanks!