React-native-firebase: 馃敟Remote push notifications stop after some time

Created on 13 Dec 2018  路  21Comments  路  Source: invertase/react-native-firebase


Issue

[Android]
I receive remote push notifications without any issues for a while after interacting with the app. When the app stays in the background for some time (about an hour, this time differs from device to device) the device stops receiving any push notifications. Once I open the app again, I start receiving push notifications as intended until I put the app in background and not interact with it for some time.

After reading a little on this issue, I figured that a lot of android devices kill off the app and it's services completely. Some Android devices have 'auto-start' turned off by default which prevents the background service to wake up after receiving a notification.

I have also noticed that some apps have an FAQ section on how to troubleshoot device-specific issues to recieve push notifications which includes suggestions like turning off battery optimizations, turning on auto-start, whitelisting app from battery saver, etc.

To understand the problem better, I took 2 approaches to FCM

1) notification + data messages from FCM
Payload from server:
```
payload = {
notification: {
title: "A title",
body: "Notification body"
},
data: {
dataExample1,
dataExample2
}
};

In this case I followed https://rnfirebase.io/docs/v5.x.x/notifications/receiving-notifications
Notification was generated by the OS and displayed appropriately

**2) data only from FCM**

payload = {
data: {
dataExample1,
dataExample2,
title: "A title",
body: "Notification body"
}
};

In this case I followed https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages
Once bgMessaging.js was triggered, I would create a local notification using the data from the payload


----------------

_**Either way, the behaviour remained the same. Devices stopped receiving notifications/fcm messages after a while.**_


<!-- Please describe your issue here --^ and provide as much detail as you can. -->
<!-- Include code snippets that show your usages of the library in the context of your project. -->
<!-- Snippets that also show how and where the library is imported in JS are useful to debug issues relating to importing or methods not found issues -->

---

## Project Files

<!-- Provide the contents of key project files which will help to debug -->
<!--     For Example: -->
<!--        - iOS: `Podfile` contents. -->
<!--        - Android: `android/build.gradle` contents. -->
<!--        - Android: `android/app/build.gradle` contents. -->
<!--        - Android: `AndroidManifest.xml` contents. -->

<!-- ADD THE CONTENTS OF THE FILES IN THE PROVIDED CODE BLOCKS BELOW -->

### iOS

#### `ios/Podfile`:

- [ ] I'm not using Pods
- [x] I'm using Pods and my Podfile looks like:

```ruby
# N/A

AppDelegate.m:

// N/A

Android

android/build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath 'com.google.gms:google-services:4.0.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        google() 
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 27
                buildToolsVersion "27.0.2"
            }
        }
    }
}

