【发布时间】:2017-01-22 06:28:13
【问题描述】:
我开发了一个应用程序,实现了推送通知。目前它在苹果商店上架。直到 iOS 9 推送工作正常,但在 iOS 10 之后它无法正常工作。
代码有什么问题?
【问题讨论】:
-
“代码有什么问题?” - 什么代码?
-
使推送在 iOS9 上工作的那个显然
标签: ios push-notification ios10
我开发了一个应用程序,实现了推送通知。目前它在苹果商店上架。直到 iOS 9 推送工作正常,但在 iOS 10 之后它无法正常工作。
代码有什么问题?
【问题讨论】:
标签: ios push-notification ios10
对于使用 xCode 8 GM 的 iOS 10。
我已使用适用于 iOS 10 的 xCode 8 GM 通过以下步骤解决了我的问题:
1) 在目标中,在功能下启用推送通知以添加推送通知权利。
2) 在您的应用中实现 UserNotifications.framework。在您的 AppDelegate 中导入 UserNotifications.framework。
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
3) 在 didFinishLaunchingWithOptions 方法中分配 UIUserNotificationSettings 并实现 UNUserNotificationCenter 委托。
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
return YES;
}
4) 现在终于实现了这两个委托方法。
//============iOS 10=============
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
//Called when a notification is delivered to a foreground app.
NSLog(@"Userinfo %@",notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
//Called to let your app know which action was selected by the user for a given notification.
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
请保留您在 iOS 9 中使用的代码, 仅使用 UserNotifications.framework 添加代码行以支持 iOS 10 的推送通知。
【讨论】:
在 iOS 上,应用程序通过调用 UIApplication 的 registerUserNotificationSettings: 策略来接近客户端以获得推送警告的授权。
应用程序为UIApplication (iOS) 调用registerForRemoteNotifications: 技术或NSApplication (OS X) 的策略registerForRemoteNotificationTypes:。
应用程序对UIApplicationDelegate (iOS) 或NSApplicationDelegate (OS X) 执行application:didRegisterForRemoteNotificationsWithDeviceToken: 技术,以获取推送收益产生的一种小工具令牌。
应用程序对UIApplicationDelegate (iOS) 或NSApplicationDelegate (OS X) 执行application:didFailToRegisterForRemoteNotificationsWithError: 技术,以在注册失败时出错。
【讨论】:
不要忘记,在测试时,您必须使用sandbox 地址才能让您的通知生效。
【讨论】:
我遇到了 iOS 10 静默推送通知的问题。在 iOS9 和更早的版本中,发送一个包含额外数据字段但数据中有一个空的aps 属性的推送通知可以正常工作。但是在 iOS10 中,带有空 aps 属性的推送通知根本不会命中 didReceiveRemoteNotification 应用程序委托方法,这意味着我所有的静默推送通知(我们只是在内部用于在应用程序打开时触发操作的通知)停止工作iOS10。
通过在推送通知的aps 部分添加至少一个属性,我能够在不向我的应用推送更新的情况下解决此问题,在我的情况下,我刚刚添加了badge: 0,我的静默推送通知又开始工作了在 iOS 10 中。我希望这对其他人有帮助!
【讨论】:
priority 5 可能会导致推送根本不显示,如文档中所示:"They are throttled, and in some cases are not delivered."!
@Ashish Shah 代码的 swift 3 版本是:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//notifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
// Fallback on earlier versions
}
return true
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
}
【讨论】:
【讨论】: