OneSignal-Flutter-SDK icon indicating copy to clipboard operation
OneSignal-Flutter-SDK copied to clipboard

[Feedback]: Migration guide from 3.5.1 to 5.0.0

Open Fudal opened this issue 2 years ago • 31 comments
trafficstars

What's on your mind?

We need migration guide from 3.5.1 to 5.0.0.

A lot of methods have been added or changed. The version jump itself testifies to this, but no migration guide has been presented to us, so users are confused.

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Fudal avatar Aug 21 '23 12:08 Fudal

Here is the migration guide:

https://github.com/OneSignal/OneSignal-Flutter-SDK/blob/user_model/main/MIGRATION_GUIDE.md

petodavid avatar Aug 22 '23 08:08 petodavid

@petodavid okay, thanks. But I can't see anywhere what should I replace these methods:

  1. OneSignal.shared.setExternalUserId(userId)
  2. OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event)
  3. OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result)
  4. OneSignal.shared.promptUserForPushNotificationPermission()

Fudal avatar Aug 22 '23 08:08 Fudal

Please update docs and example

target 'OneSignalNotificationServiceExtension' do
  use_frameworks!
  pod 'OneSignalXCFramework', '5.0.1'
end

is necessary for iOS to get Pod installed

xunreal75 avatar Aug 22 '23 15:08 xunreal75

Thank you for the feedback we will work on clarifying the migration steps!

emawby avatar Aug 22 '23 17:08 emawby

@petodavid okay, thanks. But I can't see anywhere what should I replace these methods:

  1. OneSignal.shared.setExternalUserId(userId)
  2. OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event)
  3. OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result)
  4. OneSignal.shared.promptUserForPushNotificationPermission()

I have the same case like yours, did you find any solution ?

wahyu-handayani avatar Sep 01 '23 03:09 wahyu-handayani

@wahyu-handayani no, I'm still using 3.5.1 version

Fudal avatar Sep 01 '23 06:09 Fudal

@Fudal @wahyu-handayani The solution from me are change OneSignal.shared.setNotificationOpenedHandler(_handleNotificationOpened) to OneSignal.Notifications.addClickListener(_handleNotificationOpened)

change _handleNotificationOpened(OSNotificationOpenedResult result) to _handleNotificationOpened(OSNotificationClickEvent result)

huszm avatar Sep 02 '23 09:09 huszm

Hi this should help you to get started Onesignal V5.0.0

  if (kDebugMode) {
        OneSignal.Debug.setAlertLevel(OSLogLevel.none);
        OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
      } else {
        OneSignal.Debug.setAlertLevel(OSLogLevel.none);
        OneSignal.Debug.setLogLevel(OSLogLevel.none);
      }

      OneSignal.initialize(oneSignalAppId); //init with your appid
      OneSignal.Location.setShared(false); //avoid location sharing

      OneSignal.Notifications.addForegroundWillDisplayListener((event) {
      });

      OneSignal.Notifications.addPermissionObserver((state) {
         //log.i(text: 'Accepted Onesignal permission: $state');
      });

      OneSignal.InAppMessages.addWillDisplayListener((event) {
        //log.i(text: 'inAppMessage ${event.message}');
      });


      OneSignal.Notifications.addClickListener((event) {
        //var json = action.jsonRepresentation();
        var clickName = event.result.actionId;
        var clickURL = event.notification.launchUrl;
        var title = event.notification.title;
        var body = event.notification.body;
        ...
        }

xunreal75 avatar Sep 02 '23 19:09 xunreal75

@wahyu-handayani no, I'm still using 3.5.1 version

I try to install onesignal using this version, may I take a look your Podfile and NotificationService.m (inside ios/OneSignalNotificationServiceExtension/ folder) ? because I always failed when debugging in ios

wahyu-handayani avatar Sep 06 '23 04:09 wahyu-handayani

@wahyu-handayani

Sure, here is NotificationService.m:

//
//  NotificationService.m
//  OneSignalNotificationServiceExtension
//
//  Created by Brad Hesse on 7/13/18.
//  Copyright © 2018 The Chromium Authors. All rights reserved.
//

#import <OneSignal/OneSignal.h>

#import "NotificationService.h"

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNNotificationRequest *receivedRequest;
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.receivedRequest = request;
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    [OneSignal didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent withContentHandler:self.contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    
    [OneSignal serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];
    
    self.contentHandler(self.bestAttemptContent);
}

@end

here Podfile:

# Uncomment this line to define a global platform for your project
platform :ios, '14.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
  installer.pods_project.targets.each do |target|

   target.build_configurations.each do |config|
        # Here are some configurations automatically generated by flutter
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
          '$(inherited)',

          ## dart: PermissionGroup.calendar
          # 'PERMISSION_EVENTS=0',

          ## dart: PermissionGroup.reminders
          'PERMISSION_REMINDERS=0',

          ## dart: PermissionGroup.contacts
          # 'PERMISSION_CONTACTS=0',

          ## dart: PermissionGroup.camera
          'PERMISSION_CAMERA=1',

          'PERMISSION_ACTIVITY_RECOGNITION=1',

          ## dart: PermissionGroup.microphone
          # 'PERMISSION_MICROPHONE=1',

          ## dart: PermissionGroup.speech
          # 'PERMISSION_SPEECH_RECOGNIZER=0',

          ## dart: PermissionGroup.photos
          'PERMISSION_PHOTOS=0',

          ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
          # 'PERMISSION_LOCATION=0',

          ## dart: PermissionGroup.notification
          'PERMISSION_NOTIFICATIONS=1',

          ## dart: PermissionGroup.mediaLibrary
          'PERMISSION_MEDIA_LIBRARY=0',

          ## dart: PermissionGroup.sensors
          # 'PERMISSION_SENSORS=1',

          ## dart: PermissionGroup.bluetooth
          # 'PERMISSION_BLUETOOTH=0',
        ]
      end
    flutter_additional_ios_build_settings(target)
  end
