推送通知委托方法问题

时间:2012-08-24 12:25:13

标签: objective-c ios push-notification

我是Objective 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 个答案:

答案 0 :(得分:3)

在您的AppDelegated didfinishLaunching with options中写下以下行

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

答案 1 :(得分:0)

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

if([application 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;

答案 2 :(得分:0)

使用新的委托方法

夫特:  func应用程序(应用程序:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject],fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) - &gt; Void)