video call setup with flutter 2.8, firebase modifications

sikander_video_call_improvements
Sikander Saleem 4 years ago
parent 7d7864a1eb
commit 67b87449a9

@ -0,0 +1,114 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugins.firebase.messaging;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.google.firebase.messaging.RemoteMessage;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class FlutterFirebaseMessagingReceiver extends BroadcastReceiver {
private static final String TAG = "FLTFireMsgReceiver";
static HashMap<String, RemoteMessage> notifications = new HashMap<>();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "broadcast received for message");
if (ContextHolder.getApplicationContext() == null) {
ContextHolder.setApplicationContext(context.getApplicationContext());
}
if (intent.getExtras() == null) {
Log.d(
TAG,
"broadcast received but intent contained no extras to process RemoteMessage. Operation cancelled.");
return;
}
RemoteMessage remoteMessage = new RemoteMessage(intent.getExtras());
// Store the RemoteMessage if the message contains a notification payload.
if (remoteMessage.getNotification() != null) {
notifications.put(remoteMessage.getMessageId(), remoteMessage);
FlutterFirebaseMessagingStore.getInstance().storeFirebaseMessage(remoteMessage);
}
// |-> ---------------------
// App in Foreground
// ------------------------
if (FlutterFirebaseMessagingUtils.isApplicationForeground(context)) {
Intent onMessageIntent = new Intent(FlutterFirebaseMessagingUtils.ACTION_REMOTE_MESSAGE);
onMessageIntent.putExtra(FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
LocalBroadcastManager.getInstance(context).sendBroadcast(onMessageIntent);
return;
}
// |-> ---------------------
// App in Background/Quit
// ------------------------
if (remoteMessage.getData().containsKey("is_call")) {
Intent intent12 = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.startActivity(intent12);
try {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent onMessageIntent = new Intent(FlutterFirebaseMessagingUtils.ACTION_REMOTE_MESSAGE);
onMessageIntent.putExtra(FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
LocalBroadcastManager.getInstance(context).sendBroadcast(onMessageIntent);
}
}, 5000);
} catch (Exception e) {
Log.e("AppCallingException", e.getMessage());
}
// super.onMessageReceived(remoteMessage);
} //else
// super.onMessageReceived(remoteMessage);
//
// if (remoteMessage.getData().containsKey("is_call")) {
// Log.e("AppCalling", "started...");
// Intent intent12 = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
// intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// context.startActivity(intent12);
// try {
// Log.e("AppCalling", "going to sleep...");
// TimeUnit.SECONDS.sleep(10);
// Log.e("AppCalling", "sendig to broadcast receiver...");
// Log.e("AppCalling:DAta", remoteMessage.getData().containsKey("is_call") + "");
// Intent onBackgroundMessageIntent =
// new Intent(context, FlutterFirebaseMessagingBackgroundService.class);
// onBackgroundMessageIntent.putExtra(
// FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
// FlutterFirebaseMessagingBackgroundService.enqueueMessageProcessing(
// context, onBackgroundMessageIntent);
// //return;
// } catch (Exception e) {
// Log.e("AppCallingException", e.getMessage());
// }
//
// }
// Intent onBackgroundMessageIntent =
// new Intent(context, FlutterFirebaseMessagingBackgroundService.class);
// onBackgroundMessageIntent.putExtra(
// FlutterFirebaseMessagingUtils.EXTRA_REMOTE_MESSAGE, remoteMessage);
// FlutterFirebaseMessagingBackgroundService.enqueueMessageProcessing(
// context, onBackgroundMessageIntent);
}
}

