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

[Feedback]: Update Migration file #1778

Open
1 task done
IsaacIsrael opened this issue Jan 2, 2025 · 2 comments
Open
1 task done

[Feedback]: Update Migration file #1778

IsaacIsrael opened this issue Jan 2, 2025 · 2 comments

Comments

@IsaacIsrael
Copy link

IsaacIsrael commented Jan 2, 2025

What's on your mind?

Hey, I was following the migration file, and I think one step is missing: we should update the OneSignalNotificationServiceExtension file with this code.

import UserNotifications

import OneSignalExtension

class NotificationService: UNNotificationServiceExtension {
    
    var contentHandler: ((UNNotificationContent) -> Void)?
    var receivedRequest: UNNotificationRequest!
    var bestAttemptContent: UNMutableNotificationContent?
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.receivedRequest = request
        self.contentHandler = contentHandler
        self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            /* DEBUGGING: Uncomment the 2 lines below to check this extension is executing
                          Note, this extension only runs when mutable-content is set
                          Setting an attachment or action buttons automatically adds this */
            // print("Running NotificationServiceExtension")
            // bestAttemptContent.body = "[Modified] " + bestAttemptContent.body
            
            OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)
        }
    }
    
    override func 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.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            OneSignalExtension.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
            contentHandler(bestAttemptContent)
        }
    }  
}

Once the old version imported OneSignal, it is now required to import OneSignalExtension.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@PirunSeng
Copy link

Thanks, a good one to add. Could you share updates on NotificationServiceExtension.java in Android app build gradle please?

@nan-li
Copy link
Contributor

nan-li commented Feb 10, 2025

Hi @IsaacIsrael and @PirunSeng thank you for the feedback, we will update the migration guide with information for the service extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants