質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Q&A

0回答

2609閲覧

flutterのプッシュ通知(fcm)で、releaseビルドの時だけ受信時にエラーになる

nagi2011

総合スコア17

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

0グッド

0クリップ

投稿2021/07/19 01:21

flutterで、fcmを利用したプッシュ通知を実装しようとしています。
debugビルドの時はうまくプッシュ通知を受信できるのですが、
releaseビルドの時に、プッシュ通知を受信した瞬間にエラーとなって、アプリが落ちてしまいます。

dart

1import 'dart:async'; 2import 'package:firebase_core/firebase_core.dart'; 3import 'package:firebase_messaging/firebase_messaging.dart'; 4import 'package:flutter/material.dart'; 5import 'package:flutter_local_notifications/flutter_local_notifications.dart'; 6 7Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { 8 // If you're going to use other Firebase services in the background, such as Firestore, 9 // make sure you call `initializeApp` before using other Firebase services. 10 await Firebase.initializeApp(); 11 print('Handling a background message ${message.messageId}'); 12} 13 14const AndroidNotificationChannel channel = AndroidNotificationChannel( 15 'high_importance_channel', // id 16 'High Importance Notifications', // title 17 'This channel is used for important notifications.', // description 18 importance: Importance.high, 19); 20 21final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = 22 FlutterLocalNotificationsPlugin(); 23 24Future<void> firebaseInit() async { 25 print("firebaseInit"); 26 27 WidgetsFlutterBinding.ensureInitialized(); 28 await Firebase.initializeApp(); 29 30 // Set the background messaging handler early on, 31 // as a named top-level function 32 FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); 33 34 /// Create an Android Notification Channel. 35 /// 36 /// We use this channel in the `AndroidManifest.xml` file to override the 37 /// default FCM channel to enable heads up notifications. 38 await flutterLocalNotificationsPlugin 39 .resolvePlatformSpecificImplementation< 40 AndroidFlutterLocalNotificationsPlugin>() 41 ?.createNotificationChannel(channel); 42 43 /// Update the iOS foreground notification presentation options to allow 44 /// heads up notifications. 45 await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( 46 alert: true, 47 badge: true, 48 sound: true, 49 ); 50 51 FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) { 52 //print("message : " + message.toString()); 53 }); 54 55 FirebaseMessaging.onMessage.listen((RemoteMessage message) { 56 final notification = message.notification; 57 final android = message.notification?.android; 58 //print("notification : " + notification.toString()); 59 60 if (notification != null && android != null) { 61 flutterLocalNotificationsPlugin.show( 62 notification.hashCode, 63 notification.title, 64 notification.body, 65 NotificationDetails( 66 android: AndroidNotificationDetails( 67 channel.id, 68 channel.name, 69 channel.description, 70 // ignore: flutter_style_todos 71 // TODO add a proper drawable resource to android, for now using 72 // one that already exists in example app. 73 icon: 'launch_background', 74 ), 75 )); 76 } 77 }); 78 79 //runApp(Permissions()); 80 /* 81 NotificationSettings settings = 82 await FirebaseMessaging.instance.requestPermission( 83 announcement: true, 84 carPlay: true, 85 criticalAlert: true, 86 ); 87 88 */ 89} 90

flavorで、devとprodで分けていますが、
devもprodも、debugモードのみプッシュ通知を正常に受信でき、releaseモードの時にエラーとなってしまいます。

def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 30 sourceSets { main.java.srcDirs += 'src/main/kotlin' } lintOptions { disable 'InvalidPackage' checkReleaseBuilds false // issue https://github.com/flutter/flutter/issues/22397 } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.test" minSdkVersion 23 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } // SigningConfigs apply from: 'signingConfigs/debug.gradle', to: android apply from: 'signingConfigs/release.gradle', to: android buildTypes { release { signingConfig signingConfigs.release } debug { debuggable true signingConfig signingConfigs.debug } } flavorDimensions "app" productFlavors { dev { dimension "app" resValue "string", "app_name", "DEV_test-app" applicationIdSuffix ".dev" } prod { dimension "app" } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "com.google.firebase:firebase-messaging:21.0.1" }

実機デバックすれば原因がわかりそうなものなのですが、
実機デバックの際はdebugモードになってしまい(releaseモードにはできない?)、
正常に成功してしまいます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問