import UserNotificationsimport OneSignalExtensionclass NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var receivedRequest: UNNotificationRequest! var bestAttemptContent: UNMutableNotificationContent? // Note this extension only runs when `mutable_content` is set // Setting an attachment or action buttons automatically sets the property to true 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// print("Running NotificationServiceExtension")// bestAttemptContent.body = "[Modified] " + bestAttemptContent.body OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler) } } override func serviceExtensionTimeWillExpire() { // 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) } }}
#import <OneSignalExtension/OneSignalExtension.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// Note, this extension only runs when mutable-content is set// Setting an attachment or action buttons automatically adds this- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.receivedRequest = request; self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // DEBUGGING: Uncomment the 2 lines below and comment out the one above to ensure this extension is executing// NSLog(@"Running NotificationServiceExtension");// self.bestAttemptContent.body = [@"[Modified] " stringByAppendingString:self.bestAttemptContent.body]; [OneSignalExtension didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent withContentHandler:self.contentHandler];}- (void)serviceExtensionTimeWillExpire { // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. [OneSignalExtension serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent]; self.contentHandler(self.bestAttemptContent);}@end
# If platform is uncommented, set to the same value as your minimum deployment target in Xcode # platform :ios, '15.0' target 'your_project_name' do pod 'OneSignal/OneSignal', '>= 5.2.9', '< 6.0' # If your app does not use In-App messages, you can remove this: pod 'OneSignal/OneSignalInAppMessages', '>= 5.2.9', '< 6.0' # If your app does not use CoreLocation, you can remove this: pod 'OneSignal/OneSignalLocation', '>= 5.2.9', '< 6.0' # Your other pods here end target 'OneSignalNotificationServiceExtension' do pod 'OneSignal/OneSignal', '>= 5.2.9', '< 6.0' end
依存関係をメインアプリターゲットとOneSignalNotificationServiceExtensionターゲットに追加します。platformのコメントが解除されている場合は、Xcodeの最小デプロイメントターゲットと同じ値であることを確認してください。次のコマンドを実行して、OneSignal iOS SDK podをプルダウンしてプロジェクトに追加します:pod repo update && pod installインストールが完了したら、プロジェクトにちなんで名付けられたXCWorkspaceファイル(例:<project-name>.xcworkspace)を開いてください
import SwiftUIimport OneSignalFramework@mainstruct YOURAPP_NAME: App { //Connect the SwiftUI app to the UIKit app delegate @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } }}class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { // Enable verbose logging for debugging (remove in production) OneSignal.Debug.setLogLevel(.LL_VERBOSE) // Initialize with your OneSignal App ID OneSignal.initialize("YOUR_APP_ID", withLaunchOptions: launchOptions) // Use this method to prompt for push notifications. // We recommend removing this method after testing and instead use In-App Messages to prompt for notification permission. OneSignal.Notifications.requestPermission({ accepted in print("User accepted notifications: \(accepted)") }, fallbackToSettings: false) return true }}
//AppDelegate.swiftimport UIKitimport OneSignalFramework@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Enable verbose logging for debugging (remove in production) OneSignal.Debug.setLogLevel(.LL_VERBOSE) // Initialize with your OneSignal App ID OneSignal.initialize("YOUR_APP_ID", withLaunchOptions: launchOptions) // Use this method to prompt for push notifications. // We recommend removing this method after testing and instead use In-App Messages to prompt for notification permission. OneSignal.Notifications.requestPermission({ accepted in print("User accepted notifications: \(accepted)") }, fallbackToSettings: false) return true}// Remaining contents of your AppDelegate Class...}
#import <OneSignalFramework/OneSignalFramework.h>@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Enable verbose logging for debugging (remove in production) [OneSignal.Debug setLogLevel:ONE_S_LL_VERBOSE]; // Initialize with your OneSignal App ID [OneSignal initialize:@"YOUR_APP_ID" withLaunchOptions:launchOptions]; // Use this method to prompt for push notifications. // We recommend removing this method after testing and instead use In-App Messages to prompt for notification permission. [OneSignal.Notifications requestPermission:^(BOOL accepted) { NSLog(@"User accepted notifications: %d", accepted); } fallbackToSettings:false]; // Login your customer with externalId // [OneSignal login:@"EXTERNAL_ID"]; return YES;}