Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onMessageRecieved not working when app is killed. Any solution? #822

Closed
KORuL opened this issue Mar 2, 2019 · 27 comments
Closed

onMessageRecieved not working when app is killed. Any solution? #822

KORuL opened this issue Mar 2, 2019 · 27 comments

Comments

@KORuL
Copy link

KORuL commented Mar 2, 2019

Step 1: Describe your environment

  • Android device: Meizu M1 Note
  • Android OS version: 5.1
  • Google Play Services version: com.google.gms:google-services:4.2.0
  • Firebase/Play Services SDK version:
    implementation 'com.google.firebase:firebase-core:16.0.7'
//    implementation 'com.google.firebase:firebase-iid:17.1.0'
    implementation 'com.google.firebase:firebase-messaging:17.4.0'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
    implementation 'com.google.firebase:firebase-functions:16.1.3'
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    implementation 'com.google.firebase:firebase-database:16.0.6'
    implementation 'com.google.firebase:firebase-crash:16.2.1'
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.google.gms:google-services:4.2.0'
  

Step 2: Describe the problem:

Almost all phones are fine. But in some phone (Meizu M1 note):

onMessageRecieved not working when app is killed in some devices. It works perfect in foreground and in background. But when app is killed I don t receive notification in system tray, or call onMessageRecieved .

I send only DATA (to, data) notification. What I do wrong?

I read #4 , #89 , #86 , #368 , #365 , #337 , #205 , #96 , #20 , #4, #151 , #816 did not find the answer.

I get a strange error in logcat W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=net.korul.hbbft (has extras) }

I made this receiver - but it did not help

<receiver
            android:name=".MyBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="net.korul.hbbft" />
            </intent-filter>
        </receiver>

AndroidManifest contains:

        <service android:name=".services.MyFirebaseMessagingService"
                 android:permission="true"
                 ndroid:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.firebase. MESSAGING_EVENT"/>
            </intent-filter>
        </service>

MyFirebaseMessagingService - is standart from snippset

Steps to reproduce:

  1. Minimize and swipe out your application
  2. Send FCM notification
  3. You can see the below log but notification doesn't trigge

Observed Results:

W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=net.korul.hbbft (has extras) }

Expected Results:

Should get the notification

Relevant Code:

I read all the branches dedicated to this issue, do not refer to them! The problem is, she is over 2 years old! Need a solution!

I also tried to send a push message with the firebase console, and it does not come - the exact same error

@google-oss-bot
Copy link

This issue does not seem to follow the issue template. Make sure you provide all the required information.

@samtstern
Copy link
Contributor

@KORuL thank you for reading all of those other issues. It may very well be that Meizu has device-specific behavior changes which break FCM. Does this happen for you on other manufacturer's devices like Samsung or Google?

@kroikie do you think there is anything different going on here versus the other issues?

@KORuL
Copy link
Author

KORuL commented Mar 30, 2019

This does not happen with Google and Samsung devices.
But how can I deal with those who do not come pushes? Do I have to hang in the form of service and put the battery? Or are there other ways? How to implement your own, for example, messenger if you don’t get it?

@jshandorov1
Copy link

Hello,
Exactly the same behaviour on my Huawei P20 Pro + Android 9. Just I have to wait ~12 hours and app gets optimized automatically. No notifications are received after this.

@EdwardQuixote
Copy link

EdwardQuixote commented Apr 5, 2019

I get the same problem on a Tecno Spark:

  • Model: K7
  • Android 7.0

Even worse is how, the app is in the "Recent Apps" list, but still doesn't get any push messages when it gets into Doze. Please note I'm using FCM High Priority Messages and I'm sending a data type of message.

I tried out other push notification services on the same device and I noticed one of the services, does restart it's notification listening service even when a user kills the App:

W/ActivityManager: Scheduling restart of crashed service me.pushy.example/me.pushy.sdk.services.PushySocketService in 1000ms
D/ActivityManager: AMS: *** Restart service me.pushy.example/me.pushy.sdk.services.PushySocketService
D/ActivityManager: AMS: Service me.pushy.example/me.pushy.sdk.services.PushySocketService is called by self.

Q) Is it possible to have such an implementation on FCM, regardless of the device?

@samtstern
Copy link
Contributor

@EdwardQuixote just FYI the "pushy" service does not use FCM it uses its own MQTT-based push notification service so it has totally different properties.

@KORuL
Copy link
Author

KORuL commented Apr 5, 2019

@samtstern Thank you, I will look at this library! It’s a pity that @google @samtstern doesn’t want to do anything with an obvious BAG, which has been preventing us from developing good applications for more than 2 years!

@EdwardQuixote
Copy link

@samtstern Yes it uses MQTT. I just thought maybe Google could give us something like it even if FCM uses different protocols.

@samtstern
Copy link
Contributor

@EdwardQuixote that's a reasonable thing to ask, I was just saying that there's no need for us to do something like "restart the service" since FCM messages are received by Google Play services and not your app itself. When you use something like Pushy then each app is going to keep its own connection open. So that's where the different behavior comes from. If every app on your phone had an MQTT connection open in the background your battery would be very unhappy :-)

@KORuL
Copy link
Author

KORuL commented Apr 6, 2019

@samtstern Why, then, Google will not fix this bug? Accepts push with google service and redirects the receiver in the application. It's good if all the push messages are handled by one service in the system, and only it keeps one connection for all. And yes, if each application opens the connection it will be bad. But what to do in this situation? Push is required when the application is closed!
I don’t like the answer of Google, from the category - we are fine on a pixel and samsung, so leave us alone! This is not an answer! Does not work on many phones!

@bsonmez
Copy link

bsonmez commented May 14, 2019

It's bizarre. It works correctly android eight on a real device, and Android nine on a simulator, however, doesn't work on an actual device with android six unless it works on background. When the app killed by the user, doesn't receive messages.

@rayworks
Copy link

rayworks commented May 24, 2019

Got the same issue on Nokia TA-1000 OS 9.0 with
com.google.gms:google-services:4.2.0
com.google.firebase:firebase-messaging:17.4.0

W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg="app-pkg-name" (has extras) }

Also failed on SAMSUNG-G9300 (8.0.0) with an extra info :
D/MARsPolicyManager: Package: app-pkg-name, userid: 0, hostingType: broadcast is Restricted by policy: autorun(2) caller is: ProcessRecord{1e82998 19971:com.google.android.gms.persistent/u0a188}

W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg="app-pkg-name" (has extras) }

However, it succeeded on Nexus 6P (OS 8.0), receiving the msg instantly. You could also check out this thread.

@montaqa
Copy link

montaqa commented Jun 27, 2019

i believe this has nothing to do with FCM, it is a service at the end of the day and you need to look on how to maintain a service from being killed by the OS and you will find a lot by googling on this.

@taar1
Copy link

taar1 commented Jul 1, 2019

I was able to solve the problem. In our app we are displaying a splash screen (SplashActivity.java) before the app starts.
So when a push notification comes in and I tapped it it opened the app but loaded SplashActivity and not MainActivity. SplashActivity does nothing else than opening MainActivity with an Intent. The problem there was that it didn't forward the Extras from the Bundle and therefore there never was any payload in MainActivity. Forwarding the payload from SplashActivity to MainActivity solved the problem for me.

Here how it looks like:


public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        // Forward the Payload of Push Notifications to the MainActivity here.
        if (getIntent().getExtras() != null) {
            intent.putExtras(getIntent().getExtras());
        }
        startActivity(intent);
        finish();
    }
}

@KORuL
Copy link
Author

KORuL commented Jul 14, 2019

