Package not found in Application.java in flutter FlutterFirebaseMessaging plugin - android

I am working with firebase messaging. I followed the steps as given in readme of the plugin. But my application .java is giving an error.
Application.java
package com.app.demoapp;
import com.transistorsoft.flutter.backgroundfetch.BackgroundFetchPlugin;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
BackgroundFetchPlugin.setPluginRegistrant(this);
}
#Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
Error:
error: cannot find symbol
FlutterFirebaseMessagingService.setPluginRegistrant(this);
^
symbol: method setPluginRegistrant(Application)
location: class FlutterFirebaseMessagingService
1 error

I have faced the same problem and so far I have not found any solution
but If you want just show notification with out handle it in background and just lunch app when click on it
remove FlutterFirebaseMessagingService.setPluginRegistrant(this);and the notification will work fine as Notification messages type
if you don't know about Notification type in fcm
refer to Message types
With FCM, you can send two types of messages to clients:
1- Notification messages, sometimes thought of as "display messages."
These are handled by the FCM SDK automatically.
2- Data messages, which are handled by the client app.
so we use Notification messages here until find solution for handle Data messages

Looks like the instructions file is outdated, it was missing a very important step that you can check at the github repository README
Add the com.google.firebase:firebase-messaging dependency in your app-level build.gradle file that is typically located at /android/app/build.gradle.
dependencies {
// ...
implementation 'com.google.firebase:firebase-messaging:20.1.0'
}

just set with
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 extends FlutterApplication implements
PluginRegistrantCallback {
#Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
#Override
public void registerWith(PluginRegistry registry) {
FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
}
}
And make FirebaseCloudMessagingPluginRegisttrant java

Related

Implement Play Asset Delivery In flutter

How to implement on-demand Play Asset Delivery in flutter through methodchannels.
Actually i am trying to make a dashboard for some app which accesses assets using ContentProvider so i thought play asset delivery might work here.
I know that same can be achieved using deferred components and I have already tried deferred components which is provided by flutter.
You can find it here
Currently, there is an issue with flutter which causes assets to not load when deferred. You can find a link to the issue here.
I do not have any idea of native language and this is the only option I have right now which is implementing a methodchannel so any help would be appreciated
I dont think you can achieve the same thing using play asset delivery. You can try a workaround for this
Disable android:enabled by default by adding android:enabled="false" in your content provider and then use below methodchannel to enable it later
package dev.blah.blah;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import android.content.ContextWrapper;
import android.widget.Toast;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "dev.dhanraj.kwgt.test.dashboard";
#Override
public void configureFlutterEngine(#NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
// Note: this method is invoked on the main thread.
if (call.method.equals("enable")) {
ContextWrapper aContext = new ContextWrapper(getApplicationContext());
aContext.getPackageManager().setComponentEnabledSetting(new android.content.ComponentName(aContext, "org.kustom.api.Provider"), android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 1);
result.success(null);
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
} else{
result.notImplemented();
}
}
);
}
}

React Native error: package android.support.annotation does not exist

I'm attempting to update a plugin for a react native project that was originally developed and worked in 0.40.0.
Our current project is now on:
react-native-cli: 2.0.1
react-native: 0.60.0
In the code we have this method:
private void sendEvent(ReactContext reactContext,
String eventName,
#Nullable Object params) {
reactContext
.getJSModule(RCTNativeAppEventEmitter.class)
.emit(eventName, params);
}
Which uses this import to get the Nullable class:
import android.support.annotation.Nullable;
The problem is that when we run react-native run-android
We get an error:
error: package android.support.annotation does not exist
import android.support.annotation.Nullable;
^
Any idea how I can get access to this Nullable class again or re-create the functionality such that I can pass in either a null field or like an int?
Examples of how I'd call it:
public void UpdatePoints(int points) {
sendEvent(this.reactContext, "UpdatePoints", points);
}
#Override
public void onPointsEarned() {
sendEvent(this.reactContext, "onPointsEarned", null);
}
I found another SO article that recommended this:
// build.gradle
implementation "androidx.annotation:annotation:1.1.0"
// where use it
import androidx.annotation.Nullable;
But that didn't work. I've found a few things mentioning a transition from support to androidX and it seems like this may be related.
Looks like all you need to do is handle androidx by adding import androidx.annotation.Nullable; instead of import android.support.annotation.Nullable;

Error:(42, 16) error: incompatible types required: Loader<List<String>> found:FileLoader

I'm trying to use the LoaderManager in my code however I'm getting an incompatible types error:
#Override
public Loader<List<String>> onCreateLoader(int id, Bundle args) {
return new FileLoader(MainActivity.this);
}
shouldn't this statement return new FileLoader(MainActivity.this) returns a Loader? And here is the implementation of the LoaderManager:
public class FileLoader extends AsyncTaskLoader<List<String>>{
public FileLoader(Context context){
super(context);
}
#Override
public List<String> loadInBackground() {
.........
........
return listFiles;
}
}
I had the same problem(incompatible types required), but when see your post ,it helps me to learn more.In my code, it was imported:
"import androidx.loader.content.AsyncTaskLoader;"
, so i change it to:
"import android.content.AsyncTaskLoader;"
then my problem solved.
Because "AsyncTaskLoader" : This class was deprecated in API level 28,by below ref.
https://developer.android.com/reference/android/content/AsyncTaskLoader
by this advice,"You should also consider migrating existing projects to AndroidX", the webpage below:
https://developer.android.com/jetpack/androidx/migrate/class-mappings
finally I succeeded to solve the problem ... it was that didn't Import the support library in the AsyncTaskLoader file , while using it at the Main file ...so i just removed
import android.content.AsyncTaskLoader;
and Import the following support library
import android.support.v4.content.AsyncTaskLoader;

