2012-08-24 72 views
0

我是新来的目标C.在我的应用程序中,我需要实现推送通知,我已经创建了新的应用程序ID,我也创建了新的供应配置文件。我完成了这个link推送通知代理方法问题

中提到的所有步骤我已经在我的appDelegate .m文件中声明了这个委托函数。

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 

     NSString *deviceTokens = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
     deviceTokens = [deviceTokens stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     NSLog(@"registered device token %@", deviceTokens); 
     self.deviceToken = deviceTokens; 
} 

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 

    NSString *str = [NSString stringWithFormat: @"Error: %@", err]; 
    NSLog(@"String %@",str);  

} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 

    for (id key in userInfo) { 
     NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); 
    }  

} 

但是这个委托函数没有被调用。请帮我解决这个问题。

回答

3

在你AppDelegated didfinishLaunching的选项写下面一行

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 
0

将此代码添加到您的应用程序didFinishLaunchingWithOptions方法,应用程序委托中

如果([应用respondsToSelector:@selector(registerUserNotificationSettings :) ]){

//We check because ios7 cannot respond to this method and there will be a crash.. 
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 

    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; 

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; 
    [application registerForRemoteNotifications]; 
} 
else { 
    //This will work on ios7 devices and below 
    UIRemoteNotificationType types = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
    [application registerForRemoteNotificationTypes:types]; 
    [application registerForRemoteNotifications]; 
} 

return YES; 
+0

我不知道如何获取代码源是第一部分工作,但要确保你在应用程序中调用了registerForRemoteNotifications方法,否则它不会触发你的推送通知委托方法,因为它没有尝试registerForNotifications – Mthokozisi

0

使用新的委托方法

斯威夫特: FUNC应用程序(应用程序:UIApplication的,didReceiveRemoteNotification用户信息:[NSObject的:AnyObject],fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) - >无效)