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.54k stars 2.19k forks source link

πŸ”₯[Android] Notification not showing when app is killed #1888

Closed alexander126 closed 4 years ago

alexander126 commented 5 years ago

Issue

I am trying to display a notification, when a data-only notification is received from a third party platform. Its all good on iOS, but on Android when the app is killed, notification is never displayed. onMessageReceived event is triggered, followed by -

java.lang.IllegalStateException: Not allowed to start service Intent 
{ cmp=my.app/io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService (has extras) }: app is in background uid UidRecord{259bf42 u0a380 RCVR idle procs:1 seq(0,0,0)} 

Here is how my index.js looks like:

import { AppRegistry } from 'react-native';
import bgMessaging from './app/utils/bgMessaging';
import App from './app/App';

AppRegistry.registerHeadlessTask(
  'RNFirebaseBackgroundMessage',
  () => bgMessaging
);

AppRegistry.registerComponent('MyApp', () => App);

And my bgMessaging:


export default async message => {
  const localNotification = new firebase.notifications.Notification()
    .setNotificationId(message.messageId)
    .setTitle(message.data.author_name)
    .setSubtitle('Test')
    .setBody(message.data.body)
    .setData(message.data)
    .android.setChannelId('all')
    .android.setAutoCancel(true)
    .android.setPriority(firebase.notifications.Android.Priority.High);

  firebase.notifications().displayNotification(localNotification);
  return Promise.resolve();
};

Project Files

Android

AndroidManifest.xml:

   <uses-permission android:name="android.permission.INTERNET" />
    <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.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <application android:launchMode="singleTop" android:name=".MainApplication" 
    android:label="@string/app_name" android:icon="@mipmap/ic_launcher"  
    android:allowBackup="false" android:fullBackupContent="false" android:theme="@style/AppTheme" 
    tools:replace="android:allowBackup">
     <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" />

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

antaehyeon commented 5 years ago

The same issue. What's the problem? My iOS error :

Possible Unhandled Promise Rejection Error: Failed to display notificaton

antaehyeon commented 5 years ago

In the case of Android,

remove implementation "com.google.firebase:firebase-ads:16.0.1" implementation "com.google.firebase:firebase-invites:16.0.6"

from app/build.gradle

This is a temporary measure.

alexander126 commented 5 years ago

@antaehyeon I don't really use ads or invites in my project, and also i can't see how this could fix the notifications

antaehyeon commented 5 years ago

Can you show me the app/build.gradle?

alexander126 commented 5 years ago

Here are my dependencies @antaehyeon

dependencies {
    ....
    implementation project(':react-native-fbsdk')
    implementation 'com.facebook.android:facebook-android-sdk:4.34.0'
    implementation "com.google.android.gms:play-services-auth:15.0.0"
    implementation(project(':react-native-firebase')) {
        transitive = false
    }    
    implementation project(':react-native-splash-screen')
    implementation 'io.intercom.android:intercom-sdk-fcm:5.+'
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:27.0.0"
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "com.google.firebase:firebase-core:16.0.4" // Core
    implementation "com.google.firebase:firebase-auth:16.0.5"
    implementation "com.google.firebase:firebase-messaging:17.3.4"
    implementation "com.google.firebase:firebase-database:16.0.4"
    implementation "com.google.firebase:firebase-storage:16.0.4"
    implementation "com.google.firebase:firebase-analytics:16.0.4"

}
antaehyeon commented 5 years ago

Umm.. In my case

implementation "com.google.firebase:firebase-core:16.0.1"
implementation "com.google.firebase:firebase-messaging:17.1.0"
implementation 'com.google.firebase:firebase-dynamic-links:16.1.5'
implementation "com.google.firebase:firebase-ads:16.0.1"
implementation "com.google.firebase:firebase-invites:16.0.6"

remove ads, invites from my app/build.gradle

It's solved to build with below

implementation "com.google.firebase:firebase-core:16.0.1"
implementation "com.google.firebase:firebase-messaging:17.1.0"
implementation 'com.google.firebase:firebase-dynamic-links:16.1.5'

after build successed (The app didn't shut down when Push)

I'm sorry I can't help you. What I recommend is to remove the other firebase one by one.

alexander126 commented 5 years ago

I can't really remove the other dependencies as I need them in my project

stale[bot] commented 5 years ago

Hello πŸ‘‹, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

Viswa-Vinod commented 5 years ago

I am facing the same issue in Android 8.0.0. Setting my data-only message to high priority did not help. I have updated to the latest version of play-services and firebase dependencies. My logcat shows the same java.illegalStateException

classpath 'com.google.gms:google-services:4.1.0'

implementation "com.google.android.gms:play-services-base:16.0.1"
// implementation "com.google.android.gms:play-services-cast:16.1.2"
implementation "com.google.firebase:firebase-core:16.0.7"
implementation "com.google.firebase:firebase-messaging:17.4.0"  //17.3.2

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:0.20.+"  // From node_modules
implementation 'com.android.support:support-vector-drawable:28.0.0'
implementation 'com.google.firebase:firebase-analytics:16.3.0'
implementation 'com.android.support:mediarouter-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
alexander126 commented 5 years ago

@Viswa-Vinod did you manage to fix this issue? Im still having it

Viswa-Vinod commented 5 years ago

Nope. But I am exploring this possibility as the root cause. I am not sure yet. https://medium.freecodecamp.org/why-your-push-notifications-never-see-the-light-of-day-3fa297520793

stale[bot] commented 5 years ago

Hello πŸ‘‹, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

onstash commented 5 years ago

Hi, guys. Is there a solution to this issue?

chevulkar commented 5 years ago

I am stuck on the same issue.

gabrielbull commented 5 years ago

Same here. Cannot show heads up notification on background.

alexander126 commented 5 years ago

I haven't found a solution yet as well.

leron8 commented 5 years ago

Has anyone other than us, been able to use this https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#2)-Handle-background-messages ? I can't seem to make it do anything.

alexander126 commented 5 years ago

@leron8 are you referring to headlessjs?

leron8 commented 5 years ago

Yes @alexander126 , I'm trying to do something like you, listen for notification messages while the app is dead, and wake up the app or send a local notification, but following the docs in React Native Firebase Headless JS, I can't seem to get it work.

alexander126 commented 5 years ago

My problem is this - the notification seems to be received by the device, however in the logs I can see that it's not opening the intent because I dont have permissions set to HIGH. However I set them to high in both the channel and when building the local notification. Do you set your notification's priority to high when receiving it @leron8 ?

leron8 commented 5 years ago

Are you using this command to see the logs ? adb logcat *:S ReactNative:V ReactNativeJS:V

And yes, I think I'm setting the priority, but the docs are a bit confusing as to how to do it. I've tried different options like (in the fcm http api payload): "priority": 10, "android":{ "priority":"normal" },

mikehardy commented 5 years ago

I think when debugging messaging, since it involves a play services connection that may not be within the react-native app, you might widen the logcat and simply adb logcat to get everything. Verbose but better than missing a clue

alexander126 commented 5 years ago

You can see the logs from adb logcat in Android Studio

onstash commented 5 years ago
04-25 19:16:58.348  7845  7845 W GCM     : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=<app_package_identifier> (has extras) }
04-25 19:16:59.238  7845  5202 W ChimeraUtils: Non Chimera context
04-25 19:16:59.333  1358  3582 E NotificationService: Suppressing notification from package by user request. isGroupBlocked: false isRankingImportanceNone: true isRecordImportanceNone: false
04-25 19:16:59.666  7845  5202 I chatty  : uid=10014(com.google.android.gms) GCMReader identical 2 lines
04-25 19:16:59.666  7845  5202 W ChimeraUtils: Non Chimera context
04-25 19:16:59.701 19130 19172 I DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
04-25 19:16:59.714   659 19291 I netd    : ;; res_nquery(slack.com, 1, 1)
04-25 19:16:59.746  1358  3582 I chatty  : uid=1000(system) Binder:1358_C identical 2 lines
04-25 19:16:59.768  1358  3582 E NotificationService: Suppressing notification from package by user request. isGroupBlocked: false isRankingImportanceNone: true isRecordImportanceNone: false
04-25 19:17:00.000  1358  1904 V AlarmManager: Triggering alarm #0: 3 when =978837681 package =android operation =*alarm*:android.intent.action.TIME_TICK flags =0x1

Those are the logs I obtained when I sent a notification to my app when my app was killed.

mikehardy commented 5 years ago

Very interesting. https://github.com/aosp-mirror/platform_frameworks_base/blob/master/services/core/java/com/android/server/notification/NotificationManagerService.java#L4260

Your notification is failing this test:

  final boolean isBlocked =
                mRankingHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup())
                || mRankingHelper.getImportance(pkg, callingUid)
                        == NotificationManager.IMPORTANCE_NONE
                || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE;

Based on the output, it is this specific chunk:

                || mRankingHelper.getImportance(pkg, callingUid)

Your mission, should you choose to accept it, is to follow this code through to figure out why it would return IMPORTANCE_NONE, and alter your notification so that it no longer returns that value... https://github.com/aosp-mirror/platform_frameworks_base/blob/master/services/core/java/com/android/server/notification/RankingHelper.java#L528

onstash commented 5 years ago

Guys, I could not look into what @mikehardy had suggested. Could the maintainers mention how to go about this?

mikehardy commented 5 years ago

Maybe this will get you closer: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/services/core/java/com/android/server/notification/RankingHelper.java#L296

something about importance, priority, visibility, badge, whether you have a channel or not.

Near as I can tell, you have 5 things you can alter - a little matrix of possibilities. There was mention in other issues about notification visibility being affected by importance and/or priority for instance. Maybe worth playing around?

mi-mazouz commented 5 years ago

does anybody solved this issue?

mikehardy commented 5 years ago

The key on this I think is that you have to be careful with notification settings to even get them to show, plus in the original issue there was this:

Not allowed to start service Intent indicating a problem with how things were configured. Probably something related to registering headless tasks, and the way the task was written

Again @mi-mazouz - log a fresh issue with all your details - it has the best chance of receiving help

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.

Vivekban commented 5 years ago

is there any solution of this ? as tried above given one already

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

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.

farjad6 commented 4 years ago

please help here. my problem is if app is in background notifications receive fine. but if app is closed no notification comes on device. P.S: not talking about any data retrieval from api, but notifications did not show up in system tray if app is killed

blastering66 commented 4 years ago

please help here. my problem is if app is in background notifications receive fine. but if app is closed no notification comes on device. P.S: not talking about any data retrieval from api, but notifications did not show up in system tray if app is killed

In what Device u tested ? and what version is ur RN Firebase ??

Chinnese Rom has problems with killed state in FCM Notification