Reputation: 299
I followed OneSignal's set up guide, added an extension all named properly and have this file that they provided:
#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
Also copied all their code for AppDelegate as outlined in their setup guide here: https://documentation.onesignal.com/docs/ios-sdk-setup plus some other code to create the 'center' variable as someone recommended on github. Adding this instance in AppDelegate did make it so that 'didReceiveRemoteNotification' gets called, but not 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
I need 'didReceiveNotificationExtensionRequest' since I'm trying to use iOS 10's mutable content feature. I've put in breakpoints and am sure that didReceiveNotificationExtensionRequest is never called, I'm wondering if I need to hook up the class somewhere else because the .m file never does anything. Any help appreciated
If it matters my phone is running ios 10.0 (so should support the mutable content) and OneSignal version is (2.5.4) Thanks
Upvotes: 1
Views: 2264
Reputation: 1535
On iOS 10 didReceiveRemoteNotification
method will not be called...you should go for
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
Upvotes: 1