Mah*_*hir 15 xcode push-notification ios parse-platform
我目前正在使用Xcode6 beta(第一版).使用parse.com,我正在尝试实现推送通知.在我的应用代表中,我有
[application registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)
当我在ios8 beta iPhone上运行它时,应用程序不会询问我是否要启用推送通知,并且application:didRegisterForRemoteNotificationsWithDeviceToken:从不调用相应的通知.但是,当我尝试在ios7 iPhone上运行它时,应用程序崩溃了,我收到了unrecognized selector该registerForRemoteNotifications方法的错误.
然后,我尝试在以前版本的Xcode(版本5.0)上运行它,但我收到了编译错误 no visible @interface declares registerForRemoteNotifications
我假设这个错误与过渡到iOS 8的错误有关,但我不知道如何解决这个问题.
Mad*_*dao 29
你会知道怎么做.
第一:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)
像这样添加代码
#ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
Run Code Online (Sandbox Code Playgroud)
第二:
添加此功能
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
}
else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
你可以获得deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Run Code Online (Sandbox Code Playgroud)
如果它仍然不起作用,使用此功能和NSLog 错误
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)
几点意见:
测试选择器的存在,如下代码所示:
func registerForPushForiOS7AndAbove(){
UIApplication.sharedApplication()
let application = UIApplication.sharedApplication()
if application.respondsToSelector(Selector("registerUserNotificationSettings:")) {
let notifSettings = UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge,
categories: nil)
application.registerUserNotificationSettings(notifSettings)
application.registerForRemoteNotifications()
}
else{
application.registerForRemoteNotificationTypes( .Sound | .Alert | .Badge )
}
Run Code Online (Sandbox Code Playgroud)
}
(不要错过PList中的UIBackgroundModes ..可以在Capabilities中完成)
| 归档时间: |
|
| 查看次数: |
38772 次 |
| 最近记录: |