ios - 推送通知无法获取设备 token

标签 ios push-notification

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

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

- (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+: %@");
}

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

最佳答案

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

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

关于ios - 推送通知无法获取设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35112103/

相关文章:

ios - 如何在 iOS 中使用平衡支付将 href 发送到服务器进行支付处理

ios - Swift 2.2 泛型 : "Cannot convert return expression of type ItemPageControllerFactory to return type T"

ios - 谷歌 FCM 服务器 : 200 but no notification sent to phone

push-notification - 控制在SWIFT中收到推送通知后加载哪个 View Controller

node.js - 如何在 firebase node.js 云函数中推送通知?

firebase - 单击推送通知时如何在我的 flutter 应用程序中打开特定页面

ios - 带有图标的 UIButtons 不喜欢

objective-c - 使用 NSOrderedAscending 和 NSOrderedDescending 在特定范围内的日期

ios - .mp3 音频对于 iOS 应用程序是否足够,或者您还需要 .ogg?

push-notification - PWA 的推送通知图标