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

[firebase_messaging] Crash when set onBackgroundMessage #125

Closed
festelo opened this issue Sep 10, 2019 · 54 comments
Closed

[firebase_messaging] Crash when set onBackgroundMessage #125

festelo opened this issue Sep 10, 2019 · 54 comments
Assignees
Labels
impact: critical Usually a production crash with high user impact. (P1) impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging type: bug Something isn't working

Comments

@festelo
Copy link

festelo commented Sep 10, 2019

The following code causes immediate crash after app launching:

// Top Level. I also tried code from readme, no success.
Future<dynamic> myBackgroundMessageHandler(dynamic message) {
  return Future.value(0);
}

_firebaseMessaging.configure(
      onMessage: (d) async { print('on message'); handleMessage(d); },
      onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (d) async { print('on launch'); handleMessage(d); },
      onResume: (d) async { print('on resume'); handleMessage(d); },
    );

If myBackgroundMessageHandler method isn't on top level, then it's not crashing, but throws null exception ("'toRawHandle' was called on null")

Without onBackgroundMessage: myBackgroundMessageHandler, all works fine.

Application.java class:

package com.protreal;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

compileSdkVersion - 28
google-services version - 4.3.0

Crash log:

V/FA      (24891): Inactivity, disconnecting from the service
F/libc    (24891): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 in tid 24938 (Thread-3), pid 24891 (com.protreal)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'Xiaomi/cepheus/cepheus:9/PKQ1.181121.001/V10.2.3.0.PFAMIXM:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 24891, tid: 24938, name: Thread-3  >>> com.protreal <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1
Cause: null pointer dereference
    x0  0000000000000001  x1  0000007e86488660  x2  0000007e86400000  x3  0000000000000002
    x4  0000000000000088  x5  0000000000000000  x6  0000007e723beef0  x7  00000000000038c7
    x8  0000007e6abb7b60  x9  0000007e6abb7b60  x10 00000000000000e3  x11 0000000000000000
    x12 0000000000000003  x13 0000000000000010  x14 0020e0a7d87b2613  x15 0000007e723beef0
    x16 0000007f11519bd0  x17 0000007f1149ac28  x18 0000000000000010  x19 0000007e68c3b6e0
    x20 0000007e88844d40  x21 0000007e68227e60  x22 0000007e68227e90  x23 0000007e81778b98
    x24 000000000000003b  x25 0000007e723bf260  x26 0000007e723bf588  x27 000000000000003b
    x28 0000000000000001  x29 0000007e723bf3a0
    sp  0000007e723bf0f0  lr  0000007e73128078  pc  0000007e73124350
backtrace:
    #00 pc 0000000000d64350  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #01 pc 0000000000d68074  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #02 pc 0000000000d5fc0c  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #03 pc 0000000000d65f88  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #04 pc 0000000000d66664  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #05 pc 0000000000d65a88  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #06 pc 0000000000d6ac00  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #07 pc 0000000000014ff8  /system/lib64/libutils.so (android::Looper::pollInner(int)+836)
    #08 pc 0000000000014c18  /system/lib64/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+60)
    #09 pc 00000000000122a4  /system/lib64/libandroid.so (ALooper_pollOnce+96)
    #10 pc 0000000000d6ab84  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #11 pc 0000000000d659d4  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #12 pc 0000000000d68ad8  /data/app/com.protreal-Sp4ZeyXWIfVaHXpYDtdueg==/lib/arm64/libflutter.so (offset 0xd50000)
    #13 pc 000000000009043c  /system/lib64/libc.so (__pthread_start(void*)+36)
    #14 pc 0000000000023c38  /system/lib64/libc.so (__start_thread+68)
Lost connection to device.

On Xiaomi Mi 9 (9.0.0) & Genymotion virtual device (7.1.0) (gservices installed)

@festelo festelo added the type: bug Something isn't working label Sep 10, 2019
@oliwave
Copy link

oliwave commented Sep 14, 2019

I'm having the same issue.

@broberts-nybble
Copy link

Any updates on this issue?

@chrisurf
Copy link

I'm having the same issue

@ChrisOwen101
Copy link

I'm having a related issue here:
#144

@collinjackson collinjackson added the impact: critical Usually a production crash with high user impact. (P1) label Sep 30, 2019
@droidluv
Copy link

