I've developed one application in that i've implemented push notification. Currently it's live on apple store. Upto iOS 9 push is working fine but after iOS 10 it is not working.
我开发了一个应用程序,实现了push notification。目前它是在苹果商店直播的。Upto ios9 push运行良好,但ios10之后就不工作了。
What is the issue with the code?
代码有什么问题?
6 个解决方案
#1
113
For iOS 10 using xCode 8 GM.
对于ios10使用xCode 8 GM。
I have resolved my issue with following steps using xCode 8 GM for iOS 10:
我已经通过以下步骤解决了我的问题:
1) In the targets, under Capabilities enable Push Notifications to add Push Notifications Entitlements.
1)在目标中,在能力项下允许推送通知添加推送通知授权。
2) Implement UserNotifications.framework into your app. Import UserNotifications.framework in your AppDelegate.
2)在app中实现usernotiations .framework,在AppDelegate中导入UserNotifications.framework。
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
3) In didFinishLaunchingWithOptions method assign UIUserNotificationSettings
and implement UNUserNotificationCenter
delegate.
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) Now finally implement this two delegate methods.
4)现在最后实现这两个委托方法。
//============For iOS 10=============
/ / = = = = = = = = = = = = 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);
}
Please remain the code as it is you are using for iOS 9, Only add lines of code to support Push notification for iOS 10 using UserNotifications.framework.
请保留iOS 9的代码,只添加几行代码,使用UserNotifications.framework支持ios10的推送通知。
#2
#3
18
I had an issue with iOS 10 silent push notifications. In iOS9 and earlier, sending a push notification that had additional data fields but had an empty aps
attribute in the data worked fine. But in iOS10 a push notification with an empty aps
attribute does not hit the didReceiveRemoteNotification app delegate method at all, meaning that all my silent push notifications (notifications that we just use internally to trigger actions while the app is open) stopped working in iOS10.
我在iOS 10无声推送通知上有问题。在iOS9和更早的版本中,发送具有附加数据字段但数据中有一个空aps属性的推送通知效果良好。但是在iOS10中,一个带有空aps属性的推送通知根本没有击中didReceiveRemoteNotification应用程序委托方法,这意味着我所有的无声推送通知(我们只在内部使用的通知在程序打开时触发动作)在iOS10中都停止工作。
I was able to fix this without pushing an update to my app by adding at least one attribute to the aps
part of the push notification, in my case I just added badge: 0
and my silent push notifications started working again in iOS 10. I hope this helps someone else!
我可以通过在推送通知的aps部分添加至少一个属性来修复这个问题,在我的例子中,我刚刚添加了badge: 0,我的无声推送通知在ios10中重新开始工作。我希望这能帮助别人!
#4
7
The swift 3 version of @Ashish Shah code is:
@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) {
}
#5
0
Don't forget, when testing, you must use sandbox
address for your notifications to work.
不要忘记,在测试时,必须使用沙箱地址作为通知来工作。
#6
-1
On iOS, the application approaches the client for authorization to get push warnings by calling the registerUserNotificationSettings:
strategy for UIApplication
.
在iOS中,应用程序通过调用registerUserNotificationSettings: UIApplication策略来接近客户端,以获得推送警告。
The application calls the registerForRemoteNotifications:
technique for UIApplication
(iOS) or the strategy registerForRemoteNotificationTypes:
of NSApplication
(OS X).
应用程序调用registerforremotenotiation:技术用于UIApplication (iOS)或策略registerForRemoteNotificationTypes: NSApplication (OS X)。
The application executes the application:didRegisterForRemoteNotificationsWithDeviceToken:
technique for UIApplicationDelegate
(iOS) or NSApplicationDelegate
(OS X) to get the one of a kind gadget token produced by the push benefit.
应用程序执行应用程序:didRegisterForRemoteNotificationsWithDeviceToken: UIApplicationDelegate (iOS)或NSApplicationDelegate (OS X)的技术,以获得push benefit生成的一种小工具令牌。
The application executes the application:didFailToRegisterForRemoteNotificationsWithError:
technique for UIApplicationDelegate
(iOS) or NSApplicationDelegate
(OS X) to get a blunder if the enrolment fizzled.
应用程序执行应用程序:didFailToRegisterForRemoteNotificationsWithError:如果注册失败,UIApplicationDelegate (iOS)或NSApplicationDelegate (OS X)的技术会出错。
#1
113
For iOS 10 using xCode 8 GM.
对于ios10使用xCode 8 GM。
I have resolved my issue with following steps using xCode 8 GM for iOS 10:
我已经通过以下步骤解决了我的问题:
1) In the targets, under Capabilities enable Push Notifications to add Push Notifications Entitlements.
1)在目标中,在能力项下允许推送通知添加推送通知授权。
2) Implement UserNotifications.framework into your app. Import UserNotifications.framework in your AppDelegate.
2)在app中实现usernotiations .framework,在AppDelegate中导入UserNotifications.framework。
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
3) In didFinishLaunchingWithOptions method assign UIUserNotificationSettings
and implement UNUserNotificationCenter
delegate.
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) Now finally implement this two delegate methods.
4)现在最后实现这两个委托方法。
//============For iOS 10=============
/ / = = = = = = = = = = = = 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);
}
Please remain the code as it is you are using for iOS 9, Only add lines of code to support Push notification for iOS 10 using UserNotifications.framework.
请保留iOS 9的代码,只添加几行代码,使用UserNotifications.framework支持ios10的推送通知。
#2
24
Everything was working fine before iOS 10, In my case only capabilities settings cause this issue.
在ios10之前一切正常,在我的例子中,只有功能设置导致了这个问题。
It must be turned on for push notification.
它必须为推送通知打开。
#3
18
I had an issue with iOS 10 silent push notifications. In iOS9 and earlier, sending a push notification that had additional data fields but had an empty aps
attribute in the data worked fine. But in iOS10 a push notification with an empty aps
attribute does not hit the didReceiveRemoteNotification app delegate method at all, meaning that all my silent push notifications (notifications that we just use internally to trigger actions while the app is open) stopped working in iOS10.
我在iOS 10无声推送通知上有问题。在iOS9和更早的版本中,发送具有附加数据字段但数据中有一个空aps属性的推送通知效果良好。但是在iOS10中,一个带有空aps属性的推送通知根本没有击中didReceiveRemoteNotification应用程序委托方法,这意味着我所有的无声推送通知(我们只在内部使用的通知在程序打开时触发动作)在iOS10中都停止工作。
I was able to fix this without pushing an update to my app by adding at least one attribute to the aps
part of the push notification, in my case I just added badge: 0
and my silent push notifications started working again in iOS 10. I hope this helps someone else!
我可以通过在推送通知的aps部分添加至少一个属性来修复这个问题,在我的例子中,我刚刚添加了badge: 0,我的无声推送通知在ios10中重新开始工作。我希望这能帮助别人!
#4
7
The swift 3 version of @Ashish Shah code is:
@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) {
}
#5
0
Don't forget, when testing, you must use sandbox
address for your notifications to work.
不要忘记,在测试时,必须使用沙箱地址作为通知来工作。
#6
-1
On iOS, the application approaches the client for authorization to get push warnings by calling the registerUserNotificationSettings:
strategy for UIApplication
.
在iOS中,应用程序通过调用registerUserNotificationSettings: UIApplication策略来接近客户端,以获得推送警告。
The application calls the registerForRemoteNotifications:
technique for UIApplication
(iOS) or the strategy registerForRemoteNotificationTypes:
of NSApplication
(OS X).
应用程序调用registerforremotenotiation:技术用于UIApplication (iOS)或策略registerForRemoteNotificationTypes: NSApplication (OS X)。
The application executes the application:didRegisterForRemoteNotificationsWithDeviceToken:
technique for UIApplicationDelegate
(iOS) or NSApplicationDelegate
(OS X) to get the one of a kind gadget token produced by the push benefit.
应用程序执行应用程序:didRegisterForRemoteNotificationsWithDeviceToken: UIApplicationDelegate (iOS)或NSApplicationDelegate (OS X)的技术,以获得push benefit生成的一种小工具令牌。
The application executes the application:didFailToRegisterForRemoteNotificationsWithError:
technique for UIApplicationDelegate
(iOS) or NSApplicationDelegate
(OS X) to get a blunder if the enrolment fizzled.
应用程序执行应用程序:didFailToRegisterForRemoteNotificationsWithError:如果注册失败,UIApplicationDelegate (iOS)或NSApplicationDelegate (OS X)的技术会出错。