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

[question]: OneSignal Notification image doesnt appear #1414

Open
1 task done
wesamodeh opened this issue Apr 21, 2024 · 1 comment
Open
1 task done

[question]: OneSignal Notification image doesnt appear #1414

wesamodeh opened this issue Apr 21, 2024 · 1 comment

Comments

@wesamodeh
Copy link

wesamodeh commented Apr 21, 2024

How can we help?

i implemented the OneSignalNotificationServiceExtenstion but when i send a notification includes image didnt appear , can u help ?

NotificationService 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 {
            OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)
            
            // Handle attachment
            if let attachments = bestAttemptContent.userInfo["attachments"] as? [[String: Any]] {
                if let attachmentURLString = attachments.first?["url"] as? String, let attachmentURL = URL(string: attachmentURLString) {
                    do {
                        let attachment = try UNNotificationAttachment(identifier: "image", url: attachmentURL, options: nil)
                        bestAttemptContent.attachments = [attachment]
                    } catch {
                        print("Error creating attachment: \(error.localizedDescription)")
                    }
                }
            }
        }
    }
    
    // Function to download attachment data
    private func downloadAttachment(from urlString: String) -> URL? {
        if let url = URL(string: urlString) {
            do {
                let data = try Data(contentsOf: url)
                let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(url.lastPathComponent)
                try data.write(to: tempURL)
                return tempURL
            } catch {
                print("Error downloading attachment: \(error.localizedDescription)")
                return nil
            }
        }
        return nil
    }
    
    override func serviceExtensionTimeWillExpire() {
        if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
            OneSignalExtension.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
            contentHandler(bestAttemptContent)
        }
    }
}

AppDelegate code :

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <Firebase.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>



@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 [FIRApp configure];
 self.moduleName = @"newVersion";


[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (!granted && error == nil) {
       dispatch_async(dispatch_get_main_queue(), ^{
           [[UIApplication sharedApplication] registerForRemoteNotifications];
       });
   }
}];

 self.initialProps = @{};

 return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
 return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
 return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end

PodFile code

target 'OneSignalNotificationServiceExtension' do
  pod 'OneSignalXCFramework', '3.12.9'
end

Code of Conduct

  • I agree to follow this project's Code of Conduct
@nan-li
Copy link
Contributor

nan-li commented May 13, 2024

Hi @wesamodeh, I don't see anything obvious that could be a problem.

I would recommend first checking that the OneSignalNotificationServiceExtenstion is actually running by adding these two lines to your code:

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

If the OneSignalNotificationServiceExtenstion runs, you will see your notifications have the word "[Modified] ".

If the OneSignalNotificationServiceExtenstion runs but you are not able to see the image, then something else is the issue, please let us know about the first step.

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

2 participants