【问题标题】:Apple Notification Device Token changeApple 通知设备令牌更改
【发布时间】:2017-02-22 10:26:36
【问题描述】:

我在更新我的应用后遇到了问题。我正在获取这种格式的设备令牌 C43461D5-E9CB-4C10-81F8-020327A07A62 并且通知不起作用。

之前,我收到过这种格式的通知: 2add70865401171c7ca1d3b3957b719eaf721fef8f8d7a52bc91ef8a872cc004

我确实允许应用程序通知,并且我没有更改后端的任何内容。谁能指导我为什么它不起作用?

didFinishLaunchingWithOptions中的代码:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

获取设备令牌:

NSString *deviceTokenID = [[NSUserDefaults standardUserDefaults] objectForKey:DEVICE_TOKEN_PUSH_NOTI];
if ([deviceTokenID isEqualToString:@""] || deviceTokenID == nil) {
    NSString *tempApplicationUUID  = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    [dics setObject:tempApplicationUUID forKey:@"cust_device_id"];
} else {
    [dics setObject:[[NSUserDefaults standardUserDefaults] objectForKey:DEVICE_TOKEN_PUSH_NOTI] forKey:@"cust_device_id"];
}

我收到以下错误:

Code=3000 "找不到有效的 'aps-environment' 授权字符串 应用程序" UserInfo={NSLocalizedDescription=无效 为应用程序找到“aps-environment”权利字符串},无效 找到应用程序的“aps-environment”权利字符串

【问题讨论】:

  • 你能显示你的didfinishlaunch代码吗
  • 同时显示获取设备令牌的代码。
  • "if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }"
  • 显示您获取设备令牌的代码并在您的代码中显示,请勿评论。
  • 你如何获取设备令牌?

标签: ios objective-c xcode apple-push-notifications


【解决方案1】:

请去

点击 .xcodeproj -> 功能 -> 启用推送通知

希望成功

【讨论】:

    【解决方案2】:

    点击 .xcodeproj -> 功能 -> 启用推送通知

    【讨论】:

    • @Jack 这仅用于推送通知的后台模式。这与为什么没有推送通知无关。简而言之,这是 NOT 用于 Enable 推送通知的。这只是用于在后台模式下处理通知
    • @RajanMaheshwari 这个答案与错误获取有关。所以我只是给出那个错误的答案“没有为应用程序找到有效的'aps-environment'权利字符串”。
    【解决方案3】:
    NSString *tempApplicationUUID  = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    

    UUIDString 是唯一的设备 ID,但对于 Apple 推送通知,您需要从 APNS 取回设备令牌,因此使用以下方法可以获得 64 位设备令牌并获得通知。

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    
    
    {
        NSString *devToken = [[[[deviceToken description]
                                stringByReplacingOccurrencesOfString:@"<"withString:@""]
                               stringByReplacingOccurrencesOfString:@">" withString:@""]
                              stringByReplacingOccurrencesOfString: @" " withString: @""];
    
    
        NSString *str = [NSString
                         stringWithFormat:@"Device Token=%@",devToken];
    
        [[NSUserDefaults standardUserDefaults]setObject:devToken forKey:@"DeviceToken"];
    
    }
    

    【讨论】:

    • 请不要使用[NSData description]!这不是字符串化推送令牌的好方法,因为描述可以随时更改。
    • 嗯,iOS 13 中似乎发生了变化。
    猜你喜欢
    • 2012-06-30
    • 2015-02-28
    • 1970-01-01
    • 2017-03-03
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多