ios - OneSignal (iOS) - 未调用 didReceiveNotificationRequest

标签 ios objective-c apple-push-notifications onesignal remote-notifications

我遵循了 OneSignal 的设置指南,添加了一个所有名称都正确的扩展,并拥有他们提供的这个文件:

#import <OneSignal/OneSignal.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

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {


    self.receivedRequest = request;
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    [OneSignal didReceiveNotificationExtensionRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];

    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.

    [OneSignal serviceExtensionTimeWillExpireRequest:self.receivedRequest withMutableNotificationContent:self.bestAttemptContent];

    self.contentHandler(self.bestAttemptContent);
}
@end

还复制了 AppDelegate 的所有代码,如此处设置指南中所述: https://documentation.onesignal.com/docs/ios-sdk-setup 加上一些其他代码来创建 github 上有人推荐的“center”变量。在 AppDelegate 中添加此实例确实可以调用“didReceiveRemoteNotification”,但不会调用 didReceiveNotificationExtensionRequest。

#import "AppDelegate.h"

@interface AppDelegate ()
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
@end

#import <OneSignal/OneSignal.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Replace '11111111-2222-3333-4444-0123456789ab' with your OneSignal App ID.
    [OneSignal initWithLaunchOptions:launchOptions
                               appId:@"xxxxxxx (my app id is here)"
            handleNotificationAction:nil
                            settings:@{kOSSettingsKeyAutoPrompt: @false}];
    OneSignal.inFocusDisplayType = OSNotificationDisplayTypeNotification;

    // Recommend moving the below line to prompt for push after informing the user about
    //   how your app will use them.
    [OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
        NSLog(@"User accepted notifications: %d", accepted);
    }];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    // Call syncHashedEmail anywhere in your iOS app if you have the user's email.
    // This improves the effectiveness of OneSignal's "best-time" notification scheduling feature.
    // [OneSignal syncHashedEmail:userEmail];

    return YES;
}

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void
                                                                                                                               (^)(UIBackgroundFetchResult))completionHandler
{
    // iOS 10 will handle notifications through other methods

    if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )
    {
        NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
        // set a member variable to tell the new delegate that this is background

       //this block here gets called when I debug

        return;
    }
    NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );

    // custom code to handle notification content

    if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
    {
        NSLog( @"INACTIVE" );
        completionHandler( UIBackgroundFetchResultNewData );
    }
    else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
    {  
        NSLog( @"BACKGROUND" );  
        completionHandler( UIBackgroundFetchResultNewData );  
    }  
    else  
    {  
        NSLog( @"FOREGROUND" );  
        completionHandler( UIBackgroundFetchResultNewData );  
    }  
}

@end

我需要“didReceiveNotificationExtensionRequest”,因为我正在尝试使用 iOS 10 的可变内容功能。我已经设置了断点,并确定永远不会调用 didReceiveNotificationExtensionRequest,我想知道我是否需要在其他地方连接该类,因为 .m 文件从不做任何事情。任何帮助表示赞赏

如果重要的话我的手机运行的是 ios 10.0(所以应该支持可变内容)并且 OneSignal 版本是 (2.5.4) 谢谢

最佳答案

在 iOS 10 上 didReceiveRemoteNotification 方法将不会被调用...你应该去做

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

关于ios - OneSignal (iOS) - 未调用 didReceiveNotificationRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44712012/

相关文章:

javascript - 如何禁用点击输入?

ios - 在 textView 中添加 Emoji 然后 Crash Swift

ios - Swift 按钮调整大小动画

ios - 使用按钮操作发送单元格字符串值

ios - 向 UNNotificationCategory 添加动态操作

ios - 如何通过 Storyboard 将数据从推送通知传递到 ViewController?

ios - AppDelegate.swift 中没有这样的模块 'AWSMobileClient' 错误

javascript - 在导航中 react native 多颜色标题

ios - 导航 Controller 中 UIViewController 缺少后退按钮

ios - 推送通知不会发送到某些设备