end

Fudal avatar Sep 06 '23 07:09 Fudal

@wahyu-handayani No problem ;)

I don't have that target Zrzut ekranu 2023-09-6 o 09 20 55

Fudal avatar Sep 06 '23 07:09 Fudal

@wahyu-handayani No problem ;)

I don't have that target Zrzut ekranu 2023-09-6 o 09 20 55

Thank you so so sooooooooo much @Fudal for your help, I am still strugling to fix one error left, your helps really makes my day

wahyu-handayani avatar Sep 06 '23 07:09 wahyu-handayani

  1. Not sure about this OneSignal.shared.setExternalUserId
  2. OneSignal.shared.setNotificationWillShowInForegroundHandler to OneSignal.Notifications.addForegroundWillDisplayListener
  3. OneSignal.shared.setNotificationOpenedHandler to OneSignal.Notifications.addClickListener
  4. OneSignal.shared.promptUserForPushNotificationPermission to await OneSignal.Notifications.requestPermission(true)

I still have problem with android background notification, don't know why NotificationServiceExtension is error.

tovidd avatar Sep 14 '23 12:09 tovidd

Please update migration guide as described on the comment reference. All of these methods are missing without any replacement instruction.

@petodavid okay, thanks. But I can't see anywhere what should I replace these methods:

  1. OneSignal.shared.setExternalUserId(userId)
  2. OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event)
  3. OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result)
  4. OneSignal.shared.promptUserForPushNotificationPermission()

husainazkas avatar Sep 21 '23 09:09 husainazkas

I have yet to get iOS to finish building using this guide or any of the pending PR changes to it.

michael-joseph-payne avatar Sep 22 '23 19:09 michael-joseph-payne

@emawby We are still waiting for more information about right migration from 3.5.1 version.

Fudal avatar Sep 25 '23 08:09 Fudal

Deprecation would be nice first. Throwing braking changes randomly to a product is a weak move NGL.

lukeurban avatar Sep 27 '23 14:09 lukeurban

how can i fix this?

The getter 'shared' isn't defined for the type 'OneSignal'. Undefined class 'OSNotificationOpenedResult'. Undefined class 'OSNotificationReceivedEvent'.

elsegund0 avatar Oct 03 '23 06:10 elsegund0

@emawby or @nan-li can you help us? I didn't find solution in example file.

Fudal avatar Oct 25 '23 09:10 Fudal

How do I disable the foreground notifications in the new SDK? In the old one, it was:

OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
  // Display Notification, send null to not display, send notification to display           
  event.complete(event.notification);      
}); 

In the new SDK event doesn't have the complete func.

rignaneseleo avatar Nov 02 '23 16:11 rignaneseleo

How do I disable the foreground notifications in the new SDK? In the old one, it was:

OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
  // Display Notification, send null to not display, send notification to display           
  event.complete(event.notification);      
}); 

In the new SDK event doesn't have the complete func.

Same problem here, there is event.preventDefault but its completely blocks push notification. I need the app still get notification but not showing in the foreground. Is there any solution please ?

nandakista avatar Nov 10 '23 08:11 nandakista

How to retrieve tags?

hosaysg avatar Dec 11 '23 03:12 hosaysg

Hi all,

I apologize we don't have a direct v3 -> v5 migration guide yet showing the appropriate replacements. All available methods of the major release are outline here.

@tovidd is correct about the methods and the replacement for setExternalUserId is the OneSignal.login("EXT_ID") method.

To address this comment

Deprecation would be nice first. Throwing braking changes randomly to a product is a weak move NGL.

The v3.x.x OneSignal Flutter SDK is still supported, but the major release of v5.x.x does introduce breaking changes if you upgrade.

@rignaneseleo you can call preventDefault to disable foreground notifications. Please see the guide for how to use.

@nandakista Can you tell me more about this issue you are running into?

nan-li avatar Dec 11 '23 04:12 nan-li

How to call event.complete(null) in version 5.0.4? I need the app still get notification but not showing in the foreground.

shanelau avatar Dec 21 '23 11:12 shanelau

Login method is not working instead of setExternalUserId. I don't know what it does but nothing happens. Android/ios. This is important bug

bugrevealingbme avatar Jan 31 '24 10:01 bugrevealingbme

Hi @shanelau I apologize for the delay, we have an example in the migration guide on the Notification Will Display Listener:

OneSignal.Notifications.addForegroundWillDisplayListener((event) {
	/// preventDefault to not display the notification
	event.preventDefault();

	/// Do async work

	/// notification.display() to display after preventing default
	event.notification.display();
});

nan-li avatar Feb 01 '24 08:02 nan-li

Hi @bugrevealingbme,

Can you share more details about Login method is not working?

nan-li avatar Feb 01 '24 08:02 nan-li

Hi @bugrevealingbme,

Can you share more details about Login method is not working?

The problem was with me, I apologize for the misinformation. Externally, how do I check if a notification is clicked when the app is closed? There is not a document about it

bugrevealingbme avatar Feb 01 '24 08:02 bugrevealingbme

Hi @shanelau I apologize for the delay, we have an example in the migration guide on the Notification Will Display Listener:

OneSignal.Notifications.addForegroundWillDisplayListener((event) {
	/// preventDefault to not display the notification
	event.preventDefault();

	/// Do async work

	/// notification.display() to display after preventing default
	event.notification.display();
});

Nice work. It works on onesignal_flutter: ^5.1.0

shanelau avatar Feb 01 '24 09:02 shanelau

event.complete(null)

did u fine a solution?

parvxi avatar Feb 04 '24 16:02 parvxi