简体   繁体   English

如何实现iOS 10的推送通知[目标C]?

[英]How to implement push notification for iOS 10[Objective C]?

Can anyone help me with implementing push notification for iOS 10 as i have implemented following code but still getting problem in it: 任何人都可以帮我实现iOS 10的推送通知,因为我已经实现了以下代码,但仍然遇到问题:

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0"))
{
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if(!error){
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    }];
}
else {
    // Code for old versions
}

I am getting error suggesting that 我收到错误提示

Unknown receiver UIUserNotificationCenter 未知的接收器UIUserNotificationCenter

Thank you in advance! 先感谢您!

Sorry guys, I got the answer. 对不起伙计们,我得到了答案。 I just needed to import UserNotifications framework. 我只需要导入UserNotifications框架。

#import <UserNotifications/UserNotifications.h>

Check this 检查一下

#import <UserNotifications/UserNotifications.h>

Then 然后

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    } else {
        // iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
        // For iOS 10 display notification (sent via APNS)
        [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        UNAuthorizationOptions authOptions =
        UNAuthorizationOptionAlert
        | UNAuthorizationOptionSound
        | UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
        }];
#endif
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM