Skip to content
This repository was archived by the owner on Aug 6, 2023. It is now read-only.
This repository was archived by the owner on Aug 6, 2023. It is now read-only.

Usage with service extension #12

@vishalnarkhede

Description

@vishalnarkhede

Hey @artemyarulin , I am trying to use this library with UserNotification service extension.


#import "NotificationService.h"
#import "React/RCTBridge.h"
#import <React/RCTLog.h>
#import "RNMEvaluator.h"

@interface NotificationService ()

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

@end

@implementation NotificationService{
  __weak RCTBridge *_bridge;
}

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    RCTBridge* bridge = [[RCTBridge alloc] initWithBundleURL:[NSURL URLWithString:@"index"]
    moduleProvider:nil
    launchOptions:nil];
    [RNMEvaluator callSyncFunction:bridge
      name:@"Math.pow"
      args:@[@2,@2]
        cb:^(NSString *error, id returnValue) {
            if (error)
                NSLog(@"Error occured: %@", error);
            else
                NSLog(@"Function returned: %@", returnValue);
        }];

    // I want the callback to be executed first before executing further code.
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [Modified]", 
    self.bestAttemptContent.title];

    self.contentHandler(self.bestAttemptContent);
}

- (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.
    self.contentHandler(self.bestAttemptContent);
}

@end

Although can I somehow wait for cb (or callback) before proceeding in code? I mean NSLog(@"Error occured: %@", error); should be executed before I try to assign value to self.bestAttemptContent.title

P.S. I am objective-c nube!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions