17

我正在开发一个需要推送通知的 iPhone 应用程序。我按照创建认证和修改应用 ID 的说明进行操作。我不完全确定我这样做是否正确,但我确实按照指示进行了操作。知道如何检查是否可以吗?

当我在模拟器中运行时,我确实收到一条错误消息,说模拟器不支持推送通知。这在某种程度上是意料之中的。

顺便说一句:我已经好几次出现这个问题了。它似乎总是与一部越狱的手机有关。我的手机没有越狱。

但是当我在 iPhone 上调试时,didRegisterForRemoteNotificationsWithDeviceToken 方法永远不会被触发。我将非常感谢一些帮助。我的代码如下。

-(void)applicationDidFinishLaunching:(UIApplication *)application 
{    
   rootController.delegate = self;
   [window addSubview:rootController.view];
   [window makeKeyAndVisible];

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

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
   NSString *str = 
    [NSString stringWithFormat:@"%@",deviceToken];
    NSLog(str);
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err 
{ 
    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(str);    
}
4

7 回答 7

21

通常如果didRegisterForRemoteNotificationsWithDeviceToken从未调用过,那是因为您使用的配置文件不是APS-entitled. 如果您的应用程序是生产应用程序,则必须使用分发配置文件对其进行签名才能进行推送。如果是开发应用程序,您必须使用开发配置文件对其进行签名。

编辑:还值得注意的是,provisioning profile必须使用显式捆绑 ID。发送到使用通配符配置文件签名的任何内容的推送将不会被传递。

于 2012-08-22T15:48:46.230 回答
9

我遇到过同样的问题。

对我来说,这是应用程序的通知设置,一切都关闭了。

如果在 iOS 设置中禁用应用通知,则不会调用该方法。

一旦我打开通知类型,它就开始工作了。

Settings App->Notification Settings->[My App]

于 2013-12-02T21:02:41.497 回答
2

查看TN2265,其中包含调试推送通知问题的提示。我今天确实遇到了这个问题,结果证明 iPod Touch 上的 WiFi 已关闭。既然你说你是用 iPhone 工作的。可能不是这样,但值得查看技术说明以获取更多可能性。

于 2010-11-08T03:14:41.770 回答
1

检查您的防火墙设置。

来自Scott K 的回答

如果您的 iOS 设备能够使用蜂窝数据网络,请检查它是否具有有效的蜂窝数据计划。例如,在“设置”中关闭 Wi-Fi,看看您是否仍然可以使用 Safari 浏览网页。另一方面,如果推送服务使用 Wi-Fi,则您的设备或计算机与 Internet 之间的任何防火墙都必须允许 TCP 流量进出端口 5223。

于 2012-08-07T17:48:15.283 回答
1

根据 Apple 的文档“Troubleshooting Push Notifications”“如果委托回调 application:didRegisterForRemoteNotificationsWithDeviceToken: 和 application:didFailToRegisterForRemoteNotificationsWithError: 均未调用,则表示此连接尚未建立。”...“系统可能没有 Internet 连接。 .aiplane 模式。”

我也看到了这种行为,但不是因为缺乏互联网连接,而是因为正如 IrFan 指出的那样,应用程序的通知已被禁用,一旦您启用通知,该方法就会触发。

解决方案 1) 不要依赖任何被调用的委托方法。2) 如果以及何时收到委托回调,请对其采取行动。3) 缺乏网络连接,应用程序禁用推送通知,APS 未启用,或者?将导致代表不会被调用。

于 2014-05-07T20:29:41.497 回答
1

如果您使用 iOS8+ SDK(例如 xcode 6+)进行编译,那么已弃用的 iOS7 API 方法将被静默忽略,您不会收到任何错误消息。

如果你想支持 iOS7 和 8,你需要这样的东西:

// Have to explicitly use the iOS8 api as the deprecated API is silently ignored on iOS8, surprisingly.
if ([UIDevice currentDevice].systemVersion.intValue >= 8) { // iOS8+ API.
    UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else { // iOS7.
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}

如果您在aps-environment个人资料中丢失问题,您至少会收到一个有用的错误回调didFailToRegisterForRemoteNotificationsWithError

于 2015-08-11T03:29:09.133 回答
0

我在没有调用任何委托方法的 iOS 11.3 设备中遇到了同样的问题。因此,根据 Apple 文档:-

https://developer.apple.com/library/content/technotes/tn2265/_index.html

当安装第一个支持推送的应用程序时,iOS 或 OS X 会尝试与系统上所有支持推送的应用程序共享的推送服务建立持久的网络连接。如果委托回调 application:didRegisterForRemoteNotificationsWithDeviceToken: 和 application:didFailToRegisterForRemoteNotificationsWithError: 均未调用,则表示此连接尚未建立。

进一步,

这不一定是错误情况。该系统可能根本没有互联网连接,因为它超出了任何手机信号塔或 Wi-Fi 接入点的范围,或者它可能处于飞行模式。您的应用程序不应将此视为错误,而应正常运行,仅禁用依赖于推送通知的功能。

我尝试连接到其他网络并再次开始执行注册通知操作,瞧!,它就像一个魅力。所以,我猜在注册通知时存在问题,它没有调用任何委托方法。

于 2018-06-04T11:22:53.853 回答