OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
609 stars 205 forks source link

[Feedback]: Migration guide from 3.5.1 to 5.0.0 #711

Open Fudal opened 11 months ago

Fudal commented 11 months ago

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

petodavid commented 11 months ago

Here is the migration guide:

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

Fudal commented 11 months ago

@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()
xunreal75 commented 10 months ago

Please update docs and example

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

is necessary for iOS to get Pod installed

emawby commented 10 months ago

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

wahyu-handayani commented 10 months ago

@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 ?

Fudal commented 10 months ago

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

huszm commented 10 months ago

@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)

xunreal75 commented 10 months ago

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;
        ...
        }
wahyu-handayani commented 10 months ago

@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

Fudal commented 10 months ago

@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 commented 10 months ago

@wahyu-handayani No problem ;)

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

wahyu-handayani commented 10 months ago

@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

tovidd commented 10 months ago
  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.

husainazkas commented 10 months ago

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()
michael-joseph-payne commented 9 months ago

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

Fudal commented 9 months ago

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

lukeurban commented 9 months ago

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

elsegund0 commented 9 months ago

how can i fix this?

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

Fudal commented 8 months ago

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

rignaneseleo commented 8 months ago

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.

nandakista commented 8 months ago

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 ?

hosaysg commented 7 months ago

How to retrieve tags?

nan-li commented 7 months ago

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?

shanelau commented 6 months ago

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

bugrevealingbme commented 5 months ago

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

nan-li commented 5 months ago

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 commented 5 months ago

Hi @bugrevealingbme,

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

bugrevealingbme commented 5 months ago

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

shanelau commented 5 months ago

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

Parvxi commented 5 months ago

event.complete(null)

did u fine a solution?

jhandaya commented 2 months ago

Where is postNotification method ?

final notification = OSCreateNotification(
    playerIds: listOneSignalUserId,
    androidSmallIcon: 'ic_stat_one_signal_default',
    androidLargeIcon: 'ic_stat_one_signal_default',
    content: message,
    androidSound: sound,
    heading: title,
    languageCode: 'en',
    iosAttachments: {
      "id1": imgUrlString
    },
    //url: url,
    //bigPicture: imgUrlString,
    buttons: [
      OSActionButton(text: "OK", id: "id1"),
    ]);

var response = await OneSignal.shared.postNotification(notification);

any solution to post a notification from version 5 ?