Skip to content
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

Get correct Data value from OneSignal.getDeviceState().pushToken which is a String #1069

Closed
JayMehta97 opened this issue Apr 5, 2022 · 7 comments

Comments

@JayMehta97
Copy link

Description:

We are adding another 3rd party SDK for chat which provides PN for chat specific features. This SDK also requires deviceToken to setup remote push notifications. So in our current users device we have already taken remote notification access using OneSignal SDK. How do I give deviceToken to the new SDK using OneSignal.getDeviceState().pushToken? Can I convert the pushToken (String) to correct deviceToken (Data)?

Environment

iOS version - 14.8.1
OneSignal SDK version - 3.5.3
Using OneSignal SDK via SPM

@emawby
Copy link
Contributor

emawby commented Apr 5, 2022

@JayMehta97 Hello thank you for reaching out. I recommend implementing the following method in your AppDelegate:
application:didRegisterForRemoteNotificationsWithDeviceToken:

This will be called after the user accepts the push prompt and provides the deviceToken which you can then forward to the other SDK.

@JayMehta97
Copy link
Author

I tried with that method but the application:didRegisterForRemoteNotificationsWithDeviceToken: is not getting called if I just add the Oneginal SDK using SPM. If I remove that didRegisterForRemoteNotificationsWithDeviceToken is being called but adding it back again doesn't call this method.

@emawby
Copy link
Contributor

emawby commented Apr 11, 2022

Odd that method is called for me when using the XCFramework repo and Swift Package Manager. Are you able to link an example project that reproduces the issue?

@Sml995
Copy link

Sml995 commented Jun 13, 2022

Hi, we're facing the same issue here with SwiftUI

TLDR;
I suspect this to be relative to swizzling issues addressed with #1091
Do you know when this will be released ?

There's an easy way to reproduce the bug:

  • Start a fresh SwiftUI project
  • Add OneSignal-XCFramework via SPM - v3.11.1
  • Add Push notifications capability
  • Adjust your root App struct as following:
@main
struct Test_Intercom_OneSignalApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
  • Add a file named AppDelegate.swift to your main target with the following content:
import SwiftUI
import OneSignal
import Foundation

class AppDelegate: NSObject, UIApplicationDelegate {
        
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        //MARK: OneSignal initialization
        OneSignal.setLogLevel(.LL_DEBUG, visualLevel: .LL_NONE)
        OneSignal.initWithLaunchOptions(launchOptions)
        OneSignal.setAppId("your_onesignal_app_id")
        
        OneSignal.setNotificationOpenedHandler( { openedEvent in
            //
        })
        
        OneSignal.promptForPushNotifications(userResponse: { accepted in
            //
        })
        
        return true
        
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        //
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //
    }
   
}
  • Place a breakpoint within didFailToRegisterForRemoteNotificationsWithError & didRegisterForRemoteNotificationsWithDeviceToken and run the app

@jkasten2
Copy link
Member

@Sml995 Thanks for the details and digging in. I believe you are correct that PR #1091 will fix the issue with your AppDelegate didFailToRegisterForRemoteNotificationsWithError and didRegisterForRemoteNotificationsWithDeviceToken not firing. We will reply back when we have a release available with this fix.

@Sml995
Copy link

Sml995 commented Jun 14, 2022

Thanks for the quick feedback @jkasten2, the sooner the better ahah !

@Sml995
Copy link

Sml995 commented Jun 20, 2022

Getting back on this after release v3.11.2
Everything is running as expected now :)

@emawby emawby closed this as completed Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants