Flutterfire: [firebase_messaging] Cannot write file in backgroundMessageHandler

Created on 29 Oct 2019  Β·  5Comments  Β·  Source: FirebaseExtended/flutterfire

Describe the bug
This is on ANDROID only.
Hi. It seems that I can't do File.writeAsBytes() in the _backgroundMessageHandler. But works in _onMessageHandler.

Exception has occurred.
FileSystemException (FileSystemException: Cannot open file, path = 'Directory: '/data/user/0/com.mydomain.myapp/app_flutter'/150' (OS Error: No such file or directory, errno = 2))
import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:dio/dio.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as prefix0;
import 'package:provider/provider.dart';

// .....

Future<dynamic> _backgroundMessageHandler(Map<String, dynamic> message) async {
  Future<String> downloadAndSaveImage(String url) async {
    String decodedImgUrl = Uri.decodeFull(url);
    List<String> arr = decodedImgUrl.split("?");
    String filename = prefix0.basename(arr[0]);
    filename = filename.replaceAll(":", ".");

    var directory = await getApplicationDocumentsDirectory();
    var filePath = '$directory/$filename';
    try {
      var response = await Dio()
          .get(url, options: Options(responseType: ResponseType.bytes));
      var file = File(filePath);
      await file.writeAsBytes(response.data);
      return filePath;
    } on DioError catch (e) {
      log("DioError: " + e.error.toString());
      return "";
    }
  }

  void getImagePath(String url) async {
    String imagePath = await downloadAndSaveImage(url);
    log(imagePath);
  }

  getImagePath("https://via.placeholder.com/150");

}

// .....

Screen Shot 2019-10-29 at 08 21 33

To Reproduce

  1. Close the app (goes to background)
  2. Send push notification
  3. _backgroundMessageHandler handles it

Additional context

  1. I have defined the _backgroundMessageHandler on the top level Dart method
  2. I have added Application.java to my app
customer messaging bug

All 5 comments

Hi @rommyarb
could you please provide your flutter doctor -v
Thank you

Hi @rommyarb
could you please provide your flutter doctor -v
Thank you

[βœ“] Flutter (Channel stable, v1.9.1+hotfix.6, on Mac OS X 10.14.6 18G95, locale en-ID)
    β€’ Flutter version 1.9.1+hotfix.6 at /Users/rommy/development/flutter
    β€’ Framework revision 68587a0916 (8 weeks ago), 2019-09-13 19:46:58 -0700
    β€’ Engine revision b863200c37
    β€’ Dart version 2.5.0

[βœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    β€’ Android SDK at /Users/rommy/Library/Android/sdk
    β€’ Android NDK location not configured (optional; useful for native profiling support)
    β€’ Platform android-29, build-tools 29.0.2
    β€’ ANDROID_HOME = /Users/rommy/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.

[βœ“] Xcode - develop for iOS and macOS (Xcode 10.3)
    β€’ Xcode at /Applications/Xcode.app/Contents/Developer
    β€’ Xcode 10.3, Build version 10G8
    β€’ CocoaPods version 1.7.5

[βœ“] Android Studio (version 3.5)
    β€’ Android Studio at /Applications/Android Studio.app/Contents
    β€’ Flutter plugin version 40.2.2
    β€’ Dart plugin version 191.8593
    β€’ Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[βœ“] VS Code (version 1.39.2)
    β€’ VS Code at /Applications/Visual Studio Code.app/Contents
    β€’ Flutter extension version 3.6.0

[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.

Thank you for your response!

Thank you @rommyarb
issue possibly related to https://github.com/FirebaseExtended/flutterfire/issues/1325

issue possibly related to #1325

@iapicca this is #1325, typo? ☺️

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.

Was this page helpful?
0 / 5 - 0 ratings