5

I have push notifications working in React Native using @react-native-firebase/messaging. I am using FCM on the backend, and it is currently showing the OS lock screen notifications on iOS and Android.

I want to clear a given notification after a certain time, or after an amount of time has passed. Is there a way to do this? Right now when I send a notification it will stick around for days if I don't click on it. I would like to take a notification down after say an hour, or at 4pm, or whatever. Front-end and/or back-end solutions welcome.

I had assumed that the "ttl" (time to live) parameter did this, but this is not the case.

hamboy
  • 727
  • 2
  • 8
  • 21

3 Answers3

3

you could use a background handler like react-native-background-fetch

In your onMessage or backgroundMessage schedule a backgroundTask for your desired time with scheduleTask().

You could use react-native-push-notification to display the notification, which has an method cancelLocalNotifications() to cancel notifications In the task you could clear the notification depending on the id

PushNotification.configure({
  onNotification: (notification) => {
    var {id} = remoteMessage.data
    
    BackgroundFetch.configure({
      minimumFetchInterval: 15
    }, async (taskId) => {
    
      PushNotification().cancelLocalNotifications(id)
      BackgroundFetch.finish(taskId);
    })
    
    BackgroundFetch.scheduleTask({
      taskId: id,
      forceAlarmManager: true,
      delay: 5000 
    });

  }
})
Stevetro
  • 1,933
  • 3
  • 16
  • 29
  • thanks, but @react-native-firebase/messaging does not have .cancelDisplayedNotification() as far as I can tell. Does your answer depend on switching out the message receiver like Marcel's answer? – hamboy Dec 09 '20 at 16:46
  • yes, i didn't see that the solution is not possible in messaging. An other solution is with react-native-push-notification. I updated my answer – Stevetro Dec 09 '20 at 20:41
1

TTL parameter only specifies the delivery of the notification to the user device. E.g. Still deliver the message after the phone was offline for 2 hours or not.

I'm not sure if there is a way with the default firebase package, but the more advanced version of it seems to be able to handle that use case:

https://notifee.app/react-native/reference/canceldisplayednotification

I think you should be able to call that method in a background task (e.g. after receiving another (silent) notification).

Unfortunately I couldn’t test it myself yet.

Marcel
  • 174
  • 1
  • 4
0

On iOS you you can use the badge setting. If you set it to 0, it will remove all notifications. For your case you could schedule a "cleanup" task that triggers the below request after a certain amount of time.

{
  message: {
    notification: {
      body: "" // has to be empty
    },
    android: {
      notification: {
        channel_id: 'some_channel'
      }
    },
    apns: {
      payload: {
        aps: {
          category: 'some_category',
          badge: 0
        }
      }
    },
    token: device_token
  }
}

Unfortunately, I have not found a similar solution for android yet.

ccfz
  • 181
  • 1
  • 4