Skip to content

Instantly share code, notes, and snippets.

@followthemoney1
Last active November 18, 2020 17:03
Show Gist options
  • Save followthemoney1/c933a033c3803171d41c0e4a23cd3642 to your computer and use it in GitHub Desktop.
Save followthemoney1/c933a033c3803171d41c0e4a23cd3642 to your computer and use it in GitHub Desktop.
brach share flutter example
1 файл receive.dart получение (тебя интересует listenDynamicLinks)
2 класс создание( тут все по гайдам, строчка 24 )
ДЛЯ ОТКРЫТИЯ САЙТА В СЛУЧАЕЕ НЕ УСТАНОВЛЕННОГО ПРИЛОЖЕНИЯ
linkProperties.addControlParameter("$desktop_url", "https://example.com/home");
class NewsListBloc extends Bloc<NewsListEvent, NewsListState> {
NewsListBloc() : super(NewsListState(isLoading: true));
FirebaseManager firebaseManager = FirebaseManager();
SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager();
NetworkManager networkManager = NetworkManager();
StreamController<FirebaseNews> newsDataController =
StreamController<FirebaseNews>.broadcast();
List<FirebaseNews> topNewsDataController = [];
List<FirebaseNews> _news = [];
List<FirebaseNews> _fetchedNews = [];
int _newsCount = 40;
bool filtered = false;
get news => _news.length > _newsCount
? _news.getRange(0, _newsCount).toList()
: _news;
// get news => _news;
hasMoreItems({@required int count}) => news.length > count;
bool _subscribe = false;
bool _isLoadingNews = true;
var branchDeeplinkNewsId;
init() {
listenDynamicLinks();
}
StreamSubscription<Map> streamSubscription;
StreamController<String> controllerData = StreamController<String>();
StreamController<String> controllerInitSession = StreamController<String>();
StreamController<String> controllerUrl = StreamController<String>();
void listenDynamicLinks() async {
streamSubscription = FlutterBranchSdk.initSession().listen((data) {
//developer.log('listenDynamicLinks - DeepLink Data: $data');
controllerData.sink.add((data.toString()));
if (data.containsKey('+clicked_branch_link') &&
data['+clicked_branch_link'] == true) {
//developer.log('Custom string: ${data['news_id']}');
branchDeeplinkNewsId = data['news_id'];
add(NewsListEvent.openDeepLink);
}
}, onError: (error) {
PlatformException platformException = error as PlatformException;
//developer.log('InitSession error: ${platformException.code} - ${platformException.message}');
controllerInitSession.add(
'InitSession error: ${platformException.code} - ${platformException.message}');
});
}
@override
Stream<NewsListState> mapEventToState(NewsListEvent event) async* {
switch (event) {
case NewsListEvent.subscribe:
init();
if (!_subscribe) getNews(count: _newsCount);
_subscribe = true;
_getTopNews();
_subscribeToNews();
break;
case NewsListEvent.update:
yield NewsListState(
news: news,
topNews: topNewsDataController,
isLoading: _isLoadingNews,
newsIsEmpty: news == null || news.isEmpty);
break;
case NewsListEvent.openDeepLink:
if (branchDeeplinkNewsId != null) {
final oneNews = await openNewsById();
yield NewsListState(
news: news,
topNews: topNewsDataController,
isLoading: _isLoadingNews,
oneNews: oneNews,
newsIsEmpty: news == null || news.isEmpty);
branchDeeplinkNewsId = null;
//developer.log('------onnew state----');
return;
}
break;
default:
addError(Exception('unsupported event'));
}
}
...
}
class NewsDetailBloc extends Cubit<bool> {
NewsDetailBloc() : super(true);
FirebaseManager _firebaseManager = FirebaseManager();
SharedPreferenceManager _sharedPreferenceManager = SharedPreferenceManager();
BranchContentMetaData metadata;
BranchUniversalObject buo;
BranchLinkProperties lp;
BranchEvent eventStandart;
init() {}
void initDeepLinkData(FirebaseNews arguments) {
metadata =
BranchContentMetaData().addCustomMetadata('custom_string', 'abc');
buo = BranchUniversalObject(
canonicalIdentifier: 'flutter/branch',
title: arguments.title,
imageUrl: arguments.images != null && arguments.images.length > 0
? arguments.images.first.url
: '',
contentDescription: parseHtmlString(arguments.text),
contentMetadata: BranchContentMetaData()
..addCustomMetadata('news_id', arguments.key),
keywords: ['Plugin', 'Branch', 'Flutter'],
publiclyIndex: true,
locallyIndex: true,
);
FlutterBranchSdk.registerView(buo: buo);
lp = BranchLinkProperties(
channel: 'facebook',
feature: 'sharing',
//alias: 'flutterplugin' //define link url,
stage: 'new share',
campaign: arguments.channelId,
tags: ['one', 'two', 'three']);
lp.addControlParam('\$uri_redirect_mode', '1');
eventStandart = BranchEvent.standardEvent(BranchStandardEvent.ADD_TO_CART);
}
void shareLink(FirebaseNews arguments) async {
BranchResponse response = await FlutterBranchSdk.showShareSheet(
buo: buo,
linkProperties: lp,
messageText: arguments.title,
androidMessageTitle: arguments.title,
androidSharingTitle: arguments.title);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment