Can*_*ğlu 17 xcode push-notification apple-push-notifications ios provisioning-profile
首先,我在iPhone 6 Plus/iOS 8.1上,我已经在这里尝试了一切:为什么没有调用RegisterForRemoteNotificationsWithDeviceToken
仍然无济于事.总结一下:
在application:didFinishLaunchingWithOptions:我打电话:
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
//iOS 8+
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}else{
//pre-iOS 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
Run Code Online (Sandbox Code Playgroud)
在application:didRegisterUserNotificationSettings:我打电话:
[application registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)
我用断点检查了它,它被调用了.我还实现了两种方法:
application:didRegisterForRemoteNotificationsWithDeviceToken:
和
application:didFailToRegisterForRemoteNotificationsWithError:
但是,它们都没有被称为.不是错误,没有.控制台也没有错误.(我今天早些时候遇到了有关权利的问题,但创建了新的证书/配置文件解决了这个问题)
可能是什么问题?
更新:这是一些东西.在application:didRegisterUserNotificationSettings:我检查的通知设置,这里是我得到了什么:
(lldb) po notificationSettings
<UIUserNotificationSettings: 0x170031cc0; types: (none);>
Run Code Online (Sandbox Code Playgroud)
更新2:我再次检查了通知,发现现在我的应用程序被添加到设置中的通知中,它已启用,具有权限.但是,仍然没有调用处理程序.类型是没有的.我99%肯定这与问题有关.
更新3:我已经在另一台设备上测试过(iPod touch,iOS 8.1),我没有遇到任何问题.它立即application:didRegisterForRemoteNotificationsWithDeviceToken:使用其令牌调用该方法.问题是我的iPhone特有的.
小智 10
我遇到了类似的问题,代码已经实现并且工作正常.突然之间,经过一些调整,它就再也不起作用了.
在设备上运行的方案是:
我什么都试过,几乎重置所有推配置,证书和配置配置文件等.与上一版本的所有设备进行工作,但是当我安装新版本,只是不工作了.
为了解决这个问题,我刚刚这样做了:
与Xcode战斗6小时后,这是我的解决方案,没有任何解释.
我希望这有助于某人.
尝试使用这个:(效果很好),抱歉堆栈的格式似乎不太好
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"iOS 8 Requesting permission for push notifications..."); // iOS 8
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
UIUserNotificationTypeSound categories:nil];
[UIApplication.sharedApplication registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
NSLog(@"iOS 7 Registering device for push notifications..."); // iOS 7 and earlier
[UIApplication.sharedApplication registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSLog(@"received a notification while active...");
}
else if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//opened from a push notification when the app was on background
NSLog(@"i received a notification...");
NSLog(@"Message: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
NSLog(@"whole data: %@",userInfo);
}
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
self.token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
self.token = [self.token stringByReplacingOccurrencesOfString:@" " withString:@""];
[[NSUserDefaults standardUserDefaults] setObject:self.token forKey:@"deviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"-->> TOKEN:%@",self.token);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
NSLog(@"Registering device for push notifications..."); // iOS 8
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8 s
completionHandler();
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
// Respond to any push notification registration errors here.
NSLog(@"Failed to get token, error: %@", error);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12262 次 |
| 最近记录: |