推送iOS 10的通知问题

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的推送通知.

  • -willPresentNotification ==当应用程序是前台并且推送时调用它,它可用于配置显示或不显示收费推送-didReceiveNotificationResponse ==从通知中心打开通知时调用 (3认同)
  • 您为什么不支票授予?在(granted == NO)的情况下,回调发送nil表示错误。这表示用户未注册。 (2认同)

AiO*_*OsN 24

iOS 10之前一切正常,在我的情况下,只有功能设置会导致此问题.

必须打开它以进行推送通知.

在此输入图像描述


jak*_*unc 18

iOS 10静音推送通知存在问题.在iOS9及更早版本中,发送具有其他数据字段但aps在数据中具有空属性的推送通知正常工作.但是在iOS10中,具有空aps属性的推送通知根本没有达到didReceiveRemoteNotification app委托方法,这意味着我的所有静默推送通知(我们在内部用于在应用程序打开时触发操作的通知)在iOS10中停止工作.

我能够通过在aps推送通知的部分添加至少一个属性来推动对我的应用程序的更新来解决这个问题,在我的情况下我刚刚添加badge: 0并且我的静态推送通知在iOS 10中再次开始工作.我希望这有助于某人其他!


Luc*_*cho 7

@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)