ext {
    buildToolsVersion = "26.0.3"
    minSdkVersion = 16
    compileSdkVersion = 26
    targetSdkVersion = 26
    supportLibVersion = "26.1.0"
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

android/app/build.gradle:

(I have removed a few irrelevant lines and comments)

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "xxxxxxxxx"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 999
        versionName "xxxxx"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        multiDexEnabled true
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    signingConfigs{
        release { 
            storeFile file(KEYSTORE_STORE_FILE)
            storePassword pass
            keyAlias KEY_ALIAS
            keyPassword pass
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation project(':react-native-document-picker')
    implementation project(':react-native-interactable')
    implementation project(':rn-fetch-blob')
    implementation project(':react-native-svg')
    implementation project(':react-native-firebase')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation "com.google.android.gms:play-services-base:16.0.1"
    implementation "com.google.firebase:firebase-core:16.0.4"
    implementation "com.google.firebase:firebase-auth:16.0.5"
    implementation "com.google.firebase:firebase-firestore:17.1.2"
    implementation "com.google.firebase:firebase-messaging:17.3.4"
    implementation "com.google.firebase:firebase-storage:16.0.4"
    implementation "me.leolin:ShortcutBadger:1.1.21@aar"

    apply plugin: 'com.google.gms.google-services'
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

android/settings.gradle:

rootProject.name = 'xxxxx'
include ':react-native-document-picker'
project(':react-native-document-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-document-picker/android')
include ':react-native-interactable'
project(':react-native-interactable').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-interactable/android')
include ':rn-fetch-blob'
project(':rn-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/rn-fetch-blob/android')
include ':react-native-svg'
project(':react-native-svg').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-svg/android')
include ':react-native-firebase'
project(':react-native-firebase').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-firebase/android')

include ':app'

MainApplication.java:

package xxxx.xxx.xxxx;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.reactnativedocumentpicker.ReactNativeDocumentPicker;
import com.wix.interactable.Interactable;
import com.RNFetchBlob.RNFetchBlobPackage;
import com.horcrux.svg.SvgPackage;
import io.invertase.firebase.RNFirebasePackage;
import io.invertase.firebase.auth.RNFirebaseAuthPackage;
import io.invertase.firebase.firestore.RNFirebaseFirestorePackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
import io.invertase.firebase.storage.RNFirebaseStoragePackage; 
import java.util.Arrays;
import java.util.List;
import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new ReactNativeDocumentPicker(),
            new Interactable(),
            new RNFetchBlobPackage(),
            new SvgPackage(),
            new RNFirebasePackage(),
            new RNFirebaseAuthPackage(),
            new RNFirebaseFirestorePackage(),
            new RNFirebaseMessagingPackage() ,
            new RNFirebaseStoragePackage() ,
            new RNFirebaseNotificationsPackage()
      );
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="xxxxxx">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove"/>
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme"
      >
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize"
        android:launchMode="singleTop"        
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
     <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
    </service>
    <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
      <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
      </intent-filter>
    </service>
    <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
      <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_notification" />
     <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="messages"/>
    </application>

</manifest>

Environment

  • Platform that you're experiencing the issue on:

    • [ ] iOS

    • [x] Android

    • [ ] iOS but have not tested behavior on Android

    • [ ] Android but have not tested behavior on iOS

    • [ ] Both

  • If known, the version of the platform are you experiencing the issue on:

    • Observed and tested on Android API 25, 27 , 28

  • Operating System:

    • [x] MacOS, version: 10.14.1

    • [ ] Windows, version: N/A

    • [ ] Other, please specify: N/A

  • Build Tools:

    • Android Studio 3.2.1

  • React Native version:

    • 0.57.7

  • React Native Firebase library version:

    • 5.1.1

  • Firebase module(s) you're using that has the issue:

    • [x] Authentication

    • [ ] Analytics

    • [x] Cloud Firestore

    • [x] Cloud Messaging (FCM)

    • [ ] Crashlytics

    • [ ] Dynamic Links

    • [ ] Functions Callable

    • [ ] Invites

    • [ ] Instance ID

    • [x] Notifications

    • [ ] Performance Monitoring

    • [ ] Realtime Database

    • [ ] Remote Config

    • [x] Storage

  • Are you using TypeScript?

    • [x] No

    • [ ] Yes, version: N/A

  • Are you using Expo, e.g. ExpoKit?

    • [x] No

    • [ ] Yes, I've _not_ ejected

    • [ ] Yes, but I have ejected to ExpoKit

    • [ ] Yes, but I have ejected to vanilla React Native

    • Expo version: N/A




Think react-native-firebase is great? Please consider supporting the project with any of the below:

Notifications Stale

Most helpful comment

Any updates?

All 21 comments

same issue

I have a very similar issue, sometimes notifications just don't work. We are sending data-only notifications.
When this happens firebase.notifications().displayNotification() doesn't enter neither then nor catch

EDIT
I've found out that when this is happening the AsyncTask DisplayNotificationTask is not properly executed. Instead, it is instantiated but its doInBackground method is never called.

@deorukhkarchaitanya are you listening for fcm registration token changes and updating your backend with the devices new token when it changes, I'd imagine if you're not then the issue probably is that you're pushing to an old registration token.

@Salakar yes, I am listening to the token changes. The token remains unchanged, once I re-open the app and it goes in the background after a while it stops receiving notifications.

For Xiaomi devices turning on "auto-start" fixed this problem. Weird enough, apps like Facebook and Whatsapp have "auto-start" as on by default.

For an Android Go device, I tried whitelisting the app but still no luck.

I suspect this is OEM specific issue but there needs to be some way to deliver these notifications.

Any news on this?

I have the same issue.

I am also having the same issue in android mobiles which having android version 7 and above.
Below version 7 it is working fine.
Any solution?

Any updates?

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

This issue should not close yet

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

Same issue here!

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

I'm also having the same issue, background messages works until it doesn't.
Each time the app is opened, it starts working for a while again.

Hello 馃憢, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.

Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.

Still getting issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

n-scope picture n-scope  路  3Comments

alizahid picture alizahid  路  3Comments

Draccan picture Draccan  路  3Comments

escobar5 picture escobar5  路  3Comments

rtman picture rtman  路  3Comments