Skip to content

Commit

Permalink
Feat: Update plugin to 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdi-malv committed Jan 28, 2020
1 parent bf9eab0 commit 6751051
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 70 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.1

- Fix formatting of plugin
- Minor improvements

## 2.0.0

* Migrate to the new Plus sdk of Pushe
Expand Down
181 changes: 113 additions & 68 deletions lib/pushe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,123 +7,143 @@ import 'dart:convert';
/// Main plugin class handling most of SDK's works.
///
class Pushe {

// Callback handlers
static void Function(NotificationData) _receiveCallback;
static void Function(NotificationData) _clickCallback;
static void Function(NotificationData) _dismissCallback;
static void Function(String) _customContentCallback;
static void Function(NotificationData, NotificationButtonData) _buttonClickCallback;

static void Function(NotificationData, NotificationButtonData)
_buttonClickCallback;

static const MethodChannel _channel = const MethodChannel('Pushe');

/// Get the unique id of the devices generated by android id or ad id
static Future<String> getAndroidId() async => await _channel.invokeMethod("Pushe#getAndroidId");
static Future<String> getAndroidId() async =>
await _channel.invokeMethod("Pushe#getAndroidId");

/// Get google advertising id
static Future<String> getGoogleAdvertisingId() async => await _channel.invokeMethod("Pushe#getGoogleAdvertisingId");
static Future<String> getGoogleAdvertisingId() async =>
await _channel.invokeMethod("Pushe#getGoogleAdvertisingId");

/// Get custom id
static Future<String> getCustomId() async => await _channel.invokeMethod("Pushe#getCustomId");
static Future<String> getCustomId() async =>
await _channel.invokeMethod("Pushe#getCustomId");

/// Set custom id
static Future<void> setCustomId(String id) async => _channel.invokeMethod("Pushe#setCustomId",{"id":id});
static Future<void> setCustomId(String id) async =>
_channel.invokeMethod("Pushe#setCustomId", {"id": id});

/// Get email
static Future<String> getUserEmail() async => await _channel.invokeMethod("Pushe#getUserEmail");
static Future<String> getUserEmail() async =>
await _channel.invokeMethod("Pushe#getUserEmail");

/// Set email
static Future<void> setUserEmail(String email) async => _channel.invokeMethod("Pushe#setUserEmail",{"email":email});
static Future<void> setUserEmail(String email) async =>
_channel.invokeMethod("Pushe#setUserEmail", {"email": email});

/// Get user phone number
static Future<String> getUserPhoneNumber() async => await _channel.invokeMethod("Pushe#getUserPhoneNumber");
static Future<String> getUserPhoneNumber() async =>
await _channel.invokeMethod("Pushe#getUserPhoneNumber");

/// Set user phone number
static Future<void> setUserPhoneNumber(String phone) async => _channel.invokeMethod("Pushe#setUserPhoneNumber",{"phone":phone});
static Future<void> setUserPhoneNumber(String phone) async =>
_channel.invokeMethod("Pushe#setUserPhoneNumber", {"phone": phone});

/// Add tags.
/// [tags] key-value pairs
/// [callback] is an optional function that will be called with result of adding tags.
static Future<void> addTags(Map tags, {Function(bool) callback}) async {
var result = await _channel.invokeMethod("Pushe#addTags", {"tags":tags});
var result = await _channel.invokeMethod("Pushe#addTags", {"tags": tags});
callback?.call(result);
}

/// Remove tags.
/// [tags] list of tag keys to remove
/// [callback] is an optional function that will be called with result of removing tags.
static Future<void> removeTags(List tags, {Function(bool) callback}) async {
var result = await _channel.invokeMethod("Pushe#removeTags", {"tags":tags});
var result =
await _channel.invokeMethod("Pushe#removeTags", {"tags": tags});
callback?.call(result);
}

/// Get subscribed tags
static Future<Map> getSubscribedTags() async => await _channel.invokeMethod("Pushe#getSubscribedTags");
static Future<Map> getSubscribedTags() async =>
await _channel.invokeMethod("Pushe#getSubscribedTags");

/// Get subscribed topics
static Future<List> getSubscribedTopics() async => await _channel.invokeMethod("Pushe#getSubscribedTopics");


static Future<List> getSubscribedTopics() async =>
await _channel.invokeMethod("Pushe#getSubscribedTopics");

/// Subscribe to a topic.
/// [topic] is the name of that topic. The naming rules must follow FCM topic naming standards.
/// [callback] is an optional function that will be called with result of subscription.
static Future<void> subscribe(String topic, {Function(bool) callback}) async {
var result = await _channel.invokeMethod("Pushe#subscribe", {"topic":topic});
var result =
await _channel.invokeMethod("Pushe#subscribe", {"topic": topic});
callback?.call(result);
}

/// Unsubscribe from a topic already subscribed.
/// [topic] is the name of that topic. The naming rules must follow FCM topic naming standards.
/// [callback] is an optional function that will be called with result of Unsubscription.
static Future<void> unsubscribe(String topic, {Function(bool) callback}) async {
var result = await _channel.invokeMethod("Pushe#unsubscribe", { "topic":topic });
static Future<void> unsubscribe(String topic,
{Function(bool) callback}) async {
var result =
await _channel.invokeMethod("Pushe#unsubscribe", {"topic": topic});
callback?.call(result);
}

/// If this function is called, notification will not be shown.
static Future<void> setNotificationOff() async => _channel.invokeMethod("Pushe#setNotificationOff");
static Future<void> setNotificationOff() async =>
_channel.invokeMethod("Pushe#setNotificationOff");

/// Default of notification is set to On, if you have set it off, you can revert it using this function.
static Future<void> setNotificationOn() async => _channel.invokeMethod("Pushe#setNotificationOn");
static Future<void> setNotificationOn() async =>
_channel.invokeMethod("Pushe#setNotificationOn");

static Future<bool> isNotificationOn() async => _channel.invokeMethod("Pushe#isNotificationOn");
static Future<bool> isNotificationOn() async =>
_channel.invokeMethod("Pushe#isNotificationOn");

/// Check if Pushe is initialized to server or not.
static Future<bool> isInitialized() async => await _channel.invokeMethod("Pushe#isInitialized");
/// Check if Pushe is initialized to server or not.
static Future<bool> isInitialized() async =>
await _channel.invokeMethod("Pushe#isInitialized");

/// Check if Pushe is registered to server or not.
static Future<bool> isRegistered() async => await _channel.invokeMethod("Pushe#isRegistered");
static Future<bool> isRegistered() async =>
await _channel.invokeMethod("Pushe#isRegistered");

/// Call it's callback when registration is completed
static Future<void> setRegistrationCompleteListener(Function callback) async {
var result = await _channel.invokeMethod("Pushe#setRegistrationCompleteListener");
if(result)
callback?.call();
var result =
await _channel.invokeMethod("Pushe#setRegistrationCompleteListener");
if (result) callback?.call();
}

/// Call it's callback when initialization is completed
static Future<void> setInitializationCompleteListener(Function callback) async {
var result = await _channel.invokeMethod("Pushe#setInitializationCompleteListener");
if(result)
callback?.call();
static Future<void> setInitializationCompleteListener(
Function callback) async {
var result =
await _channel.invokeMethod("Pushe#setInitializationCompleteListener");
if (result) callback?.call();
}



/// To send a simple notification to another user using his/her android Id
static Future<void> sendNotificationToUser(String androidId, String title, String content) async => _channel.invokeMethod("Pushe#sendNotificationToUser", {"androidId":androidId, "title":title, "content":content});
static Future<void> sendNotificationToUser(
String androidId, String title, String content) async =>
_channel.invokeMethod("Pushe#sendNotificationToUser",
{"androidId": androidId, "title": title, "content": content});

///Send an event
///[name] is the name of event that wants to send
static Future<void> sendEvent(String name) async => _channel.invokeMethod("Pushe#sendEvent", {"name":name});
static Future<void> sendEvent(String name) async =>
_channel.invokeMethod("Pushe#sendEvent", {"name": name});

/// Send ecommerce data.
/// [name] is the name of ecommerce data
/// [price] is the value of ecommerce name
static Future<void> sendEcommerceData(String name,double price) async => _channel.invokeMethod("Pushe#sendEcommerceData", {"name":name,"price":price});

static Future<void> sendEcommerceData(String name, double price) async =>
_channel.invokeMethod(
"Pushe#sendEcommerceData", {"name": name, "price": price});

/// Set callbacks for different types of events for notifications (in foreground or when app is open in the background)
/// [onReceived] is called when notification was received.
Expand All @@ -134,14 +154,13 @@ class Pushe {
/// [applicationOverridden] : If you have added [android:name="co.pushe.plus.flutter.PusheApplication"] to your AndroidManifest application attribute,
/// the callbacks will be callable since user starts the app. But if not, callbacks will be available when you call [setNotificationListener] and before that callbacks won't work.
/// This doesn't make so much difference. But in future case when Flutter added background fcm support, this can make difference.
static setNotificationListener({
Function(NotificationData) onReceived,
Function(NotificationData) onClicked,
Function(NotificationData) onDismissed,
Function(NotificationData, NotificationButtonData) onButtonClicked,
Function(String) onCustomContentReceived,
bool applicationOverridden: false
}) {
static setNotificationListener(
{Function(NotificationData) onReceived,
Function(NotificationData) onClicked,
Function(NotificationData) onDismissed,
Function(NotificationData, NotificationButtonData) onButtonClicked,
Function(String) onCustomContentReceived,
bool applicationOverridden: false}) {
_receiveCallback = onReceived;
_clickCallback = onClicked;
_dismissCallback = onDismissed;
Expand All @@ -153,25 +172,25 @@ class Pushe {
}
}


///
/// If a method was called from native code through channel this will handle it.
///
static Future<Null> _handleMethod(MethodCall call) async {
if (call.method == 'Pushe#onNotificationReceived') {
_receiveCallback?.call(NotificationData.fromJson(call.arguments));
} else if (call.method == 'Pushe#onNotificationClicked') {
_clickCallback?.call(NotificationData.fromJson(call.arguments));
_clickCallback?.call(NotificationData.fromJson(call.arguments));
} else if (call.method == 'Pushe#onNotificationButtonClicked') {
try {
var parts = call.arguments.toString().split("|||");
_buttonClickCallback?.call(NotificationData.fromJson(parts[0]), NotificationButtonData.fromJsonString(parts[1]));
} catch(e) {}
_buttonClickCallback?.call(NotificationData.fromJson(parts[0]),
NotificationButtonData.fromJsonString(parts[1]));
} catch (e) {}
} else if (call.method == 'Pushe#onNotificationCustomContentReceived') {
try {
var customContent = jsonDecode(call.arguments['json']);
_customContentCallback?.call(customContent);
} catch(e) {}
} catch (e) {}
} else if (call.method == 'Pushe#onNotificationDismissed') {
_dismissCallback?.call(NotificationData.fromJson(call.arguments));
}
Expand All @@ -184,43 +203,69 @@ class Pushe {
/// When a notification event happens (like Receive), callbacks will hold instances of this class.
///
class NotificationData {
String _title, _content, _bigTitle, _bigContent, _summary, _imageUrl, _iconUrl, _customContent;
String _title,
_content,
_bigTitle,
_bigContent,
_summary,
_imageUrl,
_iconUrl,
_customContent;
List<NotificationButtonData> _buttons;

NotificationData._();

NotificationData.create(this._title, this._content, this._bigTitle,
this._bigContent, this._summary, this._imageUrl, this._iconUrl,
this._customContent, this._buttons);
NotificationData.create(
this._title,
this._content,
this._bigTitle,
this._bigContent,
this._summary,
this._imageUrl,
this._iconUrl,
this._customContent,
this._buttons);

static NotificationData fromJson(String json) {
try {
var data = jsonDecode(json);
return NotificationData.create(
data['title'], data['content'],
data['bigTitle'], data['bigContent'],
data['summary'], data['imageUrl'],
data['iconUrl'], data['json'],
data['title'],
data['content'],
data['bigTitle'],
data['bigContent'],
data['summary'],
data['imageUrl'],
data['iconUrl'],
data['json'],
NotificationButtonData.fromJsonList(data['buttons']));
} catch(e) {
} catch (e) {
print('Error getting notification data from json\nError:$e\nJson:$json');
return null;
}
}

@override
String toString() => 'NotificationData{_title: $_title, _content: $_content, _bigTitle: $_bigTitle, _bigContent: $_bigContent, _summary: $_summary, _imageUrl: $_imageUrl, _iconUrl: $_iconUrl, _customContent: $_customContent, buttons: $_buttons}';
String toString() =>
'NotificationData{_title: $_title, _content: $_content, _bigTitle: $_bigTitle, _bigContent: $_bigContent, _summary: $_summary, _imageUrl: $_imageUrl, _iconUrl: $_iconUrl, _customContent: $_customContent, buttons: $_buttons}';

get customContent => _customContent;

get iconUrl => _iconUrl;

get imageUrl => _imageUrl;

get summary => _summary;

get bigContent => _bigContent;

get bigTitle => _bigTitle;

get content => _content;

get title => _title;
get buttons => _buttons;

get buttons => _buttons;
}

///
Expand All @@ -232,9 +277,11 @@ class NotificationButtonData {
int _id;

NotificationButtonData._();

NotificationButtonData.create(this._text, this._id);

int get id => _id;

String get text => _text;

@override
Expand All @@ -259,12 +306,10 @@ class NotificationButtonData {
return fromMap(item);
});
return result.toList();
} catch(e) {
print('Error getting button list from notification\nError:$e\nJson:$json');
} catch (e) {
print(
'Error getting button list from notification\nError:$e\nJson:$json');
return null;
}
}



}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pushe_flutter
description: Official pushe.co plugin for flutter
version: 2.0.0
description: Pushe push notification SDK implementation for Flutter framework, for Android and iOS
version: 2.0.1
authors:
- Pushe <mahdi.malvandi@pushe.co>
homepage: https://pushe.co
Expand Down

0 comments on commit 6751051

Please sign in to comment.