React-native-fcm: Notification not get when app is first time installed in background

Created on 13 Jun 2018  路  2Comments  路  Source: evollu/react-native-fcm

Hello,I am work on an app and i have follow all step even i get notification very nicely but i have an issue that is as follow:
1.installed app first time the notification come
2.but when is go on first time it is come in force stop mode and can't get notification

  1. and when app is reopen manually is work fine
    This very big problem for me,So please give me solution for how to get notification when app is in first time in background.
    thankyou.

Most helpful comment

import FCM, {NotificationActionType, FCMEvent, WillPresentNotificationResult, NotificationType, RemoteNotificationResult} from "react-native-fcm";

// this shall be called regardless of app state: running, background or not running. Won't be called when app is killed by user in iOS
FCM.on(FCMEvent.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

    console.log('what i got from   local notif ',notif)
  }


  if(notif.opened_from_tray){
    //iOS: app is open/resumed because user clicked banner
    //Android: app is open/resumed because user clicked banner or tapped app icon
  }

});

FCM.on(FCMEvent.RefreshToken, (token) => {
  console.log(token)
  // fcm token may not be available on first load, catch it here
});

export default class App extends Component<Props> {


      componentDidMount(){

         // iOS: show permission prompt for the first call. later just check permission in user settings
          // Android: check permission in user settings
          FCM.requestPermissions({badge: true, sound: true, alert: true}).then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));

          FCM.getFCMToken().then(token => {
              console.log(token)
              // store fcm token in your server
          });

          if (Platform.OS === "ios") {
     FCM.getAPNSToken().then(token => {
       console.log("APNS TOKEN (getFCMToken)", token);
     });
   }

          this.notificationListener = FCM.on(FCMEvent.Notification, (notif) => {
              // optional, do some component related stuff
              console.log('what i got from server in listener ',notif.custom_notification);

              try{
                console.log('inside locals')
                var data = JSON.parse(notif.custom_notification);
              //  console.log(data.picture);
                FCM.presentLocalNotification({
                  vibrate: 500,
                  title: data.title,
                  body: data.body,
                  color: data.color,
                // sub_text:data.sub_text,
                 picture: data.picture,
                // big_text: data.big_text,
                  priority: data.priority,

                  large_icon: data.icon,
                  show_in_foreground: data.foreground,


                });


              }catch(e){
                console.log(e)
              console.log("Hello")
              }
              //console.log("Notification",JSON.parse(notif.custom_notification));
           //   Alert.alert('hi')
          });

          // initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
          // sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
          // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
          FCM.getInitialNotification().then(notif => {
             console.log('initial notif',notif)
          });


      }

  render() {
    return (

  <Application/>

    );
  }
}

All 2 comments

import FCM, {NotificationActionType, FCMEvent, WillPresentNotificationResult, NotificationType, RemoteNotificationResult} from "react-native-fcm";

// this shall be called regardless of app state: running, background or not running. Won't be called when app is killed by user in iOS
FCM.on(FCMEvent.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

    console.log('what i got from   local notif ',notif)
  }


  if(notif.opened_from_tray){
    //iOS: app is open/resumed because user clicked banner
    //Android: app is open/resumed because user clicked banner or tapped app icon
  }

});

FCM.on(FCMEvent.RefreshToken, (token) => {
  console.log(token)
  // fcm token may not be available on first load, catch it here
});

export default class App extends Component<Props> {


      componentDidMount(){

         // iOS: show permission prompt for the first call. later just check permission in user settings
          // Android: check permission in user settings
          FCM.requestPermissions({badge: true, sound: true, alert: true}).then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));

          FCM.getFCMToken().then(token => {
              console.log(token)
              // store fcm token in your server
          });

          if (Platform.OS === "ios") {
     FCM.getAPNSToken().then(token => {
       console.log("APNS TOKEN (getFCMToken)", token);
     });
   }

          this.notificationListener = FCM.on(FCMEvent.Notification, (notif) => {
              // optional, do some component related stuff
              console.log('what i got from server in listener ',notif.custom_notification);

              try{
                console.log('inside locals')
                var data = JSON.parse(notif.custom_notification);
              //  console.log(data.picture);
                FCM.presentLocalNotification({
                  vibrate: 500,
                  title: data.title,
                  body: data.body,
                  color: data.color,
                // sub_text:data.sub_text,
                 picture: data.picture,
                // big_text: data.big_text,
                  priority: data.priority,

                  large_icon: data.icon,
                  show_in_foreground: data.foreground,


                });


              }catch(e){
                console.log(e)
              console.log("Hello")
              }
              //console.log("Notification",JSON.parse(notif.custom_notification));
           //   Alert.alert('hi')
          });

          // initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
          // sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
          // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
          FCM.getInitialNotification().then(notif => {
             console.log('initial notif',notif)
          });


      }

  render() {
    return (

  <Application/>

    );
  }
}

@kartavyaparekh96 thank you very much for your answer.. this code worked, that was enough..

FCM.getInitialNotification().then(notif => {
     console.log('initial notif',notif)
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nailikhaled picture nailikhaled  路  5Comments

gplopes picture gplopes  路  6Comments

mts88 picture mts88  路  5Comments

havinhthai picture havinhthai  路  4Comments

404sand808s picture 404sand808s  路  5Comments