Skip to content

Commit cfba13b

Browse files
committed
Adding pushSubscription state and notification permission state as properties, adding lifecycleInit to iOS class
1 parent b95d480 commit cfba13b

File tree

6 files changed

+33
-26
lines changed

6 files changed

+33
-26
lines changed

android/src/main/java/com/onesignal/flutter/OneSignalNotifications.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void registerWith(BinaryMessenger messenger) {
5252

5353
@Override
5454
public void onMethodCall(MethodCall call, Result result) {
55-
if (call.method.contentEquals("OneSignal#getPermission"))
55+
if (call.method.contentEquals("OneSignal#permission"))
5656
replySuccess(result, OneSignal.getNotifications().getPermission());
5757
else if (call.method.contentEquals("OneSignal#requestPermission"))
5858
this.requestPermission(call, result);

example/lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class _MyAppState extends State<MyApp> with OneSignalPushSubscriptionObserver {
118118

119119
void onOSPushSubscriptionChangedWithStateChanges(
120120
OSPushSubscriptionStateChanges stateChanges) {
121+
print(OneSignal.User.pushSubscription.optedIn());
122+
print(OneSignal.User.pushSubscription.id());
123+
print(OneSignal.User.pushSubscription.token());
121124
print(stateChanges.jsonRepresentation());
122125
}
123126

ios/Classes/OSFlutterPushSubscription.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
5353
[self optIn:call withResult:result];
5454
else if ([@"OneSignal#optOut" isEqualToString:call.method])
5555
[self optOut:call withResult:result];
56-
else if ([@"OneSignal#addObserver" isEqualToString:call.method])
57-
[self addObserver:call withResult:result];
58-
else if ([@"OneSignal#removeObserver" isEqualToString:call.method])
56+
else if ([@"OneSignal#lifecycleInit" isEqualToString:call.method])
5957
[self removeObserver:call withResult:result];
6058
else
6159
result(FlutterMethodNotImplemented);
@@ -71,17 +69,11 @@ - (void)optOut:(FlutterMethodCall *)call withResult:(FlutterResult)result {
7169
result(nil);
7270
}
7371

74-
- (void)addObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result {
72+
- (void)lifecycleInit:(FlutterMethodCall *)call withResult:(FlutterResult)result {
7573
[OneSignal.User.pushSubscription addObserver:self];
7674
result(nil);
7775
}
7876

79-
// TODO: possibly don't need
80-
- (void)removeObserver:(FlutterMethodCall *)call withResult:(FlutterResult)result {
81-
[OneSignal.User.pushSubscription removeObserver:self];
82-
result(nil);
83-
}
84-
8577
- (void)onOSPushSubscriptionChangedWithStateChanges:(OSPushSubscriptionStateChanges*)stateChanges {
8678
[self.channel invokeMethod:@"OneSignal#pushSubscriptionChanged" arguments:stateChanges.jsonRepresentation];
8779
}

lib/src/inappmessages.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class OneSignalInAppMessages {
6464
return await _channel.invokeMethod("OneSignal#arePaused");
6565
}
6666

67-
Future<bool> lifecycleInit() async {
67+
Future<void> lifecycleInit() async {
6868
return await _channel.invokeMethod("OneSignal#lifecycleInit");
6969
}
7070

lib/src/notifications.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ class OneSignalNotifications {
2525
this._channel.setMethodCallHandler(_handleMethod);
2626
}
2727

28+
bool _permission = false;
29+
2830
/// Whether this app has push notification permission.
29-
Future<bool> permission() async {
30-
return await _channel.invokeMethod("OneSignal#permission");
31+
bool permission() {
32+
return _permission;
3133
}
3234

3335
/// Whether attempting to request notification permission will show a prompt.
@@ -80,7 +82,8 @@ class OneSignalNotifications {
8082
_observers.remove(observer);
8183
}
8284

83-
Future<bool> lifecycleInit() async {
85+
Future<void> lifecycleInit() async {
86+
_permission = await _channel.invokeMethod("OneSignal#permission");
8487
return await _channel.invokeMethod("OneSignal#lifecycleInit");
8588
}
8689

@@ -102,6 +105,7 @@ class OneSignalNotifications {
102105
}
103106

104107
Future<void> onOSPermissionChangedHandler(OSPermissionState state) async {
108+
_permission = state.permission;
105109
for (var observer in _observers) {
106110
observer.onOSPermissionChanged(state);
107111
}

lib/src/pushsubscription.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:ffi';
23
import 'dart:io' show Platform;
34
import 'package:flutter/services.dart';
45
import 'package:onesignal_flutter/src/defines.dart';
@@ -7,6 +8,10 @@ import 'package:onesignal_flutter/src/subscription.dart';
78
class OneSignalPushSubscription {
89
MethodChannel _channel = const MethodChannel('OneSignal#pushsubscription');
910

11+
String? _id;
12+
String? _token;
13+
bool? _optedIn;
14+
1015
List<OneSignalPushSubscriptionObserver> _observers =
1116
<OneSignalPushSubscriptionObserver>[];
1217
// constructor method
@@ -19,26 +24,22 @@ class OneSignalPushSubscription {
1924
_channel.invokeMethod("OneSignal#addObserver");
2025
}
2126

22-
// TODO: convert these syncronous by capturing an initial state and the following the stateChanges
23-
24-
/// The readonly push subscription ID.
25-
Future<String> id() async {
26-
return await _channel.invokeMethod("OneSignal#pushSubscriptionToken");
27+
String? id() {
28+
return this._id;
2729
}
2830

2931
/// The readonly push token.
30-
Future<String> token() async {
31-
return await _channel.invokeMethod("OneSignal#pushSubscriptionToken");
32+
String? token() {
33+
return this._token;
3234
}
3335

3436
/// Gets a boolean value indicating whether the current user is opted in to push notifications.
3537
/// This returns true when the app has notifications permission and optedOut is called.
3638
/// Note: Does not take into account the existence of the subscription ID and push token.
3739
/// This boolean may return true but push notifications may still not be received by the user.
38-
Future<bool> optedIn() async {
39-
return await _channel.invokeMethod("OneSignal#pushSubscriptionOptedIn");
40+
bool? optedIn() {
41+
return _optedIn;
4042
}
41-
// TODO: END
4243

4344
/// Call this method to receive push notifications on the device or to resume receiving of
4445
/// push notifications after calling optOut. If needed, this method will prompt the user for
@@ -65,7 +66,10 @@ class OneSignalPushSubscription {
6566
_observers.remove(observer);
6667
}
6768

68-
Future<bool> lifecycleInit() async {
69+
Future<void> lifecycleInit() async {
70+
_token = await _channel.invokeMethod("OneSignal#pushSubscriptionToken");
71+
_id = await _channel.invokeMethod("OneSignal#pushSubscriptionId");
72+
_optedIn = await _channel.invokeMethod("OneSignal#pushSubscriptionOptedIn");
6973
return await _channel.invokeMethod("OneSignal#lifecycleInit");
7074
}
7175

@@ -80,6 +84,10 @@ class OneSignalPushSubscription {
8084

8185
Future<void> _onSubscriptionChangedHandler(
8286
OSPushSubscriptionStateChanges stateChanges) async {
87+
this._id = stateChanges.to.id;
88+
this._token = stateChanges.to.token;
89+
this._optedIn = stateChanges.to.optedIn;
90+
8391
for (var observer in _observers) {
8492
observer.onOSPushSubscriptionChangedWithStateChanges(stateChanges);
8593
}

0 commit comments

Comments
 (0)