droidluv commented Oct 4, 2019

Yup its definitely crashing

@divan
Copy link

divan commented Oct 4, 2019

+1, crashing for me as well

@AymanMubark
Copy link

+1 For me as well

@billymahmood
Copy link

Same issue

[✓] Flutter (Channel stable, v1.9.1+hotfix.4, on Mac OS X 10.15 19A583, locale en-GB)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.0)
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.38.1)
[✓] Connected device (2 available)

• No issues found!

@broberts-nybble
Copy link

is this ever going to get fixed?

@weizhongshen
Copy link

+1, Same issue

@bthuan
Copy link

bthuan commented Oct 17, 2019

I have the same issue, but when i switch flutter channel to beta ( according to this: #144) then there's no crash anymore

@masinamichele
Copy link

Same issue here

@febg11
Copy link

febg11 commented Oct 20, 2019

same

@kleinpetr
Copy link

same

@digarahayu
Copy link

got same issue..
any update for fix this issue...?

@iapicca iapicca added the impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) label Oct 23, 2019
@iapicca
Copy link

iapicca commented Oct 23, 2019

@rlsutton1
Copy link

same problem here

@BansookNam
Copy link

same problem. It's critical bug.

@koteezy
Copy link

koteezy commented Dec 7, 2019

Guys, did you try call the messaging on top of you'r app?

I tried like this and it works for me ( so far. )

Both in dev mode and in release mode works fine now.

Future bgMsgHdl(Map<String, dynamic> message) async {
  print("onbgMessage: $message");
}

main() {
WidgetsFlutterBinding.ensureInitialized();

void firebaseMessagingHandler() {
messaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onBackgroundMessage: bgMsgHdl,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}

firebaseMessagingHandler();

messaging.getToken().then((v) {
print("Token!");
print(v);
});
setupDeviceUuid();
setupDatabase().then((db) => {runApp(MyApp())});
}

@DyaryRaoof
Copy link

Guys, did you try call the messaging on top of you'r app?

I tried like this and it works for me ( so far. )

Both in dev mode and in release mode works fine now.

Future bgMsgHdl(Map<String, dynamic> message) async {
  print("onbgMessage: $message");
}

main() {
WidgetsFlutterBinding.ensureInitialized();

void firebaseMessagingHandler() {
messaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onBackgroundMessage: bgMsgHdl,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}

firebaseMessagingHandler();

messaging.getToken().then((v) {
print("Token!");
print(v);
});
setupDeviceUuid();
setupDatabase().then((db) => {runApp(MyApp())});
}

this Doesn't work for me.

@Eddydpyl
Copy link

Eddydpyl commented Dec 8, 2019

Resolving by change channel to master, but, some another packages not working correctly on master channel ¯_(ツ)_/¯

Nor does this work for me. Though my problem is with iOS #1469. I've seen it mentioned as a solution in another issue, and it has a few likes #144 (comment). It may very well fix the problem in Android devices, but not in iOS.

@DyaryRaoof
Copy link

@Eddydpyl I have set myBackGroundMessagingHandler to null on iOS. you don't need the handler on iOS. I have set it this way :

 onBackgroundMessage:
          Platform.isAndroid ? myBackgroundMessageHandler : null,

and it works perfectly fine. I receive notifications when app is in background , terminated, or in foreground.

@Eddydpyl
Copy link

@DyaryRaoof how do I handle data messages in iOS then? I don't want the notification, just the data payload, which is why I'm setting onBackgroundMessage instead of onMessage, onLaunch or onResume.

@DyaryRaoof
Copy link

DyaryRaoof commented Dec 11, 2019

@Eddydpyl You get the payload as part of the message variable that you have setup when you configured fireBase.
for example :

_fcm.configure(
     onMessage: (Map<String, dynamic> message) async {
       print("onMessage: $message");
            String payload = message['data'];
     },
     onBackgroundMessage:
         Platform.isAndroid ? myBackgroundMessageHandler : null,
     onLaunch: (Map<String, dynamic> message) async {
       print("onLaunch: $message");
             String payload = message['data'];
     },
     onResume: (Map<String, dynamic> message) async {
       print("onResume: $message");

             String payload = message['data'];
     },
   );

print the message variable see if you have 'data' as part of the JSON.

@Eddydpyl
Copy link

Eddydpyl commented Dec 12, 2019

@DyaryRaoof I do, but I failed to mention that I need that data in the background. On Android, using onBackgroundMessage I'm able to process said data wether the app is in the foreground or not, but on iOS the onMessage callback only triggers after I reopen the app.

@naumanmir
Copy link

naumanmir commented Dec 26, 2019

@Eddydpyl can you please tell how are you able to get messages in background on Android.? My app is crashing at startup with background message handler set. I am even configuring it as a top level function. Where are you configuring it. Can you help please?

@Eddydpyl
Copy link

Eddydpyl commented Dec 26, 2019

@naumanmir I wish... I'm not doing anything special, and I haven't actually tested it on any of the phone models that people are reporting it's not working on. Furthermore, there's this issue that popped up (#1709). You can find some more details on how I'm doing things there, but again, it's nothing special.

About iOS, I never got it to work, though I assume that it has more to do with a limitation of the OS than a bug on this library's part.

@bparrishMines
Copy link
Contributor

Hi @festelo

It seems people are still experiencing this crash.

Have you tried the same code on a different virtual device? I attempted to reproduce this with a Pixel 3 API 28,
'com.android.tools.build:gradle:3.5.3',
'com.google.gms:google-services:4.3.0'.

But, I wasn't able to get it to crash when adding the onBackgroundMessageHandler. This may potentially be a crash from Firebase rather than Flutter.

I'm also assuming that you were able to receive messages in the foreground with the FirebaseMessaging.configure() onMessage callback?

@DyaryRaoof
Copy link

DyaryRaoof commented Jan 10, 2020

yes the crash is still there even with applying the classpath and using Pixel 3 API 28 Emulator.
I also tried it on a real device (Samsung Galaxy 7) the same issue occurs. Tried it on stable and master branch the same issue is there.

@SebastienMelki
Copy link

SebastienMelki commented Jan 13, 2020

Hey everyone, did anybody figure out a solution for this? App crashes as soon as notification is received when onBackgroundMessage is enabled. Without it everything works fine. But then this means that I can't process notifications when app is terminated. I can just run code onLaunch. Which beats the purpose of onBackgroundMessage. Is it just not working ?

@DyaryRaoof
Copy link

no nothing up to now

@singun-lxd
Copy link

seemed like flutter framework bug

@bparrishMines bparrishMines removed their assignment Mar 10, 2020
@Gyoko
Copy link

Gyoko commented Apr 1, 2020

Followed official tutorial with up-to-date everything. Issue is still present - I receive notifications properly and everything but onBackgroundMessage is working

@sakinaboriwala
Copy link

Guys, even after so many upvotes and frustrations nobody from the team is taking this seriously, I really don't understand how two Google products have so much incompatibility. It was so much smoother to configure Push Notifications with React native than it has been for Flutter.

The Documentation is completely outdated, there is not mention of Kotlin related changes anywhere. And even if you somehow manage to get it working as soon as you add the onBackroundMessage function the App crashes with absolutely no error. I am on OnePlus 5T device.

Please give some kind of conclusion or assurance or solution. Something. It's been a week almost since I have been trying this. This should be a very straightforward configuration IMO. It's not even an edge case. Everyone is going to need Background messaging at some point in their Application Development Cycle.

@drikanius
Copy link

First and foremost I would like to say that I agree with @sakina1403 . It drives crazy how something as important as notification can be so messy and frustrating.

I managed, somehow, to make firebase_massage work. It's not the best solution (and really verbose) but I'll share it as it can solve someone's problem.

Assuming that firebase cloud with the basic notification is working (onMessage, onLaunch, onResume), and that I work on 1.18.0-9.0.pre.38 (master), I'll try to focus on onBackgroundMessage (which caused me more headaches).

Also, I used flutter_local_notification to display messages (also needed some plugin registration to make it work).

As in all comments reported, 'onBackgroundMessage' needs a static function to work (or top level). In my case, I implemented 'myBackgroundMessageHandler'.

FirebaseMessaging configure:

    _messaging.configure(
      onMessage: (message) async {
        _notification.show(0, 'title', 'onMessage body', NotificationDetails(
          AndroidNotificationDetails(
              'id',
              'name',
              'description'
          ),
          IOSNotificationDetails(),
        ));
        print('... onMessage: $message');
      },
      onLaunch: (message) async {
        print('.. onLaunch: $message');
      },
      onResume: (message) async {
        print('. onResume: $message');
      },
      onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
    );
    
  static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
    print('AppPushs myBackgroundMessageHandler : $message');
    _showNotification('background title', 'background body');
    return Future<void>.value();
  }

