有沒有人可以向我解釋如何使用iOS10提供的新UserNotification框架註冊遠程推送通知並獲取令牌?註冊遠程通知ios10並捕獲apn令牌
1
A
回答
3
您可以註冊通知,例如您已經從iOS 8(這是少數幾個未更改的通知API)之一執行的操作。
首先,在AppDelegate中的application:didFinishLaunchingWithOptions:
方法,要求授權爲您的應用程序:
UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in
//here you can check the correct authorization
}
這將顯示通常的「應用程序想向你發送通知」的警報。新的requestAuthorization
方法的主要改進是您可以管理在關閉中直接點擊「允許/不允許」按鈕的行爲。
接下來,註冊遠程通知與UIApplication
的registerForRemoteNotifications
方法可以從iOS的8:
UIApplication.shared().registerForRemoteNotifications()
...最後管理與您的通知服務器註冊(如亞馬遜,OneSignal等.. )
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//if you need the token as a string, do this:
let tokenString = String(data: deviceToken, encoding: .utf8)
//call the notifications server for sending the device token
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Application failed to register for remote notifications")
}
1
隨着Objective-C的方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registerForRemoteNotification];
. . .
}
- (void)registerForRemoteNotification {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
UNUserNotificationCenter *uncenter = [UNUserNotificationCenter currentNotificationCenter];
[uncenter setDelegate:self];
[uncenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionBadge+UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(@"%@" , granted ? @"success to request authorization." : @"failed to request authorization .");
}];
[uncenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%s\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), settings);
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
//TODO:
} else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
//TODO:
} else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
//TODO:
}
}];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationType types = UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
UIRemoteNotificationType types = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}
#pragma clang diagnostic pop
}
這是演示:iOS10AdaptationTips。
相關問題
- 1. IOS推送通知smartface 4.3註冊和遠程令牌
- 2. 清除遠程APN通知
- 3. 如何在Swift中註冊遠程通知時獲取設備令牌
- 4. 如何註冊推送通知? ios10
- 5. 無法註冊遠程通知
- 6. 註冊遠程通知無聲無息
- 7. 註冊爲本地和遠程通知
- 8. FCM註冊令牌
- 9. iDevice未註冊APN
- 10. Firebase中的未註冊註冊令牌
- 11. 使用Firebase通知手動註冊設備令牌
- 12. 如何獲取註冊令牌?
- 13. Android GCM - 未獲取註冊令牌
- 14. 如何獲取用於Android的FCM推送通知的註冊令牌?
- 15. 在設備註冊推送通知後,iOS獲取設備令牌?
- 16. GCM:註冊ID或令牌
- 17. Firebase註冊令牌爲空?
- 18. APN令牌能否改變?
- 19. 我應該自己保存Firebase令牌還是在註冊遠程通知時自動完成?
- 20. 捕獲401並刷新Firebase Id令牌
- 21. 遠程註冊表
- 22. 註冊遠程通知不會產生任何效果
- 23. 註冊用戶登錄時的遠程通知
- 24. 註冊遠程通知不能在Xcode工作6
- 25. 如何使用Swift 2註冊iOS 7中的遠程通知?
- 26. Apple遠程通知 - 處理令牌更改
- 27. 遠程註冊/取消註冊BroadcatReceiver
- 28. 通過Google Apps遠程註冊域名
- 29. 如何接收iOS10遠程用戶通知
- 30. PushSharp庫併發送通知到APN