經(jīng)常有開發(fā)小伙伴向我們提問關于使用華為推送給蘋果手機推送消息的問題,那么首先華為推送到底支不支持蘋果手機呢?答案可以肯定地告訴你:可以。
首先你需要提前準備好開發(fā)環(huán)境:
1)安裝Xcode 10.1或更高版本。
2)安裝CocoaPods 1.4.0或更高版本。
3)準備一臺用于測試的iPhone設備或者模擬器。
開發(fā)環(huán)境準備好了,接下來就可以準備開發(fā)啦!
在開發(fā)應用前,需要在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
1. 在Xcode中為您的項目啟用推送服務,啟用“Application Targ > Signing&Capabilities”中的"Push Notifications",勾選“Application Targ > Signing&Capabilities > Background Modes”中的“Remote notifications”和“Background processing”。
2. 向APNs(蘋果推送服務)發(fā)起用戶允許發(fā)送推送通知的請求。
```
[[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];
});
}
}];
}
}];
```
用戶需要在應用程序點擊“允許”才可以接受推送消息。
3.上述步驟成功后,需要獲取device token(蘋果設備的唯一標識)。獲取device token后需要去掉其中的特殊符號,大于等于iOS13版本和小于iOS13版本的device token格式有所差別,可參考如下代碼進行處理:
```
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PushDemoLog(@"suceess get token:{%@}", deviceToken);
// 判斷iOS設備系統(tǒng)版本
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后將其作為入?yún)@取華為推送服務Token:
```
NSString *apnsToken = @"yourApnsToken";
NSString *huaweiToken = [[HmsInstanceId getInstance] getToken:apnsToken];
```
更多應用開發(fā)步驟參見:
接下來給大家分享一位開發(fā)者在論壇上提問關于蘋果手機接入華為push的問題:“我想使用華為的消息推送服務,給蘋果手機推送消息,申請應用后,缺少App Secret,無法獲取到access_token,怎么解“
推送接口以access_token鑒權,如圖:
獲取access_token的接口,如圖:
我的項目配置,不顯示app secret,如圖:
看安卓應用的配置,相同位置是有app secret的,如圖:
解決方法:
需要在相同項目下再建一個Android的應用,用Android應用的appId和appSecret去申請access_token就可以了。
那么,用安卓應用獲取到access_token為蘋果應用推送消息,是可以的嗎?
答案依舊是——可以的!