[iOS] 通知详解-相关问题

1. Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授权字符串" UserInfo={NSLocalizedDes

在启用通知的时候,证书的配置,开发中账号,一切都设置妥当,在编译时还是发生下面的错误

Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授权字符串" UserInfo={NSLocalizedDes

现在新版的 Xcode 需要在 Capabilities 打开 Push Notifications 开关,并且其下的Steps 都是对号,如果没有证书,或者Apple ID 没有通知服务,都是会报错的

[iOS] 通知详解-相关问题_第1张图片
Xcode 启用通知

这时再去编译运行项目,就没有问题了!

新版Xcode在 Signing&Capabilities 中添加:

[iOS] 通知详解-相关问题_第2张图片

在弹出框中搜索 noti:


[iOS] 通知详解-相关问题_第3张图片

然后,双击 Push Notifications 即可添加;
同样方式,添加 Background Modes,添加完成如下图所示:

[iOS] 通知详解-相关问题_第4张图片
2. You've implemented -[ application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.

You've implemented -[ application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.

需要在 Capabilities 打开Background Modes开关,并勾选 Remote notifications

[iOS] 通知详解-相关问题_第5张图片
3. 注册远程推送时,不回调方法didRegisterForRemoteNotificationsWithDeviceToken

这是在注册远程推送时遇到的问题,不管是成功的方法,还是失败的方法,都没有回调;授权后使用下面的方法注册远程通知时:

[[UIApplication sharedApplication] registerForRemoteNotifications];

注册之后没反应了,原因暂时未知,我是从以下几个方面进行尝试:

  1. 确保证书无误:在开发者平台重新制作证书
  2. 确保项目配置无误:Xcode 11 之后的配置入口有变动


    [iOS] 通知详解-相关问题_第6张图片

点击 “1”处的 Capabitity ,在弹出框中搜索 push,双击添加;然后输入background,双击添加 “Background Modes”,勾选 Remote notification;

  1. 以上无误后,重新在设备上运行项目,此时如果没反应,稍等一会儿,也许会有惊喜;
  2. 第三步还是没反应就更换一个设备,重试第三步;

我是换了一台设备进行测试,无意间运行项目后去干其它的了,等再看的时候,意外的发现成功获取到了deviceToken;添加友盟相关代码,测试推送,能够正常收到推送消息,说明项目的配置及证书是没问题的;此时再去尝试另一台设备,还是无法获取到 deviceToken,我也是佛了,还真和设备有关。。。

4. iOS 13 deviceToken 解析

iOS 13之后,deviceToken 有变化,解析方法如下
OC版本:

+ (NSString * __nullable) deviceTokenStringFromData:(NSData * __nullable) deviceToken {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
        if (![deviceToken isKindOfClass:[NSData class]]) {
            
            return nil;
        }
        const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
        NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        
        return strToken;
    } else {
        NSString *token = [NSString
                       stringWithFormat:@"%@",deviceToken];
        token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        return token;
    }
}

Swift 版本

func diveTokenStringFromData(_ deviceToken: Data) -> String {
        var token = ""
        
        let tokenBytes = [UInt8](deviceToken)
        for i in tokenBytes {
            token += String(format: "%02x",I&0x000000FF)
        }
        
        return token
    }

3. 友盟推送相关问题

这里记录友盟推送相关问题

3.1. 测试模式发送消息,设备收不到

该情况是通过友盟控制台发送

需要查看设备是否添加到测试设备 中,另外,确认已添加的测试设备的deviceToken 是否正确,这里的token是会变化的,如果手机上卸载了应用,重新安装,这个值是变化的,所以需要确认该值是否正确。

获取deviceToken 字符串方法:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
        if (![deviceToken isKindOfClass:[NSData class]]) {
            //记录获取token失败的描述
            return nil;
        }
        const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
        NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        
        return strToken;
    } else {
        NSString *token = [NSString
                       stringWithFormat:@"%@",deviceToken];
        token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        return token;
    }
3.2. 后台发送消息报错: IP 白名单未添加
{"ret":"FAIL","data":{"error_msg":"IP白名单尚未添加,请到网站后台添加您的服务器IP或关闭IP白名单功能","error_code":"2004"}}

该问题需要在友盟控制台-应用详情页添加服务器白名单,或者直接关闭:

[iOS] 通知详解-相关问题_第7张图片

修改后不要忘记点击 保存!!!

3.3. 通过后台发送的定向消息,设备收不到

该问题是通过别名发送定向消息时,设备收不到

A 确定没有 3.1. 的问题
B 通过联调发现是后台的字段设置有问题

后台设置为:

xx.setAlias(alias:"userid", aliasType:123456)

实际应该这样:

xx.setAlias(alias:"123456", aliasType:"userid")

aliasType 才是自定义的字段, alias 是需要设置的别名

你可能感兴趣的:([iOS] 通知详解-相关问题)