evollu / react-native-fcm

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

how to receive notification when app running in foreground #616

Open azamkth opened 7 years ago

azamkth commented 7 years ago

When the app is in foreground this does not work at all FCM.on("notification", notif => {

kimvex commented 6 years ago
    FCM.presentLocalNotification({
          id: "UNIQ_ID_STRING",                               // (optional for instant notification)
          title: "Title",                     // as FCM payload
          body: "Body",                    // as FCM payload (required)
          sound: "default",                                   // as FCM payload
          priority: "high",                                   // as FCM payload
          click_action: "ACTION",                             // as FCM payload
          badge: 1,                                          // as FCM payload IOS only, set 0 to clear badges
          number: 1,                                         // Android only
          ticker: "My Notification Ticker",                   // Android only
          auto_cancel: true,                                  // Android only (default true)
          large_icon: "ic_launcher",                           // Android only
          icon: "ic_launcher",                                // as FCM payload, you can relace this with custom icon you put in mipmap
          big_text: "Show when notification is expanded",     // Android only
          sub_text: "This is a subText",                      // Android only
          color: "red",                                       // Android only
          vibrate: null,                                       // Android only default: 300, no vibration if you pass null
          tag: 'correcto',                                    // Android only
          group: "group",                                     // Android only
          picture: "https://google.png",                      // Android only bigPicture style
          my_custom_data:'my_custom_field_value',             // extra data you want to throw
          lights: true,                                       // Android only, LED blinking (default false)
          show_in_foreground: true                                 // notification when app is in foreground (local & remote)
      });

this line show_in_foreground: true

evollu commented 6 years ago

@azamkth you have to describe more about your environment. what os? what os version? local or remote notif?

darkkaze commented 6 years ago

i have the same question. FCM.presentLocalNotification is not the solution

i'm sending a message from my server, and when the app is in foreground the notification is not showed on my notifications bar

ansal-exp commented 6 years ago

@darkkase you can use the local notification like on the on notification call back like

FCM.on("notification", notif => {
      if(notif.fcm && notif.fcm.body) {
          /* Create local notification for showing in a foreground */
          FCM.presentLocalNotification({
             body: notif.fcm.body,
             priority: "high",
             title: notif.fcm.title,
             sound: "default", 
             "large_icon": "ic_launcher",// Android only
             icon: "ic_launcher",
             "show_in_foreground" :true, /* notification when app is in foreground (local & remote)*/
             vibrate: 300, /* Android only default: 300, no vibration if you pass null*/
             "lights": true, // Android only, LED blinking (default false)
             status: notif.status
         });
      }
}
danielfx90 commented 6 years ago

@evollu is what @ansal-exp wrote valid? I tried it but it shows the local notification twice!

evollu commented 6 years ago

@danielfx90 not my intended usage. in 99% cases you don't need to call local notification specifically in notification callback