1

用户在第一次使用我们的应用程序时会收到一条提醒,要求他们接收苹果推送。设备的操作系统版本为 ios 8.0 或更高版本。用户允许请求。ios8.0设备可以接收apple push,ios 8.1不能。我使用这段代码:

UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
 not use:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:settings];

我不知道错误!有人可以帮助我。谢谢!

4

3 回答 3

1

我遇到了同样的问题,下面的代码可以帮助我!在 iOS 8 中,注册远程通知的方式已经改变。

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                                 settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                                 categories:nil]];


            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        else
        {
                    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
        }
于 2014-11-12T03:51:31.453 回答
0

尝试使用“show”而不是“push”,因为后者已被弃用,并且暗示将来不再使用它。

于 2014-11-12T03:45:29.017 回答
0

AppDelegate您应该为ios7 和 ios 8 添加此代码appDidFinishLaunch

/*--- Setup Push Notification ---*/
//For iOS 8
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)] && [UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)])
{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
//For iOS 7 & less
else if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotificationTypes:)])
{
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}

也许您不需要这个,但如果您需要接收远程通知方法

#pragma mark - For Remote Notification
#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

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *device_Token = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

 // show some alert or otherwise handle the failure to register.
 NSLog(@"error : %@",error.localizedDescription);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [self SetUpActionWhenPushNotiClicked:userInfo application:application PushPop:NO];
}
-(void)SetUpActionWhenPushNotiClicked:(NSDictionary*)userInfo application:(UIApplication *)application PushPop:(BOOL)pushpop
{
NSLog(@"UserInfo : %@",userInfo);
application.applicationIconBadgeNumber = 0;
if (userInfo)
{
    if (application.applicationState == UIApplicationStateActive )
    {
        //NSLog(@"app is already open");
    }
    else if (application.applicationState == UIApplicationStateBackground ||
             application.applicationState == UIApplicationStateInactive)
    {
        //NSLog(@"app is coming from bg");
    }
}

}

也许这会对你有所帮助。

于 2014-11-12T04:23:00.910 回答