iOS 10 后未收到推送通知
Push notification not receiving in iOS 10
我的应用程序在应用商店中。推送通知在 iOS 9 中运行良好,但在 iOS 10 中不起作用。我没有收到 iOS 10 台设备的任何推送通知。我已经检查了服务器中的设备令牌和证书。全部正确。我还检查了设置应用程序中的通知属性。一切都很好。但是我没有收到任何通知。我只是关闭和打开我的应用程序的通知。然后我打开我的应用程序来检查设备令牌是否正在更改。它已更改并更新到我的服务器。然后我正确地收到通知。现在我的设备运行良好。
我担心这会影响所有用户还是只影响我。任何人找到合适的解决方案请告诉我。
提前致谢
需要对 iOS 10 和 xCode 8 GM 进行一些更改
您需要实施 UserNotifications.framework 及其委托方法才能获得推送通知的工作。
我已经使用新 UserNotifications.framework 解决了我的问题。
请遵循此 link :
我们需要为 iOS 10.
更改一些代码
Appdelegate.h
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
检查os版本
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
注册通知
- (void)registerForRemoteNotifications {
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];
}
}];
}
else {
// Code for old versions
}
}
Handel 委托方法
//foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info : %@",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
completionHandler();
}
在 iOS 10 上需要添加 Push Notifications 授权,因此如果您 "Fix Issue" 在 Capabilities 中,问题将自动解决。
"UserNotifications" 在 iOS10 中不是强制性的。 “UIUserNotificationSettings”在 iOS10 中仍然有效。
如果您有以下代码,它应该可以在 iOS10.
中运行
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
但是,如果您使用 Xcode8 及更高版本构建,请确保您的 entitlements 中包含以下条目.在“Capabilities”中启用 "Push Notifications" 后,将自动添加此条目。
<key>aps-environment</key>
<string>development</string>
在发布分发版本中,这将自动更改为
正在关注
<key>aps-environment</key>
<string>production</string>
我的应用程序在应用商店中。推送通知在 iOS 9 中运行良好,但在 iOS 10 中不起作用。我没有收到 iOS 10 台设备的任何推送通知。我已经检查了服务器中的设备令牌和证书。全部正确。我还检查了设置应用程序中的通知属性。一切都很好。但是我没有收到任何通知。我只是关闭和打开我的应用程序的通知。然后我打开我的应用程序来检查设备令牌是否正在更改。它已更改并更新到我的服务器。然后我正确地收到通知。现在我的设备运行良好。
我担心这会影响所有用户还是只影响我。任何人找到合适的解决方案请告诉我。
提前致谢
需要对 iOS 10 和 xCode 8 GM 进行一些更改 您需要实施 UserNotifications.framework 及其委托方法才能获得推送通知的工作。
我已经使用新 UserNotifications.framework 解决了我的问题。
请遵循此 link :
我们需要为 iOS 10.
更改一些代码Appdelegate.h
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
检查os版本
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
注册通知
- (void)registerForRemoteNotifications {
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];
}
}];
}
else {
// Code for old versions
}
}
Handel 委托方法
//foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info : %@",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
completionHandler();
}
在 iOS 10 上需要添加 Push Notifications 授权,因此如果您 "Fix Issue" 在 Capabilities 中,问题将自动解决。
"UserNotifications" 在 iOS10 中不是强制性的。 “UIUserNotificationSettings”在 iOS10 中仍然有效。
如果您有以下代码,它应该可以在 iOS10.
中运行[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
但是,如果您使用 Xcode8 及更高版本构建,请确保您的 entitlements 中包含以下条目.在“Capabilities”中启用 "Push Notifications" 后,将自动添加此条目。
<key>aps-environment</key>
<string>development</string>
在发布分发版本中,这将自动更改为 正在关注
<key>aps-environment</key>
<string>production</string>