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

[Flutter Messaging] Adding onBackgroundMessage causes 'null pointer dereference' #144

Closed
ChrisOwen101 opened this issue Sep 12, 2019 · 8 comments
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) type: bug Something isn't working

Comments

@ChrisOwen101
Copy link

ChrisOwen101 commented Sep 12, 2019

Describe the bug
When I trying to implement onBackgroundMessage in FirebaseMessaging it triggers this error when building for Android (I can't test on iOS):

F/libc    (16924): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 in tid 16971 (Thread-5), pid 16924 (rbon_accounting)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'HONOR/COL-L29/HWCOL:9/HUAWEICOL-L29/326C432:user/release-keys'
Revision: '0'
ABI: 'arm64'
Happend: 'Thu Sep 12 23:52:45 2019
'
SYSVMTYPE: Art
APPVMTYPE: Art
pid: 16924, tid: 16971, name: Thread-5  >>> accounting.carbon.carbon_accounting <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1
Cause: null pointer dereference
    x0  0000000000000001  x1  0000007e68301cc0  x2  0000007e68200000  x3  0000000000000002
    x4  0000000000000101  x5  0000000000000000  x6  0000007e65b47ef0  x7  00000000000034e1
    x8  0000007e7de46380  x9  0000007e7de46380  x10 0000000000000101  x11 0000000000000000
    x12 0000007e7a7e4508  x13 0000000000000302  x14 0000000000000002  x15 0000007e65b47ef0
    x16 0000007f05031bd8  x17 0000007f04fc5d48  x18 0000000000000001  x19 0000007e846d66e0
    x20 0000007e7a773c40  x21 0000007e7de47310  x22 0000007e7de47340  x23 0000000000000041
    x24 0000007e5c258118  x25 0000007e65b48248  x26 0000000000000000  x27 0000000000000001
    x28 0000000000000041  x29 0000007e65b483a0
    sp  0000007e65b480f0  lr  0000007e66e60c58  pc  0000007e66e5c990
backtrace:
    #00 pc 00000000011d3990  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #01 pc 00000000011d7c54  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #02 pc 00000000011cf744  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #03 pc 00000000011d5a78  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #04 pc 00000000011d6154  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #05 pc 00000000011d55b0  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #06 pc 00000000011da7c8  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #07 pc 00000000000142cc  /system/lib64/libutils.so (android::Looper::pollInner(int)+932)
    #08 pc 0000000000013e8c  /system/lib64/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+60)
    #09 pc 0000000000012278  /system/lib64/libandroid.so (ALooper_pollOnce+96)
    #10 pc 00000000011da74c  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #11 pc 00000000011d54fc  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #12 pc 00000000011d86a0  /data/app/accounting.carbon.carbon_accounting-i2IJQytvRGzGKWUq1EQXqw==/lib/arm64/libflutter.so (offset 0x11c0000)
    #13 pc 0000000000083588  /system/lib64/libc.so (__pthread_start(void*)+36)
    #14 pc 00000000000241dc  /system/lib64/libc.so (__start_thread+68)

I have my BackgroundMessageHandler defined in a top level dart file as per the details in the docs.

Configuring the FirebaseMessaging SDK:

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on message $message');
      },
      onResume: (Map<String, dynamic> message) async {
        print('on resume $message');
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on launch $message');
      },
      onBackgroundMessage: myBackgroundMessageHandler,
    );

The handler:

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async { return Future<void>.value(); }

If I remove the line onBackgroundMessage: myBackgroundMessageHandler then the app builds successfully.

Interestingly notifications are being received properly but I can't open the app as it crashes on start up.

Relevant Flutter Doctor:


[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.14.4 18E226, locale en)
    • Flutter version 1.9.1+hotfix.2 at /Users/Chris_Owen/flutter
    • Framework revision 2d2a1ffec9 (6 days ago), 2019-09-06 18:39:49 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/Chris_Owen/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /Users/Chris_Owen/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Android Studio
    • Android Studio at /Applications/Android Studio 3.5 Preview.app/Contents
    • Flutter plugin version 37.0.2
    • Dart plugin version 191.8405
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b01)

[✓] Connected device (1 available)
    • COL L29 • android-arm64 • Android 9 (API 28)

To Reproduce
Covered above

Expected behavior
The app should build successfully

Additional context
Add any other context about the problem here.

