0

When sending from FCM i.e Firebase console in react native it is easily coming in iOS and Android both but when i am sending notification from admin or backend side it is not coming when app is in kill state. I am using react-native-fcm .

Thanx .

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="22" />

<application
  android:name=".MainApplication"
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/brainbg"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait"  >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />


  <service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
    <intent-filter>
     <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
  </service>

  <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
    <intent-filter>
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
  </service>


  <receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
    <receiver android:enabled="true" android:exported="true"  android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

</application>
Kartik Shah
  • 866
  • 5
  • 19
  • can you share your code? As if the app is in kill state then native side should capture notifications. check android manifest that all the dependency are properly setup or not. Or you can debug using android Logcat. – Rajat Gupta Mar 26 '18 at 11:37
  • Here's a good reference for Android -- https://stackoverflow.com/q/39504805/4625829 – AL. Mar 26 '18 at 12:11

1 Answers1

1

I had the same issue, resolved it by adding the code below after receiving the fcm token,

FCM.setBadgeNumber(0);

Above is for android for iOS you need to turn on background notifications in Xcode.enter image description here In Xcode select your project and open capabilities and see if background modes in on. Refer to the screenshot

Here is the snippet of the code i used

export const MyNotificationReceiver = props => {
  FCM.getInitialNotification().then(notif => {
  });

  FCM.setBadgeNumber(0); //Add this for background notification

  this.notificationListener = FCM.on(FCMEvent.Notification, async notif => {
    var notification_messgae = "";
    try {
      notification_messgae = notif.default;
    } catch (error) {
      notification_messgae = JSON.stringify(notif);
    }
    if (osDetection) {
      switch (notif._notificationType) {
        case NotificationType.Remote:
          notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
          break;
        case NotificationType.NotificationResponse:
          notif.finish();
          break;
        case NotificationType.WillPresent:
          notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
          break;
      }
    }
    if (!osDetection()) {
      if(notif.default)
      {
        FCM.presentLocalNotification({
          body: notif.default,
          priority: "high",
          show_in_foreground: true,
          large_icon: "noti_icon",                           // Android only
          icon: "noti_icon",                                // as FCM payload, you can relace this with custom icon you put in mipmap
          android_actions: JSON.stringify([{
            id: "view",
            title: 'view'
          }, {
            id: "dismiss",
            title: 'dismiss'
          }]) // for android, take syntax similar to ios's. only buttons are supported
        });
       }

    }
  });
};
  • Thank you so much bro.... @Shashank ..... searching from last 7 days.... Great work ... Cheers! – Kartik Shah Mar 26 '18 at 12:23
  • You mean, by this above code, I can receive push notifications in IOS and execute some logic like - fetch the data from the server to update the APP etc. I want to do this even if the IOS app is in killed state. Can I? Currently I am using react-native-firebase and unable to achieve this in IOS (app killed state). Please help – Shubham1164 Feb 12 '19 at 16:59
  • Yes, you will receive notification in killed state and on tap event, you will get a callback in App were you have data required to refresh the App – Shashank Srivastava Feb 17 '19 at 15:15