【问题标题】:UNUserNotificationCenterDelegate methods not being called Xcode 8 iOS 10UNUserNotificationCenterDelegate 方法未被调用 Xcode 8 iOS 10
【发布时间】:2017-03-02 08:52:34
【问题描述】:

我完全按照这里提到的所有步骤进行操作:Push notification issue with iOS 10。但不幸的是,这两个方法没有被调用:

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

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler

唯一调用的方法是:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler

但是,当应用关闭时,didReceiveRemoteNotification 不会被调用。

我使用的是 Xcode 8 和 iOS 10.0.2

AppDelegate.h

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

AppDelegate.m

#import "AppDelegate.h"

#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)


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
{

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |    UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
     {
         if( !error )
         {
             [[UIApplication sharedApplication] registerForRemoteNotifications];  // required to get the app to do anything at all about push notifications
             NSLog( @"Push registration success." );
         }
         else
         {
             NSLog( @"Push registration FAILED" );
             NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
             NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );  
         }  
     }];  
}
return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{


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
    return;
}
}

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

//Called when a notification is delivered to a foreground app.

NSLog(@"Userinfo %@",notification.request.content.userInfo);

completionHandler(UNNotificationPresentationOptionAlert);
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

//Called to let your app know which action was selected by the user for a given notification.

NSLog(@"Userinfo %@",response.notification.request.content.userInfo);

}

此外,如果我在 didReceiveRemoteNotification 中调用 return 会出现错误:

警告:应用程序委托收到了对 -application:didReceiveRemoteNotification:fetchCompletionHandler: 的调用,但从未调用过完成处理程序。

当我看到 iOS 控制台(获取更多推送通知日志的工具)时,我可以看到这个错误:

20/10/16 15:06:48 SpringBoard(UserNotificationsServer)[55]: Not saving push notification to store 3995-06A9, error=Error Domain=UNErrorDomain Code=1401 "Notification has no user-facing content" UserInfo={NSLocalizedDescription=Notification has no user-facing content}

【问题讨论】:

  • 你能显示那个appdelegate代码吗
  • @Anbu.Karthik 现在 appdelegate 代码就在那里!谢谢

标签: ios objective-c apple-push-notifications push usernotifications


【解决方案1】:

(代表 OP 发布解决方案)。

推送通知在 aps 中缺少警报键,因此用户通知服务器没有在商店中保存通知,因此没有调用这两个方法。

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 2017-02-11
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多