@ChrisOwen101 ChrisOwen101 added the type: bug Something isn't working label Sep 12, 2019
@ventskus-roman
Copy link

The same error for me

@gabrielginter
Copy link

same here

@markuspaschi
Copy link

This is my NotificationHandler class. I disable normal notifications completely and only send background messages and populate them on the device. (only tested on android).
Call "initializeFcmNotification" as early as possible in your initState.
You probably have to adjust some values, fill in some dummy data :)

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:coffee_break/common/navigator_data.dart';
import 'package:coffee_break/models/notification.dart';
import 'package:coffee_break/stores/user_store.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    new FlutterLocalNotificationsPlugin();

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
   debugPrint("background: $message");
    //_showNotification(NotificationModel.......(message),
  return Future<void>.value();
}

Future<dynamic> foregroundMessageHandler(Map<String, dynamic> message) async {
   debugPrint("foreground: $message");
    //_showNotification(NotificationModel.......(message),

}

Future onSelectNotification(BuildContext context, String payload) async {
  Map<String, dynamic> payloadMap = json.decode(payload);
    //do something with the data (such as navigator.push...)
}

_showNotification(NotificationModel notification, int id) async {
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
    Platform.isAndroid ? 'de.paschke...' : 'de.paschke...',
    notification.channelName,
    notification.channelDescription,
    playSound: true,
    enableVibration: true,
    importance: Importance.Max,
    priority: Priority.High,
  );
  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
  var platformChannelSpecifics = new NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    id,
    notification.title,
    notification.body,
    platformChannelSpecifics,
    payload: notification.payload,
  );
}

_cancelNotification(int notificationId) async {
  await flutterLocalNotificationsPlugin.cancel(notificationId);
}

class NotificationService {
  FirebaseMessaging _fcm = FirebaseMessaging();
  StreamSubscription iosSubscription;
  static final NotificationService _singleton =
      new NotificationService._internal();

  factory NotificationService() {
    return _singleton;
  }
  NotificationService._internal();

  initializeFcmNotification(BuildContext context, UserStore userStore) async {
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('drawable/app_icon_transparent');
    var initializationSettingsIOS = new IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String message) =>
            onSelectNotification(context, message));

    if (Platform.isIOS) {
      iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
        // save the token  OR subscribe to a topic here
      });

      _fcm.requestNotificationPermissions(IosNotificationSettings());
    } else {
      // _saveDeviceToken();
    }

    _fcm.configure(
      onMessage: (Map<String, dynamic> message) async {
        //foreground
        foregroundMessageHandler(message);
      },
      onBackgroundMessage:
          TargetPlatform.iOS == 'ios' ? null : myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        // debugPrint("message - onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        // debugPrint("message - onResume: $message");
      },
    );

    _fcm.getToken().then((token) {
      Firestore.instance
          .collection('users')
          .document(userStore.firebaseUser.uid)
          .updateData({'pushToken': token});
    }).catchError((err) {
      debugPrint("could not update firebase messaging token in backend");
    });
  }
}

My implementation requires, that you are sending only background messages! If you are sending out a firebase push with the notification payload, it is not working (displayed but the background handler is not being called).

This is a notification with the notification payload, it will never trigger the backgroundNotficationHandler:
DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}, "to": "MYUSERTOKEN"}'

This one has no notification payload and will trigger the backgroundNotificationHandler:
DATA='{"priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}, "to": "MYUSERTOKEN"}'

Sending out with:
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=MYSERVERKEY"

@oysterpack
Copy link

same issue happens for me

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1
Cause: null pointer dereference
    eax 00000001  ebx c966cbbc  ecx c789fb40  edx 00000000
    edi f5282010  esi c49f09b0
    ebp c7afad38  esp c7afad20  eip c8d14722

@oysterpack
Copy link

Looks like this issue is resolved on the Flutter master channel, i.e., switching to the Flutter master channel resolved the issue for me.

When will the fix make it to the stable channel ?

@yxwandroid
Copy link

The same error for me

@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

Hi @ChrisOwen101
I see there's an open issue addressing the case you described.
Please follow up on that issue,
I'm closing the current one as duplicate.
If you disagree please write in the comments
and I will reopen it.
Thank you

@iapicca iapicca closed this as completed Oct 23, 2019
@billymahmood
Copy link

Hi guys,

Any update on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants