-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 local notification initialization errors in background calls #759
Comments
I pushed a branch up that should fix the |
I can confirm that your |
Thanks, I'll will merge this in and release it shortly |
Thank you for the swift response ... I will check the solution and will let you know the response of the app |
When i receive the notification from background, getting error on console. Flutter 1.20.2 • channel stable • https://github.com/flutter/flutter.git E/MethodChannel#dexterous.com/flutter/local_notifications: Failed to handle method call |
@saravananmnm make sure you're on the latest version |
@MaikuB Yes, i have a latest one. [√] Flutter (Channel stable, 1.20.2, on Microsoft Windows [Version 10.0.18362.1016], locale en-IN) |
@saravananmnm I'd suggest running If you're still running into a problem then you'll need to provide complete, minimal example app to reproduce the problem. This could be either a link to a repository or fork the repo and update the example app to demonstrate the problem. |
I am trying to create an app that fires a simple notification through a background service dispatched by workmanager plugin. The idea is to check for any messages in mqtt instance after every 15 minutes and display a simple local notification in case of the availability of any message on the broker. The notification is called at the initialization of the application too... The main issue is that no matter how many times I terminate the app and restart it the notification works FINE but as soon as I try to dispatch a background service , the service works but the local notification plugin FAILS to initialize.. The code and the logs for the following are as follows.
CODE-
import 'dart:async';
import 'package:workmanager/workmanager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import './mqttClientWrapper.dart';
const mqtt_task_key = "MqttTask";
Future callbackDispatcher() async{
Workmanager.executeTask((taskName, inputData) async{
if(taskName == mqtt_task_key){
print("running instance in background");
print("instantiating");
await RandomState().setup();
}
return Future.value(true);
});
}
class Random extends StatefulWidget {
@OverRide
RandomState createState() => RandomState();
}
class RandomState extends State {
MQTTClientWrapper mqttClientWrapper;
String message = "";
Future setup() async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
mqttClientWrapper = MQTTClientWrapper(() => {},(newMessage) => gotNewMessage(newMessage, flutterLocalNotificationsPlugin));
print('1');
mqttClientWrapper.prepareMqttClient();
print('2');
}
Future gotNewMessage(String newMessage, flutterLocalNotificationsPlugin) async{
setState((){
this.message = newMessage;
});
await _showNotificationWithDefaultSound(flutterLocalNotificationsPlugin);
print('test');
}
@OverRide
void initState() {
super.initState();
setup();
Workmanager.cancelAll();
}
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('random'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
message,
style: Theme.of(context).textTheme.headline4,
),
FlatButton(
color: Colors.blue,
onPressed: (){
Workmanager.initialize(
callbackDispatcher,
isInDebugMode: true,
);
print("initialized");
Workmanager.registerPeriodicTask('1', mqtt_task_key, frequency: Duration(minutes: 15), initialDelay: Duration(seconds: 30));
},
)
],
),
),
);
}
Future onSelectNotification(String payload) async {
showDialog(
context: context,
builder: (_) {
return new AlertDialog(
title: Text("PayLoad"),
content: Text("Payload : $payload"),
);
},
);
}
Future _showNotificationWithDefaultSound(flutterLocalNotificationsPlugin) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'New Post',
'New Message',
platformChannelSpecifics,
payload: 'Default_Sound',
);
}
}
Logs from the console
I/flutter (16424): 1
I/flutter (16424): MQTTClientWrapper::test client connecting....
I/flutter (16424): 2020-08-19 04:01:10.452967 -- Authenticating with username '{wbynzcri}' and password '{uOIqIxMgf3Dl}'
I/flutter (16424): 2020-08-19 04:01:10.489029 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (16424): 2020-08-19 04:01:10.489300 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (16424): 2020-08-19 04:01:10.489672 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (16424): 2
I/flutter (16424): 3
I/flutter (16424): 2020-08-19 04:01:11.724721 -- MqttServerConnection::_startListening
I/flutter (16424): 2020-08-19 04:01:11.742755 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (16424): 2020-08-19 04:01:11.743072 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (16424): 2020-08-19 04:01:11.748088 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (16424): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (16424): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=false, WillQos=MqttQos.atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (16424): MqttConnectPayload - client identifier is : #
I/flutter (16424): 2020-08-19 04:01:11.780464 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
I/flutter (16424): 2020-08-19 04:01:12.118299 -- MqttConnection::_onData
I/flutter (16424): 2020-08-19 04:01:12.137902 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck
I/flutter (16424): Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2
I/flutter (16424): Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.connectionAccepted}
I/flutter (16424): 2020-08-19 04:01:12.144405 -- MqttServerConnection::_onData - message processed
I/flutter (16424): 2020-08-19 04:01:12.153670 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor
I/flutter (16424): 2020-08-19 04:01:12.154185 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor - state = connected
I/flutter (16424): MQTTClientWrapper::OnConnected client callback - Client connection was sucessful
I/flutter (16424): 2020-08-19 04:01:12.156048 -- SynchronousMqttServerConnectionHandler:: cancelling connect timer
I/flutter (16424): 2020-08-19 04:01:12.158088 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code of connectionAccepted and a disconnection origin of none
I/flutter (16424): 2020-08-19 04:01:12.158600 -- SynchronousMqttServerConnectionHandler::internalConnect exited with state Connection status is connected with return code of connectionAccepted and a disconnection origin of none
I/flutter (16424): MQTTClientWrapper::Mosquitto client connected
I/flutter (16424): MQTTClientWrapper::Subscribing to the 4060431/# topic
I/flutter (16424): 2020-08-19 04:01:12.177163 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.subscribe
I/flutter (16424): Header: MessageType = MqttMessageType.subscribe, Duplicate = false, Retain = false, Qos = MqttQos.atLeastOnce, Size = 0
I/flutter (16424): Subscribe Variable Header: MessageIdentifier={1}
I/flutter (16424): Payload: Subscription [{1}]
I/flutter (16424): {{ Topic={4060431/#}, Qos={MqttQos.atMostOnce} }}
I/flutter (16424):
I/flutter (16424): 2020-08-19 04:01:12.492990 -- MqttConnection::_onData
I/flutter (16424): 2020-08-19 04:01:12.500615 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.subscribeAck
I/flutter (16424): Header: MessageType = MqttMessageType.subscribeAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 3
I/flutter (16424): SubscribeAck Variable Header: MessageIdentifier={1}
I/flutter (16424): Payload: Qos grants [{1}]
I/flutter (16424): {{ Grant={MqttQos.atMostOnce} }}
I/flutter (16424):
I/flutter (16424): 2020-08-19 04:01:12.501061 -- MqttServerConnection::_onData - message processed
I/flutter (16424): MQTTClientWrapper::Subscription confirmed for topic 4060431/#
I/flutter (16424): 2020-08-19 04:01:12.897098 -- MqttConnection::_onData
I/flutter (16424): 2020-08-19 04:01:12.920910 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.publish
I/flutter (16424): Header: MessageType = MqttMessageType.publish, Duplicate = false, Retain = true, Qos = MqttQos.atMostOnce, Size = 19
I/flutter (16424): Publish Variable Header: TopicName={4060431/offline/}, MessageIdentifier={0}, VH Length={18}
I/flutter (16424): Payload: {1 bytes={<49>
I/flutter (16424): 2020-08-19 04:01:12.921398 -- MqttServerConnection::_onData - message processed
I/flutter (16424): 2020-08-19 04:01:12.923384 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.publish
I/flutter (16424): Header: MessageType = MqttMessageType.publish, Duplicate = false, Retain = true, Qos = MqttQos.atMostOnce, Size = 45
I/flutter (16424): Publish Variable Header: TopicName={4060431/information/codename/}, MessageIdentifier={0}, VH Length={31}
I/flutter (16424): Payload: {14 bytes={<83><77><82><84><65><76><82><84><49><66><50><76><49><71>
I/flutter (16424): 2020-08-19 04:01:12.924024 -- MqttServerConnection::_onData - message processed
I/flutter (16424): 2020-08-19 04:01:12.926510 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.publish
I/flutter (16424): Header: MessageType = MqttMessageType.publish, Duplicate = false, Retain = true, Qos = MqttQos.atMostOnce, Size = 33
I/flutter (16424): Publish Variable Header: TopicName={4060431/information/version/}, MessageIdentifier={0}, VH Length={30}
I/flutter (16424): Payload: {3 bytes={<48><46><49>
I/flutter (16424): 2020-08-19 04:01:12.926893 -- MqttServerConnection::_onData - message processed
I/flutter (16424): 2020-08-19 04:01:12.928255 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.publish
I/flutter (16424): Header: MessageType = MqttMessageType.publish, Duplicate = false, Retain = true, Qos = MqttQos.atMostOnce, Size = 27
I/flutter (16424): Publish Variable Header: TopicName={4060431/sensor/0/type/}, MessageIdentifier={0}, VH Length={24}
I/flutter (16424): Payload: {3 bytes={<71><65><83>
I/flutter (16424): 2020-08-19 04:01:12.928666 -- MqttServerConnection::_onData - message processed
I/flutter (16424): 2020-08-19 04:01:12.929900 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.publish
I/flutter (16424): Header: MessageType = MqttMessageType.publish, Duplicate = false, Retain = true, Qos = MqttQos.atMostOnce, Size = 26
I/flutter (16424): Publish Variable Header: TopicName={4060431/sensor/0/state/}, MessageIdentifier={0}, VH Length={25}
I/flutter (16424): Payload: {1 bytes={<49>
I/flutter (16424): 2020-08-19 04:01:12.930394 -- MqttServerConnection::_onData - message processed
I/flutter (16424): MQTTClientWrapper::GOT A NEW MESSAGE 1
I/flutter (16424): MQTTClientWrapper::GOT A NEW MESSAGE SMRTALRT1B2L1G
I/flutter (16424): MQTTClientWrapper::GOT A NEW MESSAGE 0.1
I/flutter (16424): MQTTClientWrapper::GOT A NEW MESSAGE GAS
I/flutter (16424): MQTTClientWrapper::GOT A NEW MESSAGE 1
I/flutter (16424): test
I/flutter (16424): test
I/flutter (16424): test
I/flutter (16424): test
I/flutter (16424): test
I/art (16424): Do partial code cache collection, code=21KB, data=30KB
I/art (16424): After code cache collection, code=21KB, data=30KB
I/art (16424): Increasing code cache capacity to 128KB
I/flutter (16424): initialized
I/flutter (16424): 2020-08-19 04:01:30.486502 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.pingRequest
I/flutter (16424): Header: MessageType = MqttMessageType.pingRequest, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (16424): 2020-08-19 04:01:30.807658 -- MqttConnection::_onData
I/flutter (16424): 2020-08-19 04:01:30.811320 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.pingResponse
I/flutter (16424): Header: MessageType = MqttMessageType.pingResponse, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (16424): 2020-08-19 04:01:30.812317 -- MqttServerConnection::_onData - message processed
I/flutter (16424): 2020-08-19 04:01:50.502517 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.pingRequest
I/flutter (16424): Header: MessageType = MqttMessageType.pingRequest, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (16424): 2020-08-19 04:01:50.833619 -- MqttConnection::_onData
I/flutter (16424): 2020-08-19 04:01:50.834847 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.pingResponse
I/flutter (16424): Header: MessageType = MqttMessageType.pingResponse, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (16424): 2020-08-19 04:01:50.835866 -- MqttServerConnection::_onData - message processed
I/flutter (16424): running instance in background
I/flutter (16424): instantiating
I/flutter (16424): 1
I/flutter (16424): MQTTClientWrapper::test client connecting....
I/flutter (16424): 2020-08-19 04:01:53.864177 -- Authenticating with username '{wbynzcri}' and password '{uOIqIxMgf3Dl}'
I/flutter (16424): 2020-08-19 04:01:53.918924 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (16424): 2020-08-19 04:01:53.919223 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (16424): 2020-08-19 04:01:53.919650 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (16424): 2
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.app.Activity.getIntent()' on a null object reference
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.initialize(FlutterLocalNotificationsPlugin.java:856)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:742)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:226)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:631)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at android.os.Looper.loop(Looper.java:136)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at android.app.ActivityThread.main(ActivityThread.java:6165)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
E/MethodChannel#dexterous.com/flutter/local_notifications(16424): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
I/WM-WorkerWrapper(16424): Worker result FAILURE for Work [ id=9519d0f6-1acd-445d-a325-8281ae5dd445, tags={ be.tramckrijte.workmanager.BackgroundWorker } ]
Note that the initialized is printed on the console once the background task is fired ... and there is a delay of 30 seconds between the actual execution of the task and the command that is why you will see mqtt instance messages in between ...
The main issue starts form method call.
Please urgent help is required on the matter
Thank You
The text was updated successfully, but these errors were encountered: