简体   繁体   中英

Flutter VoIP Push notification on Android

I am trying to implement kind of push notification for Video app.

On Flutter side I receive FCM Push notification and when this happens a function is call to handle code from native side.

onMessage: (Map<String, dynamic> message) async {
        print('on resume $message');

        await callFromAndroid();

And Future function which is being called is this:

Future<void> callFromAndroid() async
  {
    try {
      await platform.invokeMethod('callFromAndroid');
    } catch (e) {
      print(e);
    }
  }

And on Android side this is MainActivity which should trigger VideoActivity handler...

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "my.test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

                if (methodCall.method.equals("callFromAndroid")) {

                    videoActivityHandler();
                }

            }
        });
    }

    public void videoActivityHandler() {
        Intent intent = new Intent(this, VideoCallActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        getApplicationContext().startActivity(intent);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


// to release screen lock
        KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
        keyguardLock.disableKeyguard();
    }
}

However, when FCM notification arrives it arises empty activity. Also it is not working when app is in background or phone is locked.

I am total beginner in mobile app development and have no experience in Android Native development. All these code I found while doing research on this topic...

So any help is appreciated.

I am too late, but...

If you like to receive FCM push when app is on background/terminated or even in foreground you should send data payload without notification payload. In this case you can handle notification in FCM's onBackgroundMessage callback.

May be someone can find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM