Muz*_*mil 82 objective-c apple-push-notifications
我正在制作一个应用程序,我想在其中实现apple推送通知服务.我按照本教程中给出的分步说明进行操作.
但仍然没有调用这些方法.我不知道造成这个问题的原因.谁能帮我?
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//NSString * token = [[NSString alloc] initWithData:deviceTokenencoding:NSUTF8StringEncoding];
NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(@"Device Token:%@",str);
//NSLog(@"Device token is called");
//const void *devTokenBytes = [deviceToken bytes];
//NSLog(@"Device Token");
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @"Error: %@", err];
NSLog(@"Error:%@",str);
}
Run Code Online (Sandbox Code Playgroud)
100*_*ams 69
我有同样的问题:registerForRemoteNotificationTypes:调用既不调用application:didRegisterForRemoteNotificationsWithDeviceToken:也不调用application:didFailToRegisterForRemoteNotificationsWithError:
我最终在Apple的技术说明TN2265的帮助下解决了这个问题.
这就是我做的:
首先,我仔细检查了我确实正确注册了Push Notifications,包括验证我的"aps-environment"密钥的配置文件以及.app文件本身的代码签名.我把它全部设置正确.
然后,我必须在控制台中调试推送通知状态消息(您需要在设备上安装PersistentConnectionLogging.mobileconfig配置文件并重新启动它.请参阅"观察推送状态消息"下的TN2265).我注意到apns进程启动了一个计时器并计算了一个最小的开火日期,这让我怀疑APNS正在通过此时推出的推送通知注册确认消息,如TN2265所示:
重置iOS上的推送通知权限警报
第一次启用推送的应用程序注册推送通知时,iOS会询问用户是否希望接收该应用程序的通知.一旦用户响应此警报,除非设备已恢复或应用程序已卸载至少一天,否则不会再次显示该警报.
如果您想模拟首次运行的应用,可以将应用程序卸载一天.通过将系统时钟向前设置一天或更长时间,完全关闭设备,然后重新打开设备,您可以实现后者而无需实际等待一天.
因此,我从设备中删除了应用程序,然后在"设置"中手动更改了iPhone的日期,重新启动了设备,并重新安装了应用程序.
下次我的代码调用时registerForRemoteNotificationTypes,它会按预期收到回调.
这解决了我的问题.希望能帮助到你.
小智 66
在iOS 8中,不推荐使用某些方法.请按照以下步骤兼容iOS 8
if([[UIDevice currentDevice] systemVersion].floatValue >= 8.0)
{
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];
}
Run Code Online (Sandbox Code Playgroud)
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
//For interactive notification only
- (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"]){
}
}
Run Code Online (Sandbox Code Playgroud)
注意:除了didRegisterForRemoteNotificationsWithDeviceToken和之外,iOS 8中还需要以上两种新方法didReceiveRemoteNotification.否则将不会调用委托方法.
请参阅:远程通知iOS 8
Kyl*_*egg 26
在iOS 8中,除了以不同方式请求推送通知访问之外,您还需要以不同方式注册.
申请进入:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// iOS 8
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 7 or iOS 6
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
Run Code Online (Sandbox Code Playgroud)
处理注册设备:
// New in iOS 8
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
// iOS 7 or iOS 6
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
// Send token to server
}
Run Code Online (Sandbox Code Playgroud)
小智 12
确保调用代码(根据支持的通知类型进行更新)
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
Run Code Online (Sandbox Code Playgroud)
并且配置文件已启用APNS.启用APNS后,您可能需要重新下载配置文件.如果您遇到麻烦并且出现错误,那么您可能应该创建一个Entitlements.plist并添加具有值"development"或"production"的密钥"aps-environment",具体取决于构建的类型(通常这个键值对是包含在配置文件中,但有时Xcode会乱用它们).
Eri*_*ric 12
请记住,模拟器中不支持远程通知.因此,如果您在模拟器中运行您的应用程序,didRegisterForRemoteNotificationsWithDeviceToken则不会被调用.
如果在启用和配置Apple推送通知服务之前使用配置文件,则需要再次重新下载配置文件.
从Xcode Organizer和iPhone/iPad删除配置文件.去吧Settings -> General -> Profiles -> [Your provisioning] -> Remove.
安装新下载的配置文件.然后从XCode清理并运行项目.现在didRegisterForRemoteNotificationsWithDeviceToken应该被召唤.
试试这个对我有用,
第一步
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)
在上面的方法中添加下面的代码
UIApplication *application = [UIApplication sharedApplication];
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
|UIUserNotificationTypeSound
|UIUserNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
}
else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
Run Code Online (Sandbox Code Playgroud)
第二步
添加下面的代码功能
#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
Run Code Online (Sandbox Code Playgroud)
您将在以下功能中获得设备令牌
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Run Code Online (Sandbox Code Playgroud)
有关详细答案,请参阅此
希望这对某人有所帮助。
| 归档时间: |
|
| 查看次数: |
76720 次 |
| 最近记录: |