cleverpush_flutter 1.23.28 cleverpush_flutter: ^1.23.28 copied to clipboard
CleverPush
import 'dart:async';
import 'package:cleverpush_flutter/cleverpush_flutter.dart';
import 'package:cleverpush_flutter/cleverpush_chat_view.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _debugLabelString = "";
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
if (!mounted) return;
CleverPush.shared.setShowNotificationsInForeground(false);
CleverPush.shared.enableDevelopmentMode();
CleverPush.shared
.setNotificationReceivedHandler((CPNotificationReceivedResult result) {
this.setState(() {
_debugLabelString =
"Notification received: \n${result.notification!.jsonRepresentation().replaceAll("\\n", "\n")}";
});
});
CleverPush.shared
.setNotificationOpenedHandler((CPNotificationOpenedResult result) {
this.setState(() {
_debugLabelString =
"Notification opened: \n${result.notification!.jsonRepresentation().replaceAll("\\n", "\n")}";
});
});
CleverPush.shared.setSubscribedHandler((subscriptionId) {
this.setState(() {
_debugLabelString = "Subscribed: " + subscriptionId!;
});
print("Subscribed: $subscriptionId");
});
CleverPush.shared.enableAppBanners();
CleverPush.shared.setAppBannerShownHandler((CPAppBanner appBanner) {
this.setState(() {
_debugLabelString = "APP BANNER SHOWN: " + appBanner.name!;
});
});
CleverPush.shared.setBrandingColor("#ff0000");
// CleverPush Channel ID
await CleverPush.shared.init("CHANNEL_ID", true);
CleverPush.shared.setChatUrlOpenedHandler((url) {
Widget continueButton = TextButton(
child: Text("Ok"),
onPressed: () => Navigator.pop(context, 'Ok'),
);
showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Chat URL opened'),
content: Text(url),
actions: <Widget>[
continueButton,
],
),
);
});
}
void _handleSubscribe() async {
print("Prompting for Permission");
var id = await CleverPush.shared.subscribe();
print("Subscribed: $id");
}
void _handleUnsubscribe() {
CleverPush.shared.unsubscribe();
}
void _handleIsSubscribed() {
CleverPush.shared.isSubscribed().then((status) {
this.setState(() {
_debugLabelString = status.toString();
});
});
}
void _handleAreNotificationsEnabled() {
CleverPush.shared.areNotificationsEnabled().then((status) {
this.setState(() {
_debugLabelString = status.toString();
});
});
}
void _handleTopicsDialog() {
CleverPush.shared.showTopicsDialog();
}
void _getNotifications() async {
List<CPNotification> notifications = await CleverPush.shared.getNotifications();
if (notifications.isNotEmpty) {
print("Get Notifications: " + notifications[0].jsonRepresentation());
}
this.setState(() {
_debugLabelString = notifications.length.toString();
});
}
void _getNotificationsWithApi() async {
bool combineWithApi = true;
List<CPNotification> remoteNotifications =
await CleverPush.shared.getNotificationsWithApi(combineWithApi);
if (remoteNotifications.isNotEmpty) {
print("Get Notifications With API: " + remoteNotifications[0].jsonRepresentation());
}
this.setState(() {
_debugLabelString = remoteNotifications.length.toString();
});
}
void _getSubscriptionTopics() async {
var topicIds = await CleverPush.shared.getSubscriptionTopics();
String topicIdsString = "";
for (var i = 0; i < topicIds.length; i++) {
if (topicIdsString.isEmpty) {
topicIdsString = topicIds[i];
} else {
topicIdsString = topicIdsString + "," + topicIds[i];
}
}
this.setState(() {
_debugLabelString = topicIdsString;
});
}
void _setSubscriptionTopics() {
List<String> topics = ['hello', 'world'];
CleverPush.shared.setSubscriptionTopics(topics);
}
void _getAvailableTopics() async {
var topicIds = await CleverPush.shared.getAvailableTopics();
if (topicIds.isNotEmpty) {
print(topicIds[0]);
}
this.setState(() {
_debugLabelString = topicIds.length.toString();
});
}
void _getSubscriptionTags() async {
var tagIds = await CleverPush.shared.getSubscriptionTags();
String tagIdsString = "";
for (var i = 0; i < tagIds.length; i++) {
if (tagIdsString.isEmpty) {
tagIdsString = tagIds[i];
} else {
tagIdsString = tagIdsString + "," + tagIds[i];
}
}
this.setState(() {
_debugLabelString = tagIdsString;
});
}
void _addSubscriptionTag() {
CleverPush.shared.addSubscriptionTag('TAG_ID');
}
void _addSubscriptionTags() {
List<String> tagIds = ['TAG_ID1', 'TAG_ID2'];
CleverPush.shared.addSubscriptionTags(tagIds);
}
void _removeSubscriptionTag() {
CleverPush.shared.removeSubscriptionTag('TAG_ID');
}
void _removeSubscriptionTags() {
List<String> tagIds = ['TAG_ID1', 'TAG_ID2'];
CleverPush.shared.removeSubscriptionTags(tagIds);
}
void _getAvailableTags() async {
var tagIds = await CleverPush.shared.getAvailableTags();
if (tagIds.isNotEmpty) {
print(tagIds[0]);
}
this.setState(() {
_debugLabelString = tagIds.length.toString();
});
}
void _trackEvent() async {
CleverPush.shared.trackEvent("EVENT_NAME");
}
void _triggerFollowUpEvent() async {
CleverPush.shared.triggerFollowUpEvent("EVENT_NAME", {
"property_1": "value",
});
}
void _pushSubscriptionAttributeValue() async {
CleverPush.shared.pushSubscriptionAttributeValue("ATTRIBUTE_KEY","ATTRIBUTE_VALUE");
}
void _pullSubscriptionAttributeValue() async {
CleverPush.shared.pullSubscriptionAttributeValue("ATTRIBUTE_KEY","ATTRIBUTE_VALUE");
}
void _showAppBanner() async {
CleverPush.shared.showAppBanner("APP_BANNER_ID");
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('CleverPush Flutter'),
backgroundColor: Color.fromARGB(255, 33, 33, 33),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: SingleChildScrollView(
child: new Table(
children: [
new TableRow(children: [
new CleverPushButton( "Show Chat View", () {
Navigator.pushNamed(context, '/chat');
}, true)
]),
new TableRow(children: [
new CleverPushButton("Subscribe", _handleSubscribe, true)
]),
new TableRow(children: [
new CleverPushButton(
"Unsubscribe", _handleUnsubscribe, true)
]),
new TableRow(children: [
new CleverPushButton(
"Is Subscribed?", _handleIsSubscribed, true)
]),
new TableRow(children: [
new CleverPushButton(
"Are Notifications Enabled?", _handleAreNotificationsEnabled, true)
]),
new TableRow(children: [
new CleverPushButton(
"Show topics dialog", _handleTopicsDialog, true)
]),
new TableRow(children: [
new CleverPushButton(
"Get Notifications", _getNotifications, true)
]),
new TableRow(children: [
new CleverPushButton("Get Remote Notifications",
_getNotificationsWithApi, true)
]),
new TableRow(children: [
new CleverPushButton(
"Get Subscription Topics", _getSubscriptionTopics, true)
]),
new TableRow(children: [
new CleverPushButton(
"Set Subscription Topics", _setSubscriptionTopics, true)
]),
new TableRow(children: [
new CleverPushButton(
"Get Available Topics", _getAvailableTopics, true)
]),
new TableRow(children: [
new CleverPushButton(
"Get Subscription Tags", _getSubscriptionTags, true)
]),
new TableRow(children: [
new CleverPushButton(
"Add Subscription Tag", _addSubscriptionTag, true)
]),
new TableRow(children: [
new CleverPushButton(
"Remove Subscription Tag", _removeSubscriptionTag, true)
]),
new TableRow(children: [
new CleverPushButton(
"Add Subscription Tags", _addSubscriptionTags, true)
]),
new TableRow(children: [
new CleverPushButton(
"Remove Subscription Tags", _removeSubscriptionTags, true)
]),
new TableRow(children: [
new CleverPushButton(
"Get Available Tags", _getAvailableTags, true)
]),
new TableRow(children: [
new CleverPushButton(
"Track Event", _trackEvent, true)
]),
new TableRow(children: [
new CleverPushButton(
"Show App Banner", _showAppBanner, true)
]),
new TableRow(children: [
new CleverPushButton(
"Track Follow-Up Event", _triggerFollowUpEvent, true)
]),
new TableRow(children: [
new CleverPushButton(
"Push Subscription Attribute Value", _pushSubscriptionAttributeValue, true)
]),
new TableRow(children: [
new CleverPushButton(
"Pull Subscription Attribute Value", _pullSubscriptionAttributeValue, true)
]),
new TableRow(children: [
Container(
height: 8.0,
)
]),
new TableRow(children: [
new Container(
child: new Text(_debugLabelString),
alignment: Alignment.center,
)
]),
],
),
),
),
);
},
'/chat': (BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: const Text('Chat View'),
),
body: new Container(
child: CleverPushChatView(
),
),
);
},
},
);
}
}
typedef void OnButtonPressed();
class CleverPushButton extends StatefulWidget {
final String title;
final OnButtonPressed onPressed;
final bool enabled;
CleverPushButton(this.title, this.onPressed, this.enabled);
State<StatefulWidget> createState() => new CleverPushButtonState();
}
class CleverPushButtonState extends State<CleverPushButton> {
@override
Widget build(BuildContext context) {
return new Table(
children: [
new TableRow(children: [
new TextButton(
child: new Text(widget.title),
onPressed: widget.enabled ? widget.onPressed : null,
)
]),
new TableRow(children: [
Container(
height: 8.0,
)
]),
],
);
}
}