registerForRemoteNotificationTypes: ไม่รองรับใน iOS 8.0 และใหม่กว่า


209

เมื่อพยายามลงทะเบียนสำหรับการแจ้งเตือนแบบพุชภายใต้ iOS 8.x:

application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound)

ฉันได้รับข้อผิดพลาดต่อไปนี้:

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.

ความคิดใด ๆ ที่เป็นวิธีใหม่ในการทำมัน? มันใช้งานได้เมื่อฉันเปิดแอพ Swift บน iOS 7.x

แก้ไข

บน iOS 7.x เมื่อฉันรวมรหัสตามเงื่อนไขที่ฉันได้รับ (เงื่อนไขอย่างใดอย่างหนึ่งของ SystemVersion หรือ #if __IPHONE_OS_VERSION_MAX_ALLOWED> = 80000)

dyld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings

1
ดูเอกสารประกอบของ UIA ฉันคิดว่าคุณควรจะใช้ registerUserNotificationSettings และ registerForRemoteNotifications
Skyte

3
ขอบคุณฉันจะตรวจสอบว่าในวันจันทร์
Wojtek Turowicz

@Skyte: วิธีการนั้นใช้ได้เฉพาะใน iOS 8+
user102008

ไม่มีใครรู้ว่าทำไมยังคงทำงานกับแอพที่มีอยู่แล้วในแอพสโตร์ แต่ไม่ลองทดสอบในเครื่อง
最白目

1
มันขึ้นอยู่กับเวอร์ชั่นของ xCode หรือไม่ ขอโทษสำหรับความคิดเห็นที่ 2 ในแถวฉันสายเกินไปสำหรับการแก้ไขความคิดเห็นข้างต้นของฉัน
最白目

คำตอบ:


145

ตามที่คุณอธิบายคุณจะต้องใช้วิธีการอื่นตาม iOS เวอร์ชันต่างๆ หากทีมของคุณใช้ทั้ง Xcode 5 (ซึ่งไม่ทราบเกี่ยวกับตัวเลือก iOS 8) และ Xcode 6 คุณจะต้องใช้การรวบรวมตามเงื่อนไขดังนี้:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
} else {
    // use registerForRemoteNotificationTypes:
}
#else
// use registerForRemoteNotificationTypes:
#endif

หากคุณใช้งาน Xcode 6 เพียงอย่างเดียวคุณสามารถติดกับสิ่งนี้ได้:

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
} else {
    // use registerForRemoteNotificationTypes:
}

เหตุผลอยู่ที่นี่คือวิธีที่คุณได้รับสิทธิ์การแจ้งเตือนมีการเปลี่ยนแปลงใน iOS 8 A UserNotificationเป็นข้อความที่แสดงให้ผู้ใช้ไม่ว่าจะจากระยะไกลหรือจากท้องถิ่น คุณต้องได้รับอนุญาตก่อนจึงจะสามารถแสดงได้ อธิบายไว้ในวิดีโอ WWDC 2014 "มีอะไรใหม่ในการแจ้งเตือน iOS"


11
@Matt - คุณมีการอ้างอิงว่าทำไมแอปเปิ้ลเสีย API ก่อนหน้านี้เพื่อรับสิทธิ์ในการส่งการผลักดันใน iOS8 หรือไม่? ฉันทำสิ่งเดียวกันในรหัสของฉัน แต่ฉันต้องการแบ่งปันเอกสารอย่างเป็นทางการเพื่ออธิบายสิ่งนี้กับคนอื่น ๆ ใน บริษัท ของฉัน
Kris Subramanian

3
@KrisSubramanian การอ้างอิงที่ดีที่สุดที่ฉันมีคือเอกสารเผยแพร่ก่อนวางจำหน่าย : "แอปที่ใช้การแจ้งเตือนที่มองเห็นหรือได้ยินพร้อมกับการแจ้งเตือนในท้องถิ่นหรือการแจ้งเตือนแบบพุชต้องลงทะเบียนประเภทการแจ้งเตือนที่ใช้" สำหรับ "ทำไม" ฉันมีเพียงการตีความของฉัน: ความสะดวกสบายของผู้ใช้เพื่อไม่ให้รบกวนข้อความโดยไม่คำนึงถึงแหล่งที่มา
matt ---

2
คุณไม่สามารถใช้__IPHONE_OS_VERSION_MAX_ALLOWEDตรวจสอบสิ่งนี้ได้เนื่องจากเป็นการตรวจสอบเวลาแบบคอมไพล์
Rob Keniger

5
การตรวจสอบเวลาแบบคอมไพล์คือสิ่งที่คุณต้องการในกรณีของ Xcode 5
ด้าน ---

1
@woheras registerUserNotificationSettings:มีการบันทึกไว้ที่นี่
matt ---

334

สำหรับ iOS <10

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    //-- Set Notification
    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
           // iOS 8 Notifications
           [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

           [application registerForRemoteNotifications];
    }
    else
    {
          // iOS < 8 Notifications
          [application registerForRemoteNotificationTypes:
                     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

     //--- your custom code
     return YES;
}

สำหรับ iOS10

https://stackoverflow.com/a/39383027/3560390


วิธีการเกี่ยวกับการโทร registerForRemoteNotifications จากการเรียกกลับของ registerUserNotificationSettings หากคุณต้องการให้แน่ใจว่าคุณไม่ได้ส่งการแจ้งเตือนครั้งแรกก่อนที่คุณจะได้รับอนุญาตจากผู้ใช้เพื่อแสดงการแจ้งเตือน?
Mohamed Hafez

5
แทนที่จะตรวจสอบsystemVersionคุณควรตรวจสอบ[[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]
แอนดี้

1
[[UIApplication sharedApplication] registerForRemoteNotifications];จะไม่ไปapplication:didRegisterForRemoteNotificationsWithDeviceToken:หรือapplication:didFailToRegisterForRemoteNotificationsWithError:หากผู้ใช้ปิดการใช้งาน "อนุญาตการแจ้งเตือน" ในการตั้งค่า -> การแจ้งเตือน -> <แอพของฉัน>
Protocole

IMO Apple ควรลบฟังก์ชั่นทั้งหมดใน iOS 8 แทนที่จะเลิกใช้งานหรือมีไว้เพื่อความเข้ากันได้ย้อนหลัง วิธีที่เป็นอยู่ตอนนี้การแจ้งเตือนแบบพุชล้มเหลวอย่างเงียบ ๆ ในหลาย ๆ แอพและตอนนี้นักพัฒนาก็กำลังดิ้นรนเพื่อแก้ไขปัญหา
Josh Liptzin

7
IMO พวกเขาไม่ควรจะเสียความเข้ากันได้ไปข้างหลัง ดูว่ารหัสของคุณน่าเกลียดที่จะให้การสนับสนุนทั้งสองรุ่นเมื่อเทียบกับหนึ่งบรรทัดก่อนหน้านี้ การใช้ API เก่าของคุณใหม่อย่างโปร่งใสในแง่ของใหม่เป็นเทคนิคที่แข็งแกร่งและส่งผลให้นักพัฒนารำคาญน้อยลง ทัศนคติของ Apple หมายความว่าเป็นเรื่องยากที่จะสนับสนุนแอพ iOS ซึ่งความพยายามในการรักษาระดับการทำงานเท่าเดิมในเวลาเพียง 2 ปีนั้นไม่ใช่เรื่องเล็กน้อย
robbie_c

23

การสร้างคำตอบของ @ Prasath นี่คือวิธีที่คุณทำในSwift :

if application.respondsToSelector("isRegisteredForRemoteNotifications")
{
    // iOS 8 Notifications
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil));
    application.registerForRemoteNotifications()
}
else
{
    // iOS < 8 Notifications
    application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
}

14

iOS 8 มีการเปลี่ยนแปลงการลงทะเบียนการแจ้งเตือนด้วยวิธีที่เข้ากันได้แบบย้อนหลัง ในขณะที่คุณต้องการสนับสนุน iOS 7 และ 8 (และในขณะที่ไม่รองรับแอพที่สร้างด้วย SDK 8) คุณสามารถตรวจสอบตัวเลือกที่คุณต้องการและโทรหาพวกเขาอย่างถูกต้องตามเงื่อนไขสำหรับเวอร์ชันที่รันอยู่

นี่คือหมวดหมู่ของ UIApplication ที่จะซ่อนตรรกะนี้ไว้เบื้องหลังส่วนต่อประสานที่สะอาดสำหรับคุณซึ่งจะใช้ได้ทั้งใน Xcode 5 และ Xcode 6

หัวข้อ:

//Call these from your application code for both iOS 7 and 8
//put this in the public header
@interface UIApplication (RemoteNotifications)

- (BOOL)pushNotificationsEnabled;
- (void)registerForPushNotifications;

@end

การดำเนินงาน:

//these declarations are to quiet the compiler when using 7.x SDK
//put this interface in the implementation file of this category, so they are
//not visible to any other code.
@interface NSObject (IOS8)

- (BOOL)isRegisteredForRemoteNotifications;
- (void)registerForRemoteNotifications;

+ (id)settingsForTypes:(NSUInteger)types categories:(NSSet*)categories;
- (void)registerUserNotificationSettings:(id)settings;

@end

@implementation UIApplication (RemoteNotifications)

- (BOOL)pushNotificationsEnabled
{
    if ([self respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        return [self isRegisteredForRemoteNotifications];
    }
    else
    {
        return ([self enabledRemoteNotificationTypes] & UIRemoteNotificationTypeAlert);
    }
}

- (void)registerForPushNotifications
{
    if ([self respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        [self registerForRemoteNotifications];

        Class uiUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings");

        //If you want to add other capabilities than just banner alerts, you'll need to grab their declarations from the iOS 8 SDK and define them in the same way.
        NSUInteger UIUserNotificationTypeAlert   = 1 << 2;

        id settings = [uiUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:[NSSet set]];            
        [self registerUserNotificationSettings:settings];

    }
    else
    {
        [self registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
    }
}

@end

5
ฉันไม่อยากจะเชื่อเลยว่าทำไมไม่ทำสิ่งนี้กับ Apple และนักพัฒนาต้องทำสิ่งนี้ทุกครั้งที่ Apple เลิกใช้วิธีการ iOS ใหม่ทุกรุ่นมันเหมือนกัน เป็นเรื่องน่าเศร้าที่จะเขียนรหัสใหม่เพียงเพราะ Apple เลิกใช้วิธีการที่เก่ากว่า
iVela

2
ฉันคิดว่ามันเป็นสิ่งที่ดีขึ้นเมื่อเวลาผ่านไปแทนที่จะเพิ่ม bandaids บน scabs เก่า ๆ เช่นระบบปฏิบัติการอื่น ๆ ที่ฉันคิดได้
Paul Bruneau

จากการทดสอบของฉัน (ที่ใช้เวลาทั้งวัน) ถ้าฉันไปที่Settingsและปิดการแจ้งเตือนisRegisteredForRemoteNotificationsก็ยังคงกลับมาYES
Iulian Onofrei

ยกนิ้วให้เพิ่มโซลูชันที่เหมาะสม: เลเยอร์ทางอ้อมอีกชั้น!
berkus

6

ฉันคิดว่านี่เป็นวิธีที่ดีกว่าในการรักษาความเข้ากันได้แบบย้อนหลังหากเราใช้วิธีนี้มันใช้งานได้กับกรณีของฉันและหวังว่าจะได้ผลสำหรับคุณ ยังสวยเข้าใจง่าย

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

ใช้ดีกว่าif ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)])ดังที่นี่
Iulian Onofrei

5

สำหรับ Swift-inclined:

if let registration: AnyObject = NSClassFromString("UIUserNotificationSettings") { // iOS 8+
    let notificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound)
    let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)

    application.registerUserNotificationSettings(notificationSettings)
} else { // iOS 7
    application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
}

3
ใน Swift 2.0 เท่าที่ฉันเข้าใจคุณควรมีตัวเลือกในชุด [.Alert, .Badge, .Sound] เพราะ (.Alert | .Badge | .Sound) ไม่ได้ผลสำหรับฉัน
Apan

3

ฉันไม่สามารถระบุได้ว่าตัวแปร NSSet "หมวดหมู่" ควรกำหนดเป็นเช่นไรดังนั้นหากใครบางคนกรอกฉันได้ฉันยินดีที่จะแก้ไขโพสต์นี้ อย่างไรก็ตามข้อความต่อไปนี้แสดงข้อความโต้ตอบการแจ้งเตือนแบบพุช

[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

แก้ไข: ฉันได้รับการแจ้งเตือนแบบพุชเพื่อส่งรหัสนี้ไปยังโทรศัพท์ของฉันดังนั้นฉันไม่แน่ใจว่าจำเป็นต้องใช้พารามิเตอร์หมวดหมู่หรือไม่


ใช่มันใช้งานได้กับ iOS8 แต่ฉันจะทำให้มันใช้งานร่วมกับ iOS7 ได้อย่างไร บน iOS7 สิ่งนี้จะผิดพลาด การตรวจสอบเวอร์ชั่น iOS ไม่ได้ผลเนื่องจาก iOS7 ไม่ได้จดจำสัญลักษณ์ใหม่
Wojtek Turowicz

2
categoriesใช้เพื่อตั้งค่าการแจ้งเตือนใน iOS 8 คุณสามารถดูวิดีโอ WWDC 2014 "มีอะไรใหม่ในการแจ้งเตือน iOS"สำหรับรายละเอียดเพิ่มเติม
matt ---

3

ดังนั้นปรากฎว่าเนื่องจาก AnyObject เป็นผู้สืบทอดทางจิตวิญญาณที่จะ id คุณสามารถโทรข้อความใด ๆ ที่คุณต้องการใน AnyObject นั่นเท่ากับการส่งข้อความถึง id ตกลงยุติธรรมพอ แต่ตอนนี้เราเพิ่มแนวคิดที่ว่าวิธีการทั้งหมดเป็นตัวเลือกใน AnyObjectและเรามีบางสิ่งที่เราสามารถทำงานได้

จากข้างบนฉันหวังว่าฉันจะส่ง UIApplication.sharedApplication () ไปที่ AnyObject จากนั้นสร้างตัวแปรที่เท่ากับลายเซ็นของเมธอดตั้งค่าตัวแปรนั้นเป็นวิธีเลือกจากนั้นทดสอบตัวแปร ดูเหมือนจะไม่ได้ผล ฉันเดาว่าเมื่อรวบรวมกับ iOS 8.0 SDK คอมไพเลอร์รู้ว่ามันคิดว่าวิธีการนั้นควรอยู่ที่ใดดังนั้นมันจึงปรับสิ่งเหล่านี้ให้เหมาะกับการค้นหาหน่วยความจำ ทุกอย่างทำงานได้ดีจนกระทั่งฉันพยายามทดสอบตัวแปร ณ จุดนี้ฉันจะได้รับ EXC_BAD_ACCESS

อย่างไรก็ตามในการพูดคุย WWDC เดียวกันกับที่ฉันพบอัญมณีเกี่ยวกับวิธีการทั้งหมดเป็นทางเลือกพวกเขาใช้การเชื่อมต่อแบบเลือกได้เพื่อเรียกวิธีการเลือก - และสิ่งนี้ดูเหมือนว่าจะใช้งานได้ ส่วนขาขาดคือคุณต้องพยายามโทรหาวิธีเพื่อทราบว่ามีอยู่จริงหรือไม่ซึ่งในกรณีของการลงทะเบียนเพื่อรับการแจ้งเตือนนั้นเป็นปัญหาเพราะคุณกำลังพยายามหาวิธีนี้ก่อนที่คุณจะสร้าง วัตถุ UIUserNotificationSettings ดูเหมือนว่าการเรียกใช้เมธอดด้วยศูนย์แม้ว่าจะไม่เป็นไรดังนั้นวิธีแก้ปัญหาที่ดูเหมือนจะใช้ได้กับฉันก็คือ:

var ao: AnyObject = UIApplication.sharedApplication()
if let x:Void = ao.registerUserNotificationSettings?(nil) {
    // It's iOS 8
    var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
    var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
} else {
    // It's older
    var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
    UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
}

หลังจากการค้นหามากเกี่ยวกับสิ่งนี้ข้อมูลสำคัญมาจากการพูดคุยของ WWDC https://developer.apple.com/videos/wwdc/2014/#407ตรงกลางตรงส่วนที่เกี่ยวกับ "วิธีการเพิ่มเติมในโปรโตคอล"

ใน Xcode 6.1 beta โค้ดด้านบนไม่ทำงานอีกต่อไปรหัสด้านล่างใช้งานได้:

   if UIApplication.sharedApplication().respondsToSelector("registerUserNotificationSettings:") {
        // It's iOS 8
        var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
       var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
       UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    } else {
        // It's older
        var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
    }

3

หากคุณต้องการเพิ่มการสนับสนุนให้กับ IOS7 IOS8 คุณสามารถใช้รหัสนี้ในโครงการของคุณ

-(void) Subscribe {
    NSLog(@"Registering for push notifications...");

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

-(void)application:(UIApplication *)application 
    didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    if (notificationSettings.types) {
        NSLog(@"user allowed notifications");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        NSLog(@"user did not allow notifications");
        UIAlertView *alert =[[UIAlertView alloc] 
            initWithTitle:@"Please turn on Notification"
            message:@"Go to Settings > Notifications > App.\n Switch on Sound, Badge & Alert"
            delegate:self
            cancelButtonTitle:@"Ok"
            otherButtonTitles: nil];
        [alert show];
        // show alert here
    }
}

2

หลังจาก Xcode 6.1 Beta โค้ดด้านล่างใช้งานได้แก้ไขเล็กน้อยในโค้ด Tom S ที่หยุดทำงานกับ 6.1 beta (ทำงานกับเบต้าก่อนหน้า):

   if UIApplication.sharedApplication().respondsToSelector("registerUserNotificationSettings:") {
        // It's iOS 8
        var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
       var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
       UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    } else {
        // It's older
        var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
    }

2

คุณสามารถใช้สิ่งนี้

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
        // for iOS 8
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [application registerForRemoteNotifications];
    }
    else
    {
        // for iOS < 8
        [application registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

    // RESET THE BADGE COUNT 
    application.applicationIconBadgeNumber = 0;

2

Swift 2.0

// Checking if app is running iOS 8
    if application.respondsToSelector("isRegisteredForRemoteNotifications") {

        print("registerApplicationForPushNotifications - iOS 8")

        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil));
        application.registerForRemoteNotifications()

    } else {
        // Register for Push Notifications before iOS 8
        print("registerApplicationForPushNotifications - <iOS 8")
        application.registerForRemoteNotificationTypes([UIRemoteNotificationType.Alert, UIRemoteNotificationType.Badge, UIRemoteNotificationType.Sound])

    }

1

หากสิ่งที่คุณต้องการคือรหัส ios 8 สิ่งนี้ควรทำ

 - (BOOL)application:(UIApplication *)application       didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
       [application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound  | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)  categories:nil]];

       [application registerForRemoteNotifications];
}

 return YES;
}

0

นี่เป็นวิธีที่สะอาดกว่าที่ฉันทำและใช้งานได้ดีมาก

if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber_iOS_8_0)
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
     UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeSound];
     else {
         [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
         [application registerForRemoteNotifications];
     }

0

สำหรับ iOS 8 ขึ้นไป

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.