How to create custom shadows in robolectric 3.0?

I need to mock some custom class (create for it a shadow).
I have already read on http://robolectric.org/custom-shadows/ how to do this.
so, i have some class:
public class MyClass {
public static int regularMethod() { return 1; }
}
I create a shadow:
#Implements(MyClass.class)
public class MyShadowClass {
#Implementation
public static int regularMethod() { return 2; }
}
And i set the shadow in Test-class:
#RunWith(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, shadows={MyShadowClass.class})
public class MyTest {
#Test
public void testShadow() {
assertEquals(2, MyClass.regularMethod());
}
}
But the shadow is not used.
java.lang.AssertionError:
Expected :2
Actual :1
How to make any custom shadow visible for RobolectricGradleTestRunner?
I have already tried:
http://www.codinguser.com/2015/06/how-to-create-shadow-classes-in-robolectric-3/
https://github.com/jiahaoliuliu/RobolectricSample/blob/master/app-tests/src/main/java/com/jiahaoliuliu/robolectricsample/RobolectricGradleTestRunner.java
Mock native method with a Robolectric Custom shadow class
but i get various compilation errors, such as
InstrumentingClassLoaderConfig not found
Setup not found
how to use custom shadows correctly in robolectric 3.0?
Custom shadows should be avoided and must be a last ditch resort. It should only be used if you cannot do much refactor in your code which is preventing you from running your tests like a native method call. It's better to mock the object of that class or spy using powermock or mockito than custom shadow it. If it's a static method, then use powermock.
In our project, we had a class which had some native methods and it was the config class used everywhere in the app. So we moved the native methods to another class and shadowed that. Those native methods were failing the test cases.
Anyways here's how you can custom shadow in robolectric 3.0:
Create a custom test runner that extends RobolectricGradleTestRunner:
public class CustomRobolectricTestRunner extends RobolectricGradleTestRunner {
public CustomRobolectricTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
public InstrumentationConfiguration createClassLoaderConfig() {
InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
builder.addInstrumentedPackage("com.yourClassPackage");
return builder.build();
}
Make sure that that the package doesn't contain any test cases that you are running using robolectric.
I am Jiahao, the creator of the second repository that you are referring.
First of all thanks for to check my code. I do many researches on Android and I am glad that my research is useful for someone else.
Then, the Shadow about Robolectric. I am using Robolectric 3.1 in this project, to test how Robolectric 3 works with MarshMallow:
https://github.com/jiahaoliuliu/robolectricForMarshmallow
I have been testing the new Runtime Permission Manager, as well as shadowing application and activities.
Here is sample code of the shadowed activity:
import android.content.Context;
import com.jiahaoliuliu.robolectricformarshmallow.controller.MainController;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
/**
* Created by Jiahao on 7/18/16.
*/
#Implements(MainController.class)
public class MainControllerShadow {
public void __constructor__ (Context context) {
// Not do anything
}
#Implementation
public String getTextToDisplay(boolean permissionGranted) {
return "Test";
}
}
https://github.com/jiahaoliuliu/robolectricForMarshmallow/blob/master/app/src/test/java/com/jiahaoliuliu/robolectricformarshmallow/shadow/MainControllerShadow.java
And this is how I am using it in the unit test:
package com.jiahaoliuliu.robolectricformarshmallow;
import com.jiahaoliuliu.robolectricformarshmallow.shadow.MainControllerShadow;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.*;
/**
* Created by Jiahao on 6/30/16.
*/
#RunWith(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, manifest = Config.NONE, application = FoolApplication.class,
shadows = { MainControllerShadow.class}, sdk = 18)
public class MainActivityTest {
private MainActivity mMainActivity;
#Before
public void setUp() throws Exception {
mMainActivity = Robolectric.setupActivity(MainActivity.class);
}
#After
public void tearDown() throws Exception {
}
#Test
public void testOnCreate() throws Exception {
// Simple test to know that it works
assertTrue(true);
}
}
https://github.com/jiahaoliuliu/robolectricForMarshmallow/blob/master/app/src/test/java/com/jiahaoliuliu/robolectricformarshmallow/MainActivityTest.java
As you can see, I am not using customized Gradle Test Runner. I have checked the source code of Robolectric, for version 3.0 and 3.1 (latest) it is good enough to just specify the shadow classes in the header.
I hope it helps

Android reflection doesn't work - it don't find any class

I have an Android application.
I want to scan for all classes within a package for a specify annotation.
I have:
package com.sample.package;
import com.sample.core.Controller;
import com.sample.core.ProtocolId;
#Controller
public class OtherController implements ControllerInterface{
#ProtocolId(id=100)
public void doSomething(){
//do something
}
}
I'm finding for classes annotated with #Controller for a specify #ProtocolId number.
I'm using Google Reflections library.
Here is how I'm scanning:
package com.sample.package;
import org.reflections.ReflectionUtils;
import org.reflections.Reflections;
import com.sample.core.Controller;
import com.sample.core.ProtocolId;
public class FrontController {
public void executeProperControllerMethodBasedOnId(){
Reflections ref = new Reflections("com.sample.package");
Set<Class<?>> classes = ref.getTypesAnnotatedWith(Controller.class);
System.out.println(classes.size()); //THE SIZE IS 0!!!
//The reflection doesn't worked! It didn't found any class!
}
}
The above code doesn't find any class annotated with specify annotation. Is
there something which I miss when I'm using google reflection library on
android?

Categories

Resources