Moh*_*ara 55 push-notification ios ios10
我已经开发了一个应用程序,因为我实现了推送通知.目前它在苹果商店上市.iOS 9推送工作正常,但iOS 10之后无法正常工作.
代码有什么问题?
Ash*_*hah 117
适用于使用xCode 8 GM的iOS 10.
我已经使用xCode 8 GM for iOS 10的以下步骤解决了我的问题:
1)在目标中,在功能下启用推送通知以添加推送通知权利.
2)在您的应用中实施UserNotifications.framework.在AppDelegate中导入UserNotifications.framework.
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
请保留代码,因为它是用于iOS 9的,仅添加代码行以支持使用UserNotifications.framework支持iOS 10的推送通知.
jak*_*unc 18
iOS 10静音推送通知存在问题.在iOS9及更早版本中,发送具有其他数据字段但aps
在数据中具有空属性的推送通知正常工作.但是在iOS10中,具有空aps
属性的推送通知根本没有达到didReceiveRemoteNotification app委托方法,这意味着我的所有静默推送通知(我们在内部用于在应用程序打开时触发操作的通知)在iOS10中停止工作.
我能够通过在aps
推送通知的部分添加至少一个属性来推动对我的应用程序的更新来解决这个问题,在我的情况下我刚刚添加badge: 0
并且我的静态推送通知在iOS 10中再次开始工作.我希望这有助于某人其他!
@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) {
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46169 次 |
最近记录: |