Not much mystery so far. But that part was what made everything stop working.

In 'android/app/src/main/java/' (where are folders I created, eg com/example/my_notification_test, - same path that appears in AndroidManifest.xml at 'package')

After creating these folders I created 3 files: 'Application.java', 'FirebaseCloudMessagingPluginRegistrant.java', and 'FlutterLocalNotificationPluginRegistrant.java'.

package <your package here, the same path that appears in AndroidManifest.xml at 'package')>;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {

    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
        FlutterLocalNotificationPluginRegistrant.registerWith(registry);
    }
}
package <your package here,  same path that appears in AndroidManifest.xml at 'package')>;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public final class FirebaseCloudMessagingPluginRegistrant{
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}
package <your package here,  same path that appears in AndroidManifest.xml at 'package')>;

import io.flutter.plugin.common.PluginRegistry;
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;

public final class FlutterLocalNotificationPluginRegistrant{

    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FlutterLocalNotificationPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

In AndroidManifest.xml I changed android:name to ".Application":

    <application
        android:name=".Application"

And I've added permission on top of the tag <application. More like this:

    <uses-permission android:name="android.permission.VIBRATE" />
    <application
        android:name=".Application"

Finally my _showNotification(...) function:

  static Future _showNotification(String title, String message) async {
    print('test...');

    var initializationSettingsAndroid = AndroidInitializationSettings('launcher');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: (id, title, body, payload) {
          print('onDidReceiveLocalNotification');
          return null;
        });

    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);


    FlutterLocalNotificationsPlugin notification = FlutterLocalNotificationsPlugin();

    await notification.initialize(initializationSettings,
        onSelectNotification: (payload) {
          print('onDidReceiveLocalNotification');
          return null;
        });


    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'id',
        'name',
        'description'
    );

    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics,
        IOSNotificationDetails()
    );
    notification.show(
      0,
      '$title',
      '$message',
      platformChannelSpecifics,
      payload: 'Default_Sound',
    );
  }

The code is not organized, but this is more or less what I did to work.

I hope I've helped

@joshipucher
Copy link

@drikanius your solution made it not crash anymore for me!
But I still don't get any notifications for android when the app is terminated. It only works when the app is still in the background, which already worked before.

@joshipucher
Copy link

joshipucher commented May 9, 2020

Could solve it (at least for android) now with

  1. the solution of @drikanius
  2. The permission_handler package and asking for the "notification" permission
    https://pub.dev/packages/permission_handler

So for me, notifications also work when the app is closed now

@drikanius
Copy link

@joshipucher good to know it's working. I probably forgot to mention the permission part (sorry =/).

@hampsterx
Copy link

hampsterx commented Aug 27, 2020

I concur with @sakina1403 this is an embarassment to Flutter trying to get push notifications working (well onBackgroundMessage specifically)

@drikanius trying to follow your instructions but getting

Unable to instantiate activity ComponentInfo{...Application}: java.lang.ClassCastException: com....Application cannot be cast to android.app.Activity

Update: Nevermind, configured it incorrectly as changed activity.android.name instead of application.name.

Related issues with code snippets: #2196 #2077

@Salakar
Copy link
Member

Salakar commented Nov 4, 2020

Hey all 👋

As part of our roadmap (#2582) we've just shipped a complete rework of the firebase_messaging plugin that aims to solve this and many other issues.

If you can, please try out the dev release (see the migration guide for upgrading and for changes) and if you have any feedback then join in the discussion here.

Given the scope of the rework I'm going to go ahead and close this issue in favor of trying out the latest plugin.

Thanks everyone 🤓

@Salakar Salakar closed this as completed Nov 4, 2020
@firebase firebase locked and limited conversation to collaborators Dec 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: critical Usually a production crash with high user impact. (P1) impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests