在 iOS 10 中未收到推送通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39504334/
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:09:39  来源:igfitidea点击:

Push notification not receiving in iOS 10

iospush-notificationapple-push-notificationsios10

提问by Yogesh Mv

My App is in Appstore. Push notification is working fine in iOS 9, but in iOS 10 it is not working. I am not receiving any push notification for iOS 10 devices. I have checked the device token and certificate in my server. All are correct. I have also checked the notification properties in settings app. All are fine. But I didn't receive any notification. I just switch OFF and ON the notification for my app. And I opened my app to check whether device token is changing or not. It is changed and updated to my server. Then I am receiving notification properly. It is working fine now for my device.

我的应用程序在 Appstore 中。推送通知在 iOS 9 中运行良好,但在 iOS 10 中不起作用。我没有收到 iOS 10 设备的任何推送通知。我已经检查了服务器中的设备令牌和证书。全部正确。我还检查了设置应用程序中的通知属性。一切都很好。但是我没有收到任何通知。我只是关闭和打开我的应用程序的通知。我打开我的应用程序以检查设备令牌是否正在更改。它已更改并更新到我的服务器。然后我正确接收通知。现在我的设备运行良好。

I am worried whether this will affect for all users or only me. Anyone find the proper solution please let me know.

我担心这会影响所有用户还是只影响我。任何人找到合适的解决方案,请告诉我。

Thanks in advance

提前致谢

采纳答案by Ashish Shah

Need some changes for iOS 10 with xCode 8 GM You need to implement UserNotifications.framework and their delegate methods to get work of push notifications.

需要对带有 xCode 8 GM 的 iOS 10 进行一些更改您需要实现 UserNotifications.framework 及其委托方法以获取推送通知的工作。

I have resolved my issue using new UserNotifications.framework. Please follow this link : Push notification issue with iOS 10

我已经使用新的 UserNotifications.framework 解决了我的问题。请点击此链接:iOS 10 的推送通知问题

回答by arango_86

"UserNotifications" is not mandatory in iOS10. "UIUserNotificationSettings" still works in iOS10.

UserNotifications”在iOS10中不是强制性的。“ UIUserNotificationSettings”在iOS10中仍然有效。

If you have the following code , it should work in iOS10.

如果您有以下代码,它应该可以在 iOS10 中运行。

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

But if you are building with Xcode8and above, makes sure that you have the following entry in your entitlements. This entry will be added automatically once you enabled "Push Notifications" in "Capabilities".

但是,如果您使用Xcode8及更高版本进行构建,确保您的entitlements 中有以下条目。在“功能”中启用“推送通知”后,将自动添加此条目。

<key>aps-environment</key>
<string>development</string>

In release-distribution build this will be automatically changed to the following

在发布分发版本中,这将自动更改为以下内容

<key>aps-environment</key>
<string>production</string>

回答by Keyur Hirani

We need to change some code for iOS 10.

我们需要为 iOS 10 更改一些代码。

Appdelegate.h

Appdelegate.h

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 
@end

Check os version

检查操作系统版本

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Register notification

注册通知

- (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 delegate method

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();
}

回答by raul

On iOS 10 is necessary add the Push Notifications entitlement, so if you "Fix Issue" in Capabilities the problem will be resolved automatically.

在 iOS 10 上需要添加推送通知权利,因此如果您在功能中“修复问题”,问题将自动解决。