invertase / react-native-firebase

πŸ”₯ A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.63k stars 2.2k forks source link

πŸ”₯Remote push notifications stop after some time #1757

Closed chaitanyadeorukhkar closed 4 years ago

chaitanyadeorukhkar commented 5 years ago

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.


Project Files

iOS

ios/Podfile:

# 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


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

Nits7029 commented 5 years ago

same issue

nickfla1 commented 5 years ago

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.

Salakar commented 5 years ago

@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.

chaitanyadeorukhkar commented 5 years ago

@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.

nickfla1 commented 5 years ago

Any news on this?

AugustoAleGon commented 5 years ago

I have the same issue.

Arun1295 commented 5 years ago

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?

keech commented 5 years ago

related. https://github.com/firebase/firebase-ios-sdk/issues/2438 https://github.com/invertase/react-native-firebase/issues/1972

keech commented 5 years ago

Any updates?

stale[bot] commented 5 years ago

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.

keech commented 5 years ago

This issue should not close yet

stale[bot] commented 5 years ago

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.

keech commented 5 years ago

https://github.com/invertase/react-native-firebase/issues/1972

stale[bot] commented 5 years ago

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.

stale[bot] commented 5 years ago

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.

fgagneten commented 5 years ago

Same issue here!

stale[bot] commented 4 years ago

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.

dev-nilton commented 4 years ago

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.

stale[bot] commented 4 years ago

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.

stale[bot] commented 4 years ago

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.

chauhanPankaj007 commented 3 years ago

Still getting issue

areebvohra commented 3 years ago

i'm also getting this issue on Xiaomi and realme devices

mikehardy commented 3 years ago

Those vendors have corrupted the Android ecosystem by mis-implementing the power management APIs on purpose, complaints should go to them, and I hope with AOSP for Android 12 where the Android team promised to add compatibility checks for the power management APIs into the CTS these vendors are forced to behave. https://dontkillmyapp.com

Andriiklymiuk commented 2 years ago

Also having the same issue, when device stays in background for several days and then notifications stop coming at all. Problem only disappears, if device is terminated and opened again. Have someone figured out any way to overcome this issue?

mikehardy commented 2 years ago

https://dontkillmyapp.com probably...

mikehardy commented 1 year ago

@nasirdev74 how did it go when you followed the link I posted above and made sure Realme device wasn't doing power saving? What evidence do you see in the logs as you test this, so we have concrete detailed information to move the issue forward other than a simple "me too" comment?

AhsanRiyad commented 11 months ago

same issue

vramirezp commented 11 months ago

same here

hudsonthewolfman commented 5 months ago

Same issue

JabalnurIT commented 2 months ago

same issue

Kaddtechnologies commented 1 month ago

this is still happening in 2024