未调用方法:- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceTok
Posted
技术标签:
【中文标题】未调用方法:- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken【英文标题】:Method not being called: - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 【发布时间】:2015-03-31 21:58:17 【问题描述】:一直卡在这上面,但无法调用以下方法。我可以让手机请求许可,但之后就卡住了。
- (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);
我尝试了很多方法,包括:
1) 以下 *** 帖子中提供的所有内容:why didRegisterForRemoteNotificationsWithDeviceToken is not called
2) 查看 Apple 的技术说明:https://developer.apple.com/library/ios/technotes/tn2265/_index.html
3)整个教程:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1(希望我的证书都很好)
4) (是的,我有互联网连接)
有没有人有任何可能的解决方案?在过去的 2-3 天里,我一直在绞尽脑汁,并且已经不得不将手机恢复出厂设置两次,并更改手机上的日期 N^e 次以使通知弹出窗口出现一遍又一遍地测试。
希望有任何帮助!谢谢!
这是我用来称呼的...(尝试了其他几个版本):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//other stuff not related
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
#endif
else
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
return YES;
【问题讨论】:
是iOS8吗?还调用了 didFailedToRegister 吗? 都不调用,iOS8 构建机器上是否安装了 APNS 证书,并且还更新了配置文件? 两者都是,我按照此处的说明获取证书:raywenderlich.com/32960/… 【参考方案1】:iOS 8 对注册流程进行了更改,要求在获得用户许可后明确要求注册远程通知。
你是在调用这个代码吗?
[[UIApplication sharedApplication] registerUserNotificationSettings: settings];
并在您的应用程序委托中实现这一点?
- (void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
[application registerForRemoteNotifications];
也就是说,旧 API 现在已被弃用,但如果您计划支持 iOS 7,仍然需要这些 API。要同时支持两者,您可以检查 UIApplication 是否响应新选择器:
if ([[UIApplication sharedApplication] respondsToSelector:@SEL(registerUserNotificationSettings:)])
// iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings: settings];
// [[UIApplication sharedApplication] registerForRemoteNotifications]
// will be called in your UIApplicationDelegate callback
else
// iOS 7
[[UIApplication sharedApplication] registerForRemoteNotifications];
【讨论】:
在 if 块的 iOS 8 部分中,您需要删除对[application registerForRemoteNotifications]
的调用。在您的应用程序委托中实现didRegisterUserNotificationSettings:
回调并在该方法中调用registerForRemoteNotifications
。在权限请求完成之前,registerForRemoteNotifications
调用将无法工作。
我之前做过这样的事情: - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NSLog(@"IOS8 VERSION"); [申请注册远程通知];
它基本上使我的应用程序崩溃:(以上是关于未调用方法:- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceTok的主要内容,如果未能解决你的问题,请参考以下文章
App Delegate 的应用程序:didReceiveLocalNotification:未调用
调用未定义的方法 Illuminate\Foundation\Application::bindShared()
在越狱设备上使用后台程序时未调用 UIApplicationDelegate
协议指定 void 参数类型时未调用 gRPC node-js 方法
- (void)applicationDidEnterBackground:(UIApplication *)application