invertase / react-native-notifee

Moved to https://github.com/invertase/notifee
https://invertase.io/blog/open-sourcing-notifee
Other
466 stars 31 forks source link

Notification disappears after a seconds asForegroundService: true #338

Closed gminator closed 3 years ago

gminator commented 3 years ago

Summary

I'm seeing some strange behavior when using displayNotification with asForegroundService: true...

The notification appears for a brief moment and then disappears again. I can't get it to stay unless I deliberately break the javascript inside notifee.registerForegroundService. I write some broken code, it will but every subsequent call to displayNotification works as expected.

It also behaves as expected asForegroundService is not set

Reproducible sample code

This solution below doesn't, as mentioned the notification shows & vibrates for a second and then disabpears

notifee.registerForegroundService(async (notification) => {
  notifee.onBackgroundEvent(async ({type, detail}) => {
    //do something
  }) 
  notifee.onForegroundEvent(async ({type, detail}) => {
    //do something
  }); 
});

notifee.displayNotification({
        title: "Some Title",
        body: "In Progress",
        android: {
          channelId,
          progress: {
            indeterminate: true,
          }
        }
    })

This predictably fails 1st time, but every subsequent call after removing do.something.to.break.javascript works as expected

notifee.registerForegroundService(async (notification) => {
  do.something.to.break.javascript //I do to delibrately break the javascript
  notifee.onBackgroundEvent(async ({type, detail}) => {
    //do something
  }) 
  notifee.onForegroundEvent(async ({type, detail}) => {
    //do something
  }); 
});

notifee.displayNotification({
        title: "Some Title",
        body: "In Progress",
        android: {
          channelId,
          progress: {
            indeterminate: true,
          }
        }
    })

Environment info

npx react-native info output:

info Fetching system and libraries information...
System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 2.68 GB / 15.92 GB
  Binaries:
    Node: 12.15.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
  IDEs:
    Android Studio: Version  3.6.0.0 AI-192.7142.36.36.6241897
  npmPackages:
    react: ~16.9.0 => 16.9.0
    react-native: ~0.61.4 => 0.61.5
helenaford commented 3 years ago

@gminator hi, sorry for the delay, I've taken a look and you are right - there was a typo to the docs in the example code which counteracted the text.

If you change the following code:

notifee.registerForegroundService(async (notification) => {
//...
})

to

```js
notifee.registerForegroundService((notification) => {
  return new Promise(() => {
    // Long running task...
  });
});

it should persist. Hope that helps.