iOS 10 的推送通知问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39490605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:08:25  来源:igfitidea点击:

Push notification issue with iOS 10

iospush-notificationios10

提问by Mohsin Sabasara

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.

我开发了一个应用程序,因为我已经实现了推送通知。目前它在苹果商店上线。到 iOS 9 推送工作正常,但在 iOS 10 之后它不起作用。

What is the issue with the code?

代码有什么问题?

回答by Ashish Shah

For iOS 10 using xCode 8 GM.

对于使用 xCode 8 GM 的 iOS 10。

I have resolved my issue with following steps using xCode 8 GM for iOS 10:

我已使用适用于 iOS 10 的 xCode 8 GM 通过以下步骤解决了我的问题:

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) 将 UserNotifications.framework 实施到您的应用程序中。在您的 AppDelegate 中导入 UserNotifications.framework。

#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@end

3) In didFinishLaunchingWithOptions method assign UIUserNotificationSettingsand implement UNUserNotificationCenterdelegate.

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 支持 iOS 10 的推送通知。

回答by AiOsN

Everything was working fine before iOS 10, In my case only capabilities settings cause this issue.

在 iOS 10 之前一切正常,就我而言,只有功能设置会导致此问题。

It must be turned on for push notification.

必须为推送通知打开它。

enter image description here

在此处输入图片说明

回答by jakedunc

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 apsattribute in the data worked fine. But in iOS10 a push notification with an empty apsattribute 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 apspart of the push notification, in my case I just added badge: 0and my silent push notifications started working again in iOS 10. I hope this helps someone else!

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

回答by Lucho

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

    }

回答by Ashkan Ghodrat

Don't forget, when testing, you must use sandboxaddress for your notifications to work.

不要忘记,在测试时,您必须使用sandbox地址才能使通知正常工作。

回答by anjalihiaim

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

应用程序调用registerForRemoteNotifications:的技术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.

应用程序application:didRegisterForRemoteNotificationsWithDeviceToken:UIApplicationDelegate(iOS) 或NSApplicationDelegate(OS X)执行技术以获取由推送收益生成的一种小工具令牌。

The application executes the application:didFailToRegisterForRemoteNotificationsWithError:technique for UIApplicationDelegate(iOS) or NSApplicationDelegate(OS X) to get a blunder if the enrolment fizzled.

该应用程序执行application:didFailToRegisterForRemoteNotificationsWithError:的技术UIApplicationDelegate(IOS)或NSApplicationDelegate(OS X),以得到一个错误,如果招生告吹。