Giter VIP home page Giter VIP logo

fcmsharedisolate-update's Introduction

Add this package locally to your flutter project

In this file pubspec.yaml

  fcm_shared_isolate:
    path: ./packages/FcmSharedIsolate-update-master/

Base Of the Project URL: FCM Shared Isolate

fcm_shared_isolate

Firebase Messaging Plugin for Flutter supporting shared isolate

Installing the library

After adding the library to your pubspec.yaml do the following things:

  1. Modify the main activity on the android side of your app to look like the following (typically in android/app/src/main/kotlin/your/app/id/MainActivity.kt):
package your.app.id

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.WindowManager

class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    };

    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        return provideEngine(this)
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        // do nothing, because the engine was been configured in provideEngine
    }

    companion object {
        var engine: FlutterEngine? = null
        fun provideEngine(context: Context): FlutterEngine {
            var eng = engine ?: FlutterEngine(context, emptyArray(), true, false)
            engine = eng
            return eng
        }
    }
  1. Add an FcmPushService (typically in android/app/src/main/kotlin/your/app/id/FcmPushService.kt)
package your.app.id

import com.famedly.fcm_shared_isolate.FcmSharedIsolateService

import your.app.id.MainActivity

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.view.FlutterMain
import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.WindowManager

class FcmPushService : FcmSharedIsolateService() {
    override fun getEngine(): FlutterEngine {
        return provideEngine(getApplicationContext())
    }

    companion object {
        fun provideEngine(context: Context): FlutterEngine {
            var engine = MainActivity.engine
            if (engine == null) {
                engine = MainActivity.provideEngine(context)
                engine.getLocalizationPlugin().sendLocalesToFlutter(
                    context.getResources().getConfiguration())
                engine.getDartExecutor().executeDartEntrypoint(
                    DartEntrypoint.createDefault())
            }
            return engine
        }
    }
}
  1. Add the intent filters to your AndroidManifest.xml (typically in android/app/src/main/AndroidManifest.xml):
<service android:name=".FcmPushService"
  android:exported="false">
  <intent-filter>
	<action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>

Note that the .FcmPushService has to match the class name defined in the file above

Usage

// Create the instance
final fcm = FcmSharedIsolate();

// Only for iOS you need to request permissions:
if (Platform.isIOS) {
    await fcm.requestPermission();
}

// Get the push token:
await fcm.getToken();

// Set the listeners
fcm.setListeners(
    onMessage: onMessage,
    onNewToken: onNewToken,
);

Future<void> onMessage(Map<dynamic, dynamic> message) async {
    print('Got a new message from firebase cloud messaging: $message');
}

fcmsharedisolate-update's People

Contributors

taherfattahi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.