[英]Push Notifications Cannot get Device Token
我正在嘗試獲取設備令牌,以便可以向其發送通知,但我一直收到錯誤消息:“ 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不太熟悉
這是注冊遠程通知的方法:
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設備令牌 didRegisterForRemoteNotificationsWithDeviceToken:
或NSString
然后使用AFNetworking或NSURLSession將其發送到您的后端API。 以這里為例: 使用NSURLSession發送POST請求
- (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);
}
使用上面的代碼獲取設備令牌,並獲取遠程通知,確保遠程通知的背景模式為開
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.