I was able to solve the problem. In our app we are displaying a splash screen (SplashActivity.java) before the app starts.
So when a push notification comes in and I tapped it it opened the app but loaded SplashActivity and not MainActivity. SplashActivity does nothing else than opening MainActivity with an Intent. The problem there was that it didn't forward the Extras from the Bundle and therefore there never was any payload in MainActivity. Forwarding the payload from SplashActivity to MainActivity solved the problem for me.

Here how it looks like:


public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        // Forward the Payload of Push Notifications to the MainActivity here.
        if (getIntent().getExtras() != null) {
            intent.putExtras(getIntent().getExtras());
        }
        startActivity(intent);
        finish();
    }
}

Not the problem

@KORuL
Copy link
Author

KORuL commented Jul 14, 2019

I heard that for some there is a solution with adding an application to autostart, but this requires actions from the user, maybe there is some kind of flag in the manifest?

@chandreshandroid
Copy link

@KORuL

Yes,I am 100% sure it's auto start related issue. I have cross verify with manual on auto start permission. It's seems fine. Have you any solution for this?

@rayworks
Copy link

FYI. About granting auto start permission, there is a library trying to make it for all the non-stock android phones.

@mariooooo
Copy link

Meizu M5
Android 5.1
The same problem doesn't get the message when app killed, on the emulator 5.1 all work fine

@ambr89
Copy link

ambr89 commented Dec 18, 2019

Does anyone found the solution?

I've Android device and I receive the push message only if ap is in the background but not killed.

Android 8.0.0

@suseendhar
Copy link

Is there any solution found? issue happens in samsung galaxy mobiles too

@fluidsonic
Copy link

I have the same issue. Somewhat.

  • Latest Firebase libraries
  • Happens on emulator for Android 10 (API 29 with Google APIs)
  • Also happens on Google Pixel 2 with Android 10
  • App in background = all is fine
  • App removed from recent apps by swiping = no push notifications
  • Background restriction status: not restricted
  • Standby bucket: active

Logcat (app kill):

2020-04-09 21:21:00.263 2036-2058/? I/ActivityManager: Killing 898:<package>/u0a135 (adj 905): remove task
…
2020-04-09 21:21:01.088 2036-6481/? I/ActivityManager: Force stopping <package> appid=10135 user=0: from pid 1129

Logcat (message delivery):

W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=<package> (has extras) }

adb shell dumpsys package <package> | grep stopped:

User 0: ceDataInode=-4294836147 installed=true hidden=false suspended=false stopped=true notLaunched=false enabled=0 instant=false virtual=false

But:
After some testing it looks like that stopped=true is only the case when I've started the app with Android Studio. If I start the app manually or using adb shell am start -n and then kill it (swipe from recents) then the state is stopped=false and push notifications work just fine!

@Anuradha677
Copy link

when I am publishing message in firebase, the notification message displaying in Andriod Studio logcat. But the same notification is displayed in the emulator. I am using Andriod 9.0.
Can someone please help me with this?
I have build the messaging app in Andriod Studio.

@GoldLinPan
Copy link

I also can't receive notification in System Notification Bar when App is killed by swiping remove Unless the app is set to run in the background and can start automatically in the system settings (default is auto managed by System).

mobile phone : Huawei honor 20
System: Magic UI  3.1.0 (based on Android 10)

@evgentset
Copy link

Same issue on Samsung Galaxy A5 with Android 8.0.0.
I use Data Message with custom data payload.
Method onMessageReceived (from FirebaseMessagingService) is not called if the app was removed from recent app. Looks like the app was killed in this case, so FirebaseMessagingService also was stopped.

implementation 'com.google.firebase:firebase-messaging:20.2.0'

@evgentset
Copy link

ahhh... found this on SO - https://stackoverflow.com/questions/48642423/firebase-push-notification-issue-in-android-when-app-is-closed-killed
I tested with release build and it works for me.

@katowulf
Copy link

There is nothing actionable in this thread related to the Android quickstart; this isn't the correct place to troubleshoot message delivery issues.

@firebase firebase locked as off-topic and limited conversation to collaborators Jun 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests