【问题标题】:Push Notifications Cannot get Device Token推送通知无法获取设备令牌
【发布时间】:2016-05-08 19:35:33
【问题描述】:

我正在尝试获取设备令牌,以便向它发送通知,但我不断收到错误消息“iOS 8.0 及更高版本不支持 enabledRemoteNotificationTypes。”

设备已注册,因为我可以在手机的通知设置中关闭和打开通知。这是我用来尝试检索令牌的代码:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(@"This is device token%@", deviceToken);
}

我正在使用此代码注册设备:

if ([application   respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // iOS 8 Notifications
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];
    NSLog(@"ios 8+: %@");
}
else
{
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |      UIRemoteNotificationTypeSound)];
    NSLog(@"< ios 8+: %@");
}

理想情况下,我想检索设备令牌并将它们发送到 mysql 数据库,但不知道如何执行此操作,因为我是 Web 开发人员并且不太熟悉 Objective C

【问题讨论】:

  • didRegisterForRemoteNotificationsWithDeviceToken 不是错误。一旦您注册了推送通知 deviceToken,它就是一个有效的回调,因为 nsdata 与它一起传递包含您可以提取然后很可能传递到您的后端的设备令牌
  • 我的错误是“iOS 8.0 及更高版本不支持 enabledRemoteNotificationTypes。”
  • 您如何注册远程通知?
  • 刚刚将其添加到问题中。

标签: ios push-notification


【解决方案1】:

以下是您应该注册远程通知的方式:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
{
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
    UIRemoteNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}

另外,如果你需要将它发送到你的后端,你先看看这里并在didRegisterForRemoteNotificationsWithDeviceToken:中提取一个字符串令牌:Iphone device token - NSData or NSString

然后使用 AFNetworking 或 NSURLSession 将其发送到您的后端 API。 例如看这里:Send POST request using NSURLSession

【讨论】:

  • 好的,所以从手机中删除了应用程序,添加了新代码,然后通过 xcode 再次运行它。它提示我收到我收到的通知,但在日志中它仍然不会给我我的设备令牌。 “iOS 8.0 及更高版本不支持 enabledRemoteNotificationTypes。”
  • 您需要从 didRegisterForRemoteNotificationsWithDeviceToken 的 NSData 参数中提取一个令牌:stackoverflow.com/questions/1587407/… 然后使用此令牌并在 http post 请求中调用您的后端 API(可能是 application/x-www-form-urlencoded 格式)
  • didRegisterForRemoteNotificationsWithDeviceToken 不会触发
  • 是的,代码很好用,我可以注册!但我无法将设备令牌检索到 NSlog 以查看它。
【解决方案2】:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
 {

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{
    NSString *myDeviceToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    myDeviceToken = [myDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"Device Token: %@", myDeviceToken);
}

使用上述代码获取设备令牌,并获取远程通知确保您的后台模式为远程通知开启

【讨论】:

  • 详细说明您的答案。
  • 使用上述代码获取设备令牌,并获取远程通知确保您的后台模式为远程通知开启
  • 使用“编辑”按钮将其添加到您的答案中。我已经为你做了,等待审核。
猜你喜欢
  • 2012-02-06
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
相关资源
最近更新 更多