Skip to content

Fix handlers #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public class OneSignalPlugin
OneSignal.OSNotificationWillShowInForegroundHandler {

/** Plugin registration. */
private OSNotificationOpenedResult coldStartNotificationResult;
private OSInAppMessageAction inAppMessageClickedResult;
private boolean hasSetNotificationOpenedHandler = false;
private boolean hasSetInAppMessageClickedHandler = false;
private boolean hasSetNotificationWillShowInForegroundHandler = false;
private boolean hasSetRequiresPrivacyConsent = false;
Expand Down Expand Up @@ -140,10 +138,11 @@ private void setAppId(MethodCall call, Result reply) {
String appId = call.argument("appId");
Context context = flutterRegistrar.activeContext();

OneSignal.setInAppMessageClickHandler(this);
OneSignal.initWithContext(context);
OneSignal.setAppId(appId);

if (hasSetRequiresPrivacyConsent)
if (hasSetRequiresPrivacyConsent && !OneSignal.userProvidedPrivacyConsent())
this.waitingForUserPrivacyConsent = true;
else
this.addObservers();
Expand Down Expand Up @@ -335,11 +334,7 @@ public void onFailure(OneSignal.ExternalIdError error) {
}

private void initNotificationOpenedHandlerParams() {
this.hasSetNotificationOpenedHandler = true;
if (this.coldStartNotificationResult != null) {
this.notificationOpened(this.coldStartNotificationResult);
this.coldStartNotificationResult = null;
}
OneSignal.setNotificationOpenedHandler(this);
}

private void initInAppMessageClickedHandlerParams() {
Expand Down Expand Up @@ -401,11 +396,6 @@ public void onOSPermissionChanged(OSPermissionStateChanges stateChanges) {

@Override
public void notificationOpened(OSNotificationOpenedResult result) {
if (!this.hasSetNotificationOpenedHandler) {
this.coldStartNotificationResult = result;
return;
}

try {
invokeMethodOnUiThread("OneSignal#handleOpenedNotification", OneSignalSerializer.convertNotificationOpenResultToMap(result));
} catch (JSONException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static HashMap<String, Object> convertNotificationToMap(OSNotification notificat

hash.put("androidNotificationId", notification.getAndroidNotificationId());

if (notification.getGroupedNotifications() != null) {
if (notification.getGroupedNotifications() != null && !notification.getGroupedNotifications().isEmpty()) {
JSONArray payloadJsonArray = new JSONArray();
for (OSNotification groupedNotification : notification.getGroupedNotifications())
payloadJsonArray.put(groupedNotification.toJSONObject());
Expand Down
31 changes: 14 additions & 17 deletions ios/Classes/OneSignalPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,9 @@ it will add the observers (ie. subscription)
*/
@property (atomic) BOOL waitingForUserConsent;

@property (atomic) BOOL hasSetInAppMessageClickedHandler;
@property (atomic) BOOL hasSetNotificationWillShowInForegroundHandler;

/*
Holds reference to any notifications received before the
flutter runtime channel has been opened
Thus, if a user taps a notification while the app is
terminated, the SDK will still notify the app once the
channel is open
*/
@property (strong, nonatomic) OSNotificationOpenedResult *coldStartOpenResult;

/*
Holds reference to any in app messages received before any click action
occurs on the body, button or image elements of the in app message
Expand All @@ -76,13 +68,16 @@ + (instancetype)sharedInstance {
sharedInstance.waitingForUserConsent = false;
sharedInstance.receivedNotificationCache = [NSMutableDictionary new];;
sharedInstance.notificationCompletionCache = [NSMutableDictionary new];;
sharedInstance.hasSetInAppMessageClickedHandler = false;
sharedInstance.hasSetNotificationWillShowInForegroundHandler = false;
});
return sharedInstance;
}

#pragma mark FlutterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

[OneSignal initWithLaunchOptions:nil];
[OneSignal setMSDKType:@"flutter"];

// Wrapper SDK's call init with no app ID early on in the
Expand All @@ -104,9 +99,6 @@ - (void)addObservers {
[OneSignal addSubscriptionObserver:self];
[OneSignal addPermissionObserver:self];
[OneSignal addEmailSubscriptionObserver:self];
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
[OneSignalPlugin.sharedInstance handleNotificationOpened:result];
}];
[OneSignal setNotificationWillShowInForegroundHandler:^(OSNotification *notification, OSNotificationDisplayResponse completion) {
[OneSignalPlugin.sharedInstance handleNotificationWillShowInForeground:notification completion:completion];
}];
Expand Down Expand Up @@ -162,7 +154,6 @@ - (void)setAppId:(FlutterMethodCall *)call withResult:(FlutterResult)result {
[self handleInAppMessageClicked:action];
}];

[OneSignal initWithLaunchOptions:nil];
[OneSignal setAppId:call.arguments[@"appId"]];

// If the user has required privacy consent, the SDK will not
Expand Down Expand Up @@ -300,13 +291,14 @@ - (void)removeExternalUserId:(FlutterMethodCall *)call withResult:(FlutterResult
}

- (void)initNotificationOpenedHandlerParams {
if (self.coldStartOpenResult) {
[self handleNotificationOpened:self.coldStartOpenResult];
self.coldStartOpenResult = nil;
}
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
[OneSignalPlugin.sharedInstance handleNotificationOpened:result];
}];
}

- (void)initInAppMessageClickedHandlerParams {
_hasSetInAppMessageClickedHandler = true;

if (self.inAppMessageClickedResult) {
[self handleInAppMessageClicked:self.inAppMessageClickedResult];
self.inAppMessageClickedResult = nil;
Expand Down Expand Up @@ -358,6 +350,11 @@ - (void)completeNotification:(FlutterMethodCall *)call withResult:(FlutterResult

#pragma mark In App Message Click Handler
- (void)handleInAppMessageClicked:(OSInAppMessageAction *)action {
if (!self.hasSetInAppMessageClickedHandler) {
_inAppMessageClickedResult = action;
return;
}

[self.channel invokeMethod:@"OneSignal#handleClickedInAppMessage" arguments:action.toJson];
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ class OSNotificationAction {

/// The ID of the button on your notification
/// that the user tapped
late String actionId;
String? actionId;

OSNotificationAction(Map<String, dynamic> json) {
this.type = OSNotificationActionType.opened;
this.actionId = json['id'] as String;
this.actionId = json['id'] as String?;

if (json.containsKey('type'))
this.type = OSNotificationActionType.values[json['type'] as int];
Expand Down