接入华为推送用API给iOS应用发消息时,如何获取access_token?

经常有开发小伙伴向我们提问关于使用华为推送给苹果手机推送消息的问题,那么华为推送到底支不支持苹果手机呢?答案是肯定的。

华为推送服务支持的设备详见下图:

1

苹果手机如何接入华为推送?

首先需要提前准备好开发环境:

1)安装Xcode 10.1或更高版本。

2)安装CocoaPods 1.4.0或更高版本。

3)准备一台用于测试的iPhone设备或者模拟器。

开发环境准备好了,接下来就可以准备开发啦!

在开发应用前,需要在AppGallery Connect中配置相关信息,准备iOS推送消息凭证以及配置iOS推送代理权益。具体准备方法请参见:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides-V5/ios-dev-prepare-0000001062940204-V5#ZH-CN_TOPIC_0000001124013099__section113157170295?ha_source=hms1

如何获取Token?

1. 在Xcode中为您的项目启用推送服务,启用“Application Targ > Signing&Capabilities”中的"Push Notifications",勾选“Application Targ > Signing&Capabilities > Background Modes”中的“Remote notifications”和“Background processing”。

2. 向APNs(苹果推送服务)发起用户允许发送推送通知的请求。

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
 
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError *_Nullable error) {
    if (granted) {
        // 授权成功
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *_Nonnull settings) {
            if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
               dispatch_async(dispatch_get_main_queue(), ^{
                 NSLog(@"grant authorized");
                 [[UIApplication sharedApplication] registerForRemoteNotifications];
               });
            }
        }];
    }
}];

用户需要在应用程序点击“允许”才可以接受推送消息。

21

3.上述步骤成功后,需要获取device token(苹果设备的唯一标识)。获取device token后需要去掉其中的特殊符号,大于等于iOS13版本和小于iOS13版本的device token格式有所差别,可参考如下代码进行处理:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    PushDemoLog(@"suceess get token:{%@}", deviceToken);
    // 判断iOS设备系统版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
        if (![deviceToken isKindOfClass:[NSData class]]) {
            return;
        }
        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])];
        PushDemoLog(@">=ios13 My FINAL TOKEN is:%@", strToken);
        APN_TOKEN = strToken;
        return;
    } else {
        NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
        token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        PushDemoLog(@"My FINAL TOKEN is %@", token);
        APN_TOKEN = token;
    }
}

4.成功处理device token后将其作为入参获取华为推送服务Token:

NSString *apnsToken = @"yourApnsToken";
NSString *huaweiToken = [[HmsInstanceId getInstance] getToken:apnsToken];

更多应用开发步骤参见:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/ios-dev-guides-0000001062462396?ha_source=hms1

问题分享:使用华为推送给苹果手机推送消息,无法获取到access_token怎么解?

这是华为开发者论坛一位开发小伙伴的提问:我想使用华为的消息推送服务,给苹果手机推送消息,申请应用后,缺少App Secret,无法获取到access_token,怎么解?

  • 推送接口以access_token鉴权,如图:

2

  • 获取access_token的接口,如图:

2

  • 项目配置不显示app secret,如图:

6

  • 看安卓应用的配置,相同位置是有app secret的,如图:

8

解决方法

在相同项目下再建一个Android的应用,用Android应用的appId和appSecret去申请access_token就可以了。

通过这个方法安卓应用就可以为苹果应用推送消息。

我们的服务在持续优化更新中,将会为开发者带来更便捷的操作体验,敬请期待~

欲了解更多详情,请参阅华为推送服务官网:https://developer.huawei.com/consumer/cn/hms/huawei-pushkit%20?ha_source=hms1

>>访问华为开发者联盟官网,了解更多相关内容

>>获取开发指导文档

>>华为移动服务开源仓库地址:GitHubGitee

关注我们,第一时间了解华为移动服务最新技术资讯~

猜你喜欢

转载自blog.csdn.net/HUAWEI_HMSCore/article/details/115119997