0

我是 Objective C 的新手。在我的应用程序中,我需要实现推送通知,我已经创建了新的应用程序 ID,并且我还创建了新的配置文件。我已完成此链接中提到的所有步骤

我已经在我的 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]);
    }    

}

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

4

3 回答 3

3

在带有选项的 AppDelegated didfinishLaunching 中写入以下行

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
于 2012-08-24T12:38:05.080 回答
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;
于 2015-04-22T08:10:11.877 回答
0

使用新的委托方法

Swift:func 应用程序(应用程序:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject],fetchCompletionHandler completionHandler:(UIBackgroundFetchResult)-> Void)

于 2016-02-29T22:49:12.370 回答