evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

show_in_forground doesn't work #181

Open woniesong92 opened 7 years ago

woniesong92 commented 7 years ago

I'm using RN 35.0 and Android 6.0. Below is my code.

  componentDidMount() {
    this.notificationUnsubscribe = FCM.on('notification', (notif) => {
      // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
      if(notif.local_notification){
        //this is a local notification
      }
      if(notif.opened_from_tray){
        //app is open/resumed because user clicked banner
      }

      console.log(notif)

      // FCM.getBadgeNumber().then(number =>
      //   FCM.setBadgeNumber(number + 1)
      // )

      if (!notif.local_notification) {
        FCM.presentLocalNotification({
          title: "My Notification Title",                     // as FCM payload
          body: notif.body,                                   // as FCM payload (required)
          sound: "default",                                   // as FCM payload
          priority: "high",                                   // as FCM payload
          click_action: "ACTION",                             // as FCM payload
          large_icon: "ic_launcher",                           // Android only
          icon: "ic_notification",                            // as FCM payload
          color: "red",                                       // Android only
          vibrate: 300,                                       // Android only default: 300, no vibration if you pass null
          lights: true,                                       // Android only, LED blinking (default false)
          show_in_foreground: true,                           // notification when app is in foreground (local & remote)
        })
      }
    })

1. App is killed, Device is locked Notification appears in a banner

2. App is killed, Device is unlocked Notification appears in the system tray

3. App is in background Notification appears in the system tray

4. App is in foreground Notification comes in but calling FCM.presentLocalNotification({}) doesn't show any UI (I expected to see a banner or modal coming from bottom or top)

Q1. How come notification only appears in system tray silently?

ss11 Only in system tray

ss22 Banner comes down

Q2. How do I show a banner while the user is using the app (foreground)? I thought calling FCM.presentLocalNotification({}) will show a UI (either modal or banner) inside an app to notify the user. I saw your answers in F&Q:

Use show_in_foreground attribute to tell app to show banner even if the app is in foreground. Warning: foreground banner won't show in android for remote notification due to limitation of FCM SDK. However you can create a local notification yourself. A pull is welcome to fix this.

When you say you can create a local notification yourself. what do you mean?

evollu commented 7 years ago

Q1: I've asked Firebase and the team decides to show the banner silently only. Q2. You should see a banner. let me try to reproduce

ukutaht commented 7 years ago

Might be related. I wasn't seeing local notifications either. Turns out it was because ic_notification icon wasn't found in android resources and the notification centre refused to show the notification without an icon.

Maybe these lines should default to ic_launcer if the resource is not found and log a warning? Currently it trusts the library user to provide a usable icon name.

proelite commented 7 years ago

The readme should be changed to ic_launcher for the small icon in notification.

evollu commented 7 years ago

Will update

evollu commented 7 years ago

@woniesong92 does this solve your issue?

rasmusviben commented 7 years ago

Hi @evollu. Is it not possible to show the banner when the app is in background at all? I know you answered this: Q1: I've asked Firebase and the team decides to show the banner silently only. But is there a way to get around this, so it is showing the banner when app is in background?

evollu commented 7 years ago

@rasmusviben check the latest repo. It provides a way to show the banner now

avishayil commented 7 years ago

Still doesn't work for me here. Local notification not showing when app is on foreground.

dino115 commented 7 years ago

Hi, i have still the same issue on Android it doesn't show the notification banner when app is in background and it shows nothing if the app is in foreground (except sound and vibration, but no banner and no notification in the notification center).

Using: RN: 41.2 RN-FCM: 6.1.0 Android: 7.1 Firebase: 10.0.1

This is my code to create a local notification, i think there is nothing special here:

FCM.presentLocalNotification({
    title: 'My title',
    body: 'My Body',
    big_text: 'My big Text',
    priority: "high",
    show_in_foreground: true,
    local: true,
    icon: "ic_launcher",
    large_icon: "ic_launcher",
});

I'have tried local notifications and remote notifications with the custom_notification feature. Also tried with different values, but it's always the same result. To setup notifications on android i followed the README and checked it several times, have i missed something, maybe? Do you have any advice or solution for this?

evollu commented 7 years ago

have you tried the example project?

dino115 commented 7 years ago

For now i've only used code from the example project and it doesn't work in my project. But when building the example project it works as expected.

I don't know why, maybe i have forgot something or doesn't use it correctly. The Javascript code is pretty clear and easy, currently i think it has something todo with the android manifest or build... i will try it again.

evollu commented 7 years ago

have you verified permission setup in AndroidManifiest.html?

dino115 commented 7 years ago

Ok, i'm sorry i found the solution!

We are not native guys, so we followed a random internet guide to setup the android app. And we don't have an "ic_launcher" icon. We use some other custom named icons... So when i change the icon to our correct icon name it works like a charm! Finally... :)

Thanks for your patience and your great work with this package!

evollu commented 7 years ago

cool

ducpt2 commented 6 years ago

@evollu @dino115 I try to change icon to the right path, but no luck. No banner show on when i push message from firebase. Any suggest? Many thanks.

anurak-kg commented 6 years ago

same problem but cause from my phone notification setting