0

我跟着OneSignal的設置嚮導,增加了一個擴展名的命名恰當,並有此文件,他們提供的:在概述OneSignal(IOS) - didReceiveNotificationRequest不叫

#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上推薦的人。在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-ab' 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) 感謝

+0

一樣的在這裏。試圖在Swift中做到這一點。該擴展沒有被調用。正如OneSignal論壇所述:當content-available設置爲true時,擴展名不會被調用。我已經開啓了內容可用,可變的內容。我可以捕獲通知並更改內容。結構不同。例如:additionalData可以作爲''' let url =((userInfo [「custom」] as?[String:Any])?[[a]] as?[String:Any])?[「url」 ]爲?字符串 ''' –

回答

0

在iOS上10 didReceiveRemoteNotification方法不會被調用..你應該去

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