-
Notifications
You must be signed in to change notification settings - Fork 4k
🐛 [firebase_messaging] No longer receiving data messages in FOREGROUND on iOS #3735
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
Comments
Hi @rubenvereecken flutter doctor -v[✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.7 19H2, locale en-GB)
• Flutter version 1.22.0 at /Users/tahatesser/Code/flutter_stable
• Framework revision d408d302e2 (3 days ago), 2020-09-29 11:49:17 -0700
• Engine revision 5babba6c4d
• Dart version 2.10.0
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Users/tahatesser/Code/sdk
• Platform android-30, build-tools 30.0.2
• ANDROID_HOME = /Users/tahatesser/Code/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.0.1, Build version 12A7300
• CocoaPods version 1.9.3
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 50.0.1
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.49.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.15.0
[✓] Connected device (1 available)
• Taha’s iPhone (mobile) • 00008020-001059882212002E • ios • iOS 14.0.1
• No issues found! code sample// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
// Or do other work.
}
final Map<String, Item> _items = <String, Item>{};
Item _itemForMessage(Map<String, dynamic> message) {
final dynamic data = message['data'] ?? message;
final String itemId = data['id'];
final Item item = _items.putIfAbsent(itemId, () => Item(itemId: itemId))
..status = data['status'];
return item;
}
class Item {
Item({this.itemId});
final String itemId;
StreamController<Item> _controller = StreamController<Item>.broadcast();
Stream<Item> get onChanged => _controller.stream;
String _status;
String get status => _status;
set status(String value) {
_status = value;
_controller.add(this);
}
static final Map<String, Route<void>> routes = <String, Route<void>>{};
Route<void> get route {
final String routeName = '/detail/$itemId';
return routes.putIfAbsent(
routeName,
() => MaterialPageRoute<void>(
settings: RouteSettings(name: routeName),
builder: (BuildContext context) => DetailPage(itemId),
),
);
}
}
class DetailPage extends StatefulWidget {
DetailPage(this.itemId);
final String itemId;
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
Item _item;
StreamSubscription<Item> _subscription;
@override
void initState() {
super.initState();
_item = _items[widget.itemId];
_subscription = _item.onChanged.listen((Item item) {
if (!mounted) {
_subscription.cancel();
} else {
setState(() {
_item = item;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Item ${_item.itemId}"),
),
body: Material(
child: Center(child: Text("Item status: ${_item.status}")),
),
);
}
}
class PushMessagingExample extends StatefulWidget {
@override
_PushMessagingExampleState createState() => _PushMessagingExampleState();
}
class _PushMessagingExampleState extends State<PushMessagingExample> {
String _homeScreenText = "Waiting for token...";
bool _topicButtonsDisabled = false;
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
final TextEditingController _topicController =
TextEditingController(text: 'topic');
Widget _buildDialog(BuildContext context, Item item) {
return AlertDialog(
content: Text("Item ${item.itemId} has been updated"),
actions: <Widget>[
FlatButton(
child: const Text('CLOSE'),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: const Text('SHOW'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
}
void _showItemDialog(Map<String, dynamic> message) {
showDialog<bool>(
context: context,
builder: (_) => _buildDialog(context, _itemForMessage(message)),
).then((bool shouldNavigate) {
if (shouldNavigate == true) {
_navigateToItemDetail(message);
}
});
}
void _navigateToItemDetail(Map<String, dynamic> message) {
final Item item = _itemForMessage(message);
// Clear away dialogs
Navigator.popUntil(context, (Route<dynamic> route) => route is PageRoute);
if (!item.route.isCurrent) {
Navigator.push(context, item.route);
}
}
@override
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_showItemDialog(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
setState(() {
_homeScreenText = "Push Messaging token: $token";
});
print(_homeScreenText);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Push Messaging Demo'),
),
// For testing -- simulate a message being received
floatingActionButton: FloatingActionButton(
onPressed: () => _showItemDialog(<String, dynamic>{
"data": <String, String>{
"id": "2",
"status": "out of stock",
},
}),
tooltip: 'Simulate Message',
child: const Icon(Icons.message),
),
body: Material(
child: Column(
children: <Widget>[
Center(
child: Text(_homeScreenText),
),
Row(children: <Widget>[
Expanded(
child: TextField(
controller: _topicController,
onChanged: (String v) {
setState(() {
_topicButtonsDisabled = v.isEmpty;
});
}),
),
FlatButton(
child: const Text("subscribe"),
onPressed: _topicButtonsDisabled
? null
: () {
_firebaseMessaging
.subscribeToTopic(_topicController.text);
_clearTopicText();
},
),
FlatButton(
child: const Text("unsubscribe"),
onPressed: _topicButtonsDisabled
? null
: () {
_firebaseMessaging
.unsubscribeFromTopic(_topicController.text);
_clearTopicText();
},
),
])
],
),
));
}
void _clearTopicText() {
setState(() {
_topicController.text = "";
_topicButtonsDisabled = true;
});
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseApp app = await Firebase.initializeApp();
assert(app != null);
runApp(
MaterialApp(
home: PushMessagingExample(),
),
);
}
flutter doctor -v[✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.7 19H2, locale en-GB)
• Flutter version 1.22.0 at /Users/tahatesser/Code/flutter_stable
• Framework revision d408d302e2 (3 days ago), 2020-09-29 11:49:17 -0700
• Engine revision 5babba6c4d
• Dart version 2.10.0
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Users/tahatesser/Code/sdk
• Platform android-30, build-tools 30.0.2
• ANDROID_HOME = /Users/tahatesser/Code/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.0.1, Build version 12A7300
• CocoaPods version 1.9.3
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 50.0.1
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.49.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.15.0
[✓] Connected device (1 available)
• Taha’s iPhone (mobile) • 00008020-001059882212002E • ios • iOS 14.0.1
• No issues found! |
@TahaTesser Thanks for getting back to me and sorry for taking so long to reply. I wanted to dedicate some proper time to this to properly replicate your example. I've now managed and my problem remains - your example doesn't work for me. I do observe the same I did before: the message enters the device, I get some odd message, but it doesn't reach the message handler. The message I get is the following.
I copied your example ad verbatim. I sent messages using the following curl command which was confirmed to work with display messages but not data messages. curl -X POST -H "Authorization:Bearer <access_token>" -H "Content-Type: application/json" -d '{
"message":{
"token":"<token I grabbed from the device's logs>",
"data":{
"id":"3",
"status":"This be a test"
}
}
}' https://fcm.googleapis.com/v1/projects/<firebase-project>/messages:send My worry is that I set up something else incorrectly but I can't for the life of me think of what else has changed because this definitely used to work. Something I feel insecure about for example is |
Hi @rubenvereecken
|
@TahaTesser That is a display message because it has a notification property and as I said those work fine. This problem is about data messages. Please look at the error message I reported above (I'll replicate it here)
|
I'm sure the warning message contains some useful indication but I'm not experienced enough with iOS to interpret it. |
Hi @rubenvereecken |
I now understand this appears to be a duplicate of #3395 which is still open. |
hI @rubenvereecken |
As per the title, yep. |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Describe the bug
The
onMessage
handler is no longer called for data messages on iOS in the foreground. It is called for data messages on Android in the foreground, and it is called for display messages on iOS in the foreground.Steps to reproduce
Steps to reproduce the behavior:
onMessage
handler in the appExpected behavior
I would expect the
onMessage
handler to be called for data messages in the foreground. This is supposed to be the simple scenario. This used to work.Additional context
I have tried different data messages to see if my format is the culprit, to see if something's changed. To no avail (and note that they still work fine for Android).
Here is what I would normally send (no longer works - just iOS in foreground though):
(EDIT) I just saw this pop up in XCode whenever I would send a data message and I could not figure out how it's related but I suppose it must be.
I figured this was related to these lines in
AppDelegate.swift
.Yet when I removed these, I no longer received display messages (notifications) in the foreground either. So those lines are working.. but only for display messages. Something is holding back my data messages.
I have also tried to add
content_available
toaps
but no luck.The fact that it's worked before and I do still get display messages (notifications) in the
onMessage
handler with the app open in the foreground makes me think it can't be a configuration issue on my part.Flutter doctor
Run
flutter doctor
and paste the output below:Click To Expand
Flutter dependencies
Run
flutter pub deps -- --style=compact
and paste the output below:Click To Expand
The text was updated successfully, but these errors were encountered: