I wanted to try out cloud messaging in my Flutter application, but I always run into errors, and I have not found a solution yet. I followed the steps stated here: firebase_messaging
If I follow the steps except the optional part with creating an Application.java file for background messages, and I send a test message from Firebase to the launched application, I get this error:
java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3577)
at android.app.ActivityThread.access$1400(ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
E/AndroidRuntime(29468): Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java:77)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)
For me it seems like that the optional part is needed too, but when I do that I get errors when the app opens. After the errors the app still runs, and if I send a test message from Firebase the app receives it succesfully. Later it doesn't even receives the message, but thows the same errors.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference
at com.github.adee42.keyboardvisibility.KeyboardVisibilityPlugin.registerWith(KeyboardVisibilityPlugin.java:107)
at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:22)
at io.flutter.plugins.Application.registerWith(Application.java:18)
at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:164)
at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:133)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference, null)
StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
<asynchronous suspension>
FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:118:16)
main (package:vonatles/main.dart:18:22)
_AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
main (package:vonatles/main.dart:12:10)
_runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:229:25)
_rootRun (dart:async/zone.dart:1124:13)
_CustomZone.run (dart:async/zone.dart:1021:19)
_runZoned (dart:async/zone.dart:1516:10)
runZoned (dart:async/zone.dart:1500:12)
_runMainZoned.<anonymous closure> (dart:ui/hooks.dart:221:5)
_startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:305:19)
_RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
Versions:
firebase_core: ^0.4.0+1
firebase_database: 3.0.7
firebase_messaging: 5.1.5
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0"
classpath 'com.google.gms:google-services:4.3.0'
In the Flutter main function I have this:
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
The Application.java code:
package io.flutter.plugins;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
#Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
#Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
My android main directory:
-main
-java
-io.flutter.plugins
Apllication.java
GeneratedPluginRegistrant.java
-kotlin
-my.package.name
MainActivity.kt
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package.name">
<application
android:name="io.flutter.plugins.Application"
android:label="mylabel"
android:icon="#mipmap/ic_launcher">
<activity
android:name=".MainActivity"
...
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Since my MainActivity in android was a Kotlin class, I got the solution to this problem when I tried the Application class as a Kotlin class instead of trying it as a Java class.
I tried many solutions, still, I was not able to run the app.
Application.kt
package YOUR_PACKAGE_NAME
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
public class Application: FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
}
override fun registerWith(registry: PluginRegistry) {
FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
}
}
Also, FirebaseCloudMessagingPluginRegistrant.kt
package YOUR_PACKAGE_NAME
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
class FirebaseCloudMessagingPluginRegistrant {
companion object {
fun registerWith(registry: PluginRegistry) {
if (alreadyRegisteredWith(registry)) {
return;
}
FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
}
fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
val key = FirebaseCloudMessagingPluginRegistrant::class.java.name
if (registry.hasPlugin(key)) {
return true
}
registry.registrarFor(key)
return false
}
}
}
This can be added in the sample directory where your MainActivity is.
Try this Along with the steps mentioned here: Firebase messaging
by the way you can create a notification_handler.dart like this Unhandled Exception: NoSuchMethodError: The method 'toRawHandle' was called on null.
i have posted my notification_handler.dart their.
try to configure with that and just call is as below from your statefullWidget and the let me know.
#override
void initState() {
super.initState();
new NotificationHandler().initializeFcmNotification();
}
Background notification setup is not optional anymore. Make sure to follow intructions properly. And i was confused in making change in AndroidManifest.xml, So i wanted to share clear instruction.
PluginRegistrantCallback is not set" means Application.java file is not called properly at AndroidManifest.xml. Make change as follows:
First it looks like this
Change it to yourpackagename.Application like this
If you get "Default activity not found" error, run "flutter clean" command in your project folder and run it again.
In your AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package.name">
<application
android:name=".Application" //edit this line
android:label="mylabel"
android:icon="#mipmap/ic_launcher">
<activity
android:name=".MainActivity"
...
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Related
I'm wanting to automatically run my flutter application's background services as soon as the mobile is rebooted.
I implemented the following Solution, but it's throwing some errors
AndroidStudioProjects\db_app\android\app\src\main\kotlin\com\example\laravel_login\MainActivity.kt: (14, 22): Unresolved reference: Uri
AndroidStudioProjects\db_app\android\app\src\main\kotlin\com\example\laravel_login\MainActivity.kt: (14, 28): Unresolved reference: Uri
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
If you know any alternative, please help.
Thank you.
MainActivity.kt
package com.example.laravel_login
import android.os.Bundle
import android.provider.Settings
import io.flutter.embedding.android.FlutterActivity
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var REQUEST_OVERLAY_PERMISSIONS = 100
if (!Settings.canDrawOverlays(getApplicationContext())) {
val myIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
val uri: Uri = Uri.fromParts("com.example.laravel_login.BootReceiver", getPackageName(), null)
myIntent.setData(uri)
startActivityForResult(myIntent, REQUEST_OVERLAY_PERMISSIONS)
return
}
}
}
class BootReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val i = Intent(context, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i)
}
}
}
AndroidManifest.xml
<!-- app auto restart on phone restart -->
<receiver
android:enabled="true"
android:exported="true"
android:name="com.example.laravel_login.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!-- app auto restart on phone restart -->
Add this to the imports of MainActivity.kt and you're good to go.
import android.net.Uri
I have a simple dart class as follows:
import 'package:flutter/material.dart';
class UiUtils {
// TEMPORARY FOR UNIT TEST PURPOSES ONLY
int addition(int x, int y) {
return x + y;
}
}
(Note: The above is a sample, the actual class does have more than that temp function.)
My pubspec.yml file contains the following:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
get_it: ^7.1.3
provider: ^6.0.1
mockito: ^5.0.16
I have a dependency locator file as such:
import 'package:get_it/get_it.dart';
import 'package:quiz_test/utils/UiUtils.dart';
GetIt dependencyLocator = GetIt.instance;
void setupDependencyLocator() {
//dependencyLocator.registerSingleton(() => UiUtils());
dependencyLocator.registerFactory(() => UiUtils());
}
Finally, in main.dart I have the following:
void main() {
setupDependencyLocator();
runApp(MyApp());
}
(There is of course more code than this).
As it is displayed, the code works fine, however if I change the dependancy_locator file from the current factory method to the singleton instead (i.e. comment out one to enable the other) I get the following error:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type '_ServiceFactory<() => UiUtils, void, void>' is not a subtype of type '_ServiceFactory<Object, dynamic, dynamic>' of 'value'
#0 _LinkedHashMapMixin.[]= (dart:collection-patch/compact_hash.dart)
#1 _GetItImplementation._register (package:get_it/get_it_impl.dart:844:35)
#2 _GetItImplementation.registerSingleton (package:get_it/get_it_impl.dart:587:5)
#3 setupDependencyLocator (package:quiz_test/utils/dependency_locator.dart:7:21)
#4 main (package:quiz_test/main.dart:13:3)
#5 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:145:25)
#6 _rootRun (dart:async/zone.dart:1428:13)
#7 _CustomZone.run (dart:async/zone.dart:1328:19)
#8 _runZoned (dart:async/zone.dart:1863:10)
#9 runZonedGuarded (dart:async/zone.dart:1851:12)
#10 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:141:5)
#11 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.<…>
Can anyone please help me to understand why I cannot use the singleton call rather than the factory one? My thought process is that I do not need a unique instance of this class, which is what I believe factory will give me, I just need a single instance of it for any classes that require it.
Any help is greatly appreciated.
I was able to resolve this by doing the following:
dependencyLocator.registerSingleton<UiUtils>(UiUtils());
So, my dependencyLocator class now looks like this:
import 'package:get_it/get_it.dart';
import 'package:quiz_test/utils/UiUtils.dart';
GetIt dependencyLocator = GetIt.instance;
void setupDependencyLocator() {
dependencyLocator.registerSingleton<UiUtils>(UiUtils());
}
I hope this helps someone else from getting stuck!
I'm here new, I'm learning flutter and I need login with this:
POST http://localhost:3000/api/users/session
But I still can't login :( I'm waiting 5 minutes and still there is a loading screen with no effects.
I have try
So I have have made a this api_service with this tutorial made https://www.youtube.com/watch?v=_Kw4BfNX1-4
here is my api_service.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../model/login_model.dart';
class APIService {
Future<LoginResponseModel>
login(LoginRequestModel requestModel) async {
String url = "http://localhost:3000/api/users/session";
final response = await http.post(url, body: requestModel.toJson());
if (response.statusCode == 200 || response.statusCode == 400) {
return LoginResponseModel.fromJson(
json.decode(response.body),
);
} else {
throw Exception('Failed to load data!');
}
}
}
Here is debug console:
E/flutter (10967): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)]
Unhandled Exception: SocketException: OS Error: Connection refused,
errno = 111, address = localhost, port = 58856 E/flutter (10967):
I'm try with this page:
https://medium.com/#podcoder/connecting-flutter-application-to-localhost-a1022df63130
I change String url = "http://localhost:3000/api/users/session"; to
String url = "http://10.0.2.2:3000/api/users/session";
in my debug console i have completely different messages, but stilll
don't work :(
E/flutter (10967):
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)]
Unhandled Exception: Bad state: Insecure HTTP is not allowed by
platform: http://10.0.2.2:3000/api/users/session
E/flutter (10967): #0 _HttpClient._openUrl
(dart:_http/http_impl.dart:2434:7)
E/flutter (10967): #1 _HttpClient.openUrl
(dart:_http/http_impl.dart:2341:7)
E/flutter (10967): #2 IOClient.send
package:http/src/io_client.dart:31
(...) to #28 and the last ist E/flutter (10967):
Fix for the Error Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform
Flutter terms http as an insecure source Therefore you should either use https or set android:usesCleartextTraffic to true in AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="io.flutter.app.FlutterApplication"
android:label="receipt"
android:usesCleartextTraffic="true" // Add this line
android:icon="#mipmap/ic_launcher">
I have a basic flutter project running on android where when the application starts, I write an executable bundled in my assets.
static String appInternalPath = '/data/data/com.maksimdan.face_merger';
void writeExecutable() async {
var executablePath = join(appInternalPath, 'main');
if (await File(executablePath).exists()) {
File(executablePath).delete();
print('deleted old executable');
} else {
print('not executable exists');
}
ByteData data = await rootBundle.load('lib/py/dist/main');
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(executablePath).writeAsBytes(bytes);
print('wrote new executable');
}
Sometime later in my code I try to run it.
void invokeExecutable() async {
String executablePath = join(appInternalPath, 'main');
Process.run('chmod', ['u+x', executablePath]).then((ProcessResult results) {
Process.run(executablePath, []).then((ProcessResult results) {
print(results.stdout);
});
});
}
But obtain a permission denied error.
E/flutter (31825): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: ProcessException: Permission denied
E/flutter (31825): Command: /data/data/com.maksimdan.flutter_general/main
E/flutter (31825): #0 _ProcessImpl._start (dart:io-patch/process_patch.dart:390:33)
E/flutter (31825): #1 Process.start (dart:io-patch/process_patch.dart:36:20)
E/flutter (31825): #2 _runNonInteractiveProcess (dart:io-patch/process_patch.dart:565:18)
E/flutter (31825): #3 Process.run (dart:io-patch/process_patch.dart:47:12)
E/flutter (31825): #4 _MyHomePageState.invokeExecutable.<anonymous closure> (package:flutter_general/main.dart:51:15)
E/flutter (31825): #5 _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (31825): #6 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (31825): <asynchronous suspension>
I've also tried:
Process.run('/system/bin/chmod', ['744', path]).then((ProcessResult results) {
print('shell1 complete');
Process.run(path, []).then((ProcessResult results) {
print('shell2 complete');
print(results.stdout);
});
});
My executable:
// 'Hello World!' program
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
>> g++ main.cc -o main
Is there a way to run your own executables in flutter with the proper permissions? On native android, there is an option to file.setExecutable(true); using this strategy. (Hosting an executable within Android application)
Or will I have to experiment with method channels?
pubspec.yml
name: face_merger
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
sqflite: ^1.3.0+2
process_run: ^0.10.10+1
cupertino_icons: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- lib/py/dist/main
I also verified that the file was written to the internal memory of on the device that I expected it to be written to using android studio's device explore.
Besides the writable permission you need to have readable permission in your app.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.yyy">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
You need to put the .db file extension at the end of the database name (Assuming DB_DIR is for a database). var path = join(DB_DIR, 'main.db');
Then I think you may want to use path_provider package and use
Directory documentsDirectory = await getApplicationDocumentsDirectory();
for the directory where you are accessing the database.
I'm trying to implement Scringo Login status change on Android. But my Broadcast receiver is never called.
I've followed the instructions described on http://www.scringo.com/docs/android-guides/popular/handling-login-status-changes/
So I registered my broadcast receiver:
<receiver android:name="com.jino.footster.MyReceiver">
<intent-filter>
<action android:name="com.scringo.LoginBroadcast" />
</intent-filter>
</receiver>
And I then defined my Broacast receiver:
package com.jino.footster;
import com.scringo.utils.ScringoLogger;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.scringo.LoginBroadcast")) {
boolean isLogin = intent.getExtras().getBoolean("isLogin");
String accountId = intent.getExtras().getString("accountId");
ScringoLogger.e("Got Login receiver: " + isLogin + ", " + accountId);
}
}
}
When I start the application the login seems to be successful: I see the below message in logcat:
04-24 01:12:35.000: I/Scringo(4717): Your Scringo user token is: a03fgalc5E
However, the onReceive method of my broadcast receiver is never called.
Would someone be able to help?
thank you
You forgot the category:
<receiver android:name="com.jino.footster.MyReceiver">
<intent-filter>
<action android:name="com.scringo.LoginBroadcast" />
<category android:name="com.jino.footster"/>
</intent-filter>
</receiver>