2015-06-14 140 views
1

我开始遵循本教程,我通过谷歌发现。开始使用iOS推送通知

http://code.tutsplus.com/tutorials/setting-up-push-notifications-on-ios--cms-21925

但是我被困在基本的第一步它说,该方法已被弃用,我改变了他们的Xcode的 提出的那些原代码是

[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 

我改变它到

[application registerForRemoteNotification:(UIRemoteNotificationType | UIRemoteNotificationType | UIRemoteNotificationTypeSound)]; 

但我不断收到错误消息说“预期的表达式”

我仍然在这里的第一步,我正在遵循的这个教程是为ios 6 现在我正在iOS 8上工作,我找不到任何完整的教程关于如何实现推送通知我的应用程序。任何人都可以将我指向正确的方向吗?

回答

2

您错误地使用了API。

首先,推送通知API在iOS 8.0之后发生了变化。

我的答案会认为你想仍然支持iOS的7.x及更高版本

// Checks if the application responds to the API introduced in iOS 8. 
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
    // iOS 8 Support 
    // Note that you pass a object of type UIUserNotificationSettings as parameter instead of the enums only. 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]]; 
} else { 
    // Old API so use the same code from the tutorial 
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 
} 
+0

仍然给我此行的代码下弃用...... [应用registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound )]; } 谢谢,我将继续与你给我的代码一起工作,随着我正在跟随的教程,看看我是否可以让它一起工作:) – jack

+0

它会给出警告,如果你的目标是iOS 8以上,那么你不需要担心if测试,只需马上就可以使用API​​。如果您使用7或更高,则需要进行if测试。 –

+1

啊okey我做到了这一点,因为你说我的应用程序是针对iOS 8 + ...而且它像一个魅力工作...谢谢 – jack