push notification : both didFailToRegister and didRegister delegate not calling

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 15:48:52

问题


I am creating an application which support push notification
I am following all the steps.

It give error on simulator

Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x5813d20 {NSLocalizedDescription=remote notifications are not supported in the simulator}

But on device it not calling the delegates methods

didFailToRegisterForRemoteNotificationsWithError  
didRegisterForRemoteNotificationsWithDeviceToken  

My Code :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    NSLog(@"application didFinishLaunchingWithOptions");
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"My Token is %@",deviceToken);
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Failed to get token, error: %@", error);
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"Received Notification %@", userInfo);
}

回答1:


Push notification won't work in iPhone's Simulator.

Try to check with device.




回答2:


Solved for me:

In Device, go to:

`Settings->Notifications->YourApp->Enable_Notifications`



回答3:


As said by @kalyan Push Notification will NOT work in Simulator But if you want to get ride of the warning here is the code to work with both Simulator and device (Check for TARGET_IPHONE_SIMULATOR ) and iOS7 and iOS8 ( Check for registerUserNotificationSettings )

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    #if !TARGET_IPHONE_SIMULATOR
         // New User Notification Settings for iOS8 support
        if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
            UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
                                                                                     categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        } else { // for iOS 7
            UIRemoteNotificationType remoteNotificationType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             remoteNotificationType];
        }

    #endif
}


来源:https://stackoverflow.com/questions/7820566/push-notification-both-didfailtoregister-and-didregister-delegate-not-calling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!