@ -1,134 +1,127 @@
import 'dart:convert';
import 'dart:io';
import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/models/LiveCare/IncomingCallData.dart';
import 'package:diplomaticquarterapp/pages/landing/landing_page.dart';
import 'package:diplomaticquarterapp/pages/livecare/incoming_call.dart';
import 'package:diplomaticquarterapp/uitl/app-permissions.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huawei_push/huawei_push.dart' as h_push;
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_messaging/firebase_messaging.dart' as fir;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hms_gms_availability/flutter_hms_gms_availability.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:huawei_push/huawei_push.dart' as h_push;
import 'app_shared_preferences.dart';
import 'navigation_service.dart';
// |--> Push Notification Background
Future<dynamic> backgroundMessageHandler(dynamic message) async{
Future<dynamic> backgroundMessageHandler(dynamic message) async {
fir.RemoteMessage message_;
if(message is h_push.RemoteMessage){ // if huawei remote message convert it to Firebase Remote Message
if (message is h_push.RemoteMessage) {
// if huawei remote message convert it to Firebase Remote Message
message_ = toFirebaseRemoteMessage(message);
} else {
if (message.data != null && message.data['is_call'] == 'true') {
_incomingCall(message.data);
return;
}
}
if (message_.data != null && message_.data['is_call'] == 'true') {
_incomingCall(message_.data);
return;
}
h_push.Push.localNotification({
h_push.HMSLocalNotificationAttr.TITLE: 'Background Message',
h_push.HMSLocalNotificationAttr.MESSAGE: "By: BackgroundMessageHandler"
});
h_push.Push.localNotification({h_push.HMSLocalNotificationAttr.TITLE: 'Background Message', h_push.HMSLocalNotificationAttr.MESSAGE: "By: BackgroundMessageHandler"});
}
// Push Notification Background <--|
RemoteMessage toFirebaseRemoteMessage(h_push.RemoteMessage message){
RemoteMessage toFirebaseRemoteMessage(h_push.RemoteMessage message) {
final payload_data = jsonDecode(message.data);
final fire_message = RemoteMessage(
from: message.from,
collapseKey: message.collapseKey,
data: payload_data['data'],
messageId: message.messageId,
sentTime: DateTime.fromMillisecondsSinceEpoch(message.sentTime*1000),
ttl: message.ttl,
category: null,
messageType: message.type,
notification: RemoteNotification(
title: message.notification.title,
titleLocArgs: (message.notification.titleLocalizationArgs ?? []).map((e) => e.toString()).toList(),
titleLocKey: message.notification.titleLocalizationKey,
body: message.notification.body,
bodyLocArgs: (message.notification.bodyLocalizationArgs ?? []).map((e) => e.toString()).toList(),
bodyLocKey: message.notification.bodyLocalizationKey,
android: AndroidNotification(
channelId: message.notification.channelId,
clickAction: message.notification.clickAction,
color: message.notification.color,
count: null,
imageUrl: message.notification.imageUrl.path,
link: message.notification.link.path,
smallIcon: message.notification.icon,
sound: message.notification.sound,
ticker: message.notification.ticker,
tag: message.notification.tag,
),
)
);
from: message.from,
collapseKey: message.collapseKey,
data: payload_data['data'],
messageId: message.messageId,
sentTime: DateTime.fromMillisecondsSinceEpoch(message.sentTime * 1000),
ttl: message.ttl,
category: null,
messageType: message.type,
notification: RemoteNotification(
title: message.notification.title,
titleLocArgs: (message.notification.titleLocalizationArgs ?? []).map((e) => e.toString()).toList(),
titleLocKey: message.notification.titleLocalizationKey,
body: message.notification.body,
bodyLocArgs: (message.notification.bodyLocalizationArgs ?? []).map((e) => e.toString()).toList(),
bodyLocKey: message.notification.bodyLocalizationKey,
android: AndroidNotification(
channelId: message.notification.channelId,
clickAction: message.notification.clickAction,
color: message.notification.color,
count: null,
imageUrl: message.notification.imageUrl.path,
link: message.notification.link.path,
smallIcon: message.notification.icon,
sound: message.notification.sound,
ticker: message.notification.ticker,
tag: message.notification.tag,
),
));
return fire_message;
}
_incomingCall(Map data) async{
_incomingCall(Map data) async {
LandingPage.incomingCallData = IncomingCallData.fromJson(data);
if(LandingPage.isOpenCallPage == false){
if (LandingPage.isOpenCallPage == false) {
LandingPage.isOpenCallPage = true;
final permited = await AppPermission.askVideoCallPermission(currentContext);
if(permited)
await NavigationService.navigateToPage(IncomingCall(incomingCallData: LandingPage.incomingCallData));
if (permited) await NavigationService.navigateToPage(IncomingCall(incomingCallData: LandingPage.incomingCallData));
LandingPage.isOpenCallPage = false;
}
await Future.delayed(Duration(milliseconds: 500));
await AppSharedPreferences().remove('call_data');
}
class PushNotificationHandler{
class PushNotificationHandler {
final BuildContext context;
static PushNotificationHandler _instance;
PushNotificationHandler(this.context){
PushNotificationHandler(this.context) {
PushNotificationHandler._instance = this;
}
static PushNotificationHandler getInstance() => _instance;
init() async{
init() async {
if (Platform.isIOS) {
final permission = await FirebaseMessaging.instance.requestPermission();
if(permission.authorizationStatus == AuthorizationStatus.denied)
return;
if (permission.authorizationStatus == AuthorizationStatus.denied) return;
}
if(Platform.isAndroid && (await FlutterHmsGmsAvailability.isHmsAvailable)) { // 'Android HMS' (Handle Huawei Push_Kit Streams)
if (Platform.isAndroid && (await FlutterHmsGmsAvailability.isHmsAvailable)) {
// 'Android HMS' (Handle Huawei Push_Kit Streams)
h_push.Push.enableLogger();
final result = await h_push.Push.setAutoInitEnabled(true);
h_push.Push.onNotificationOpenedApp.listen((message){
h_push.Push.onNotificationOpenedApp.listen((message) {
newMessage(toFirebaseRemoteMessage(message));
}, onError: (e) => print(e.toString()));
h_push.Push.onMessageReceivedStream.listen((message){
h_push.Push.onMessageReceivedStream.listen((message) {
newMessage(toFirebaseRemoteMessage(message));
}, onError: (e) => print(e.toString()));
h_push.Push.getTokenStream.listen((token){
h_push.Push.getTokenStream.listen((token) {
onToken(token);
}, onError: (e) => print(e.toString()));
await h_push.Push.getToken('');
h_push.Push.registerBackgroundMessageHandler(backgroundMessageHandler);
}else{ // 'Android GMS or iOS' (Handle Firebase Messaging Streams)
} else {
// 'Android GMS or iOS' (Handle Firebase Messaging Streams)
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
newMessage(message);
@ -145,19 +138,15 @@ class PushNotificationHandler{
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
final fcmToken = await FirebaseMessaging.instance.getToken();
if(fcmToken != null)
onToken(fcmToken);
if (fcmToken != null) onToken(fcmToken);
}
}
newMessage(RemoteMessage remoteMessage){
if(remoteMessage.data['is_call'] == 'true' || remoteMessage.data['is_call'] == true)
_incomingCall(remoteMessage.data);
newMessage(RemoteMessage remoteMessage) {
if (remoteMessage.data['is_call'] == 'true' || remoteMessage.data['is_call'] == true) _incomingCall(remoteMessage.data);
}
onToken(String token) async{
onToken(String token) async {
print("Push Notification Token: " + token);
AppSharedPreferences().setString(PUSH_TOKEN, token);
DEVICE_TOKEN = token;
@ -165,11 +154,10 @@ class PushNotificationHandler{
onResume() async {
var call_data = await AppSharedPreferences().getObject('call_data');
if(call_data != null){
if (call_data != null) {
_incomingCall(call_data);
}
}
}
/* todo verify all functionality */
@ -275,4 +263,4 @@ class PushNotificationHandler{
// print("Is Call Not Found iOS");
// }
// },
// );
// );

Loading…
Cancel
Save