【问题标题】:Push notification issue with iOS 10iOS 10 的推送通知问题
【发布时间】:2017-01-22 06:28:13
【问题描述】:

我开发了一个应用程序,实现了推送通知。目前它在苹果商店上架。直到 iOS 9 推送工作正常,但在 iOS 10 之后它无法正常工作。

代码有什么问题?

【问题讨论】:

  • “代码有什么问题?” - 什么代码?
  • 使推送在 iOS9 上工作的那个显然

标签: ios push-notification ios10


【解决方案1】:

对于使用 xCode 8 GM 的 iOS 10。

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

1) 在目标中,在功能下启用推送通知以添加推送通知权利。

2) 在您的应用中实现 UserNotifications.framework。在您的 AppDelegate 中导入 UserNotifications.framework。

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

@end

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) 现在终于实现了这两个委托方法。

//============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);

}

请保留您在 iOS 9 中使用的代码, 仅使用 UserNotifications.framework 添加代码行以支持 iOS 10 的推送通知。

【讨论】:

  • 嗨,Ashish,谢谢。这是一个很好的教程。但我想知道苹果必须明确提到这一点,对吗?他们有任何官方文件说明这一点吗?
  • 我做了同样的事情,但在应用程序终止时无法收到通知
  • -willPresentNotification == 应用在前台并且推送到来时调用,可用于配置显示或不显示收入推送 -didReceiveNotificationResponse == 在通知中心打开通知时调用
  • 嗨@Ashish,我已经实现了上述框架和代码,但是ios 10没有收到通知为什么我不知道请帮助我。
  • 你为什么不检查批准?如果 (granted == NO) 回调发送 nil 错误。这意味着用户没有注册。
【解决方案2】:

在 iOS 上,应用程序通过调用 UIApplicationregisterUserNotificationSettings: 策略来接近客户端以获得推送警告的授权。

应用程序为UIApplication (iOS) 调用registerForRemoteNotifications: 技术或NSApplication (OS X) 的策略registerForRemoteNotificationTypes:

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

应用程序对UIApplicationDelegate (iOS) 或NSApplicationDelegate (OS X) 执行application:didFailToRegisterForRemoteNotificationsWithError: 技术,以在注册失败时出错。

【讨论】:

  • 欢迎来到 Stack Overflow!请注意overt self-promotion is not allowed。我已经编辑了您网站的链接。 允许在自己的用户页面上链接自己的网站。但是,在您获得最低分数之前,它不会被搜索引擎索引,以防止滥用。
【解决方案3】:

不要忘记,在测试时,您必须使用sandbox 地址才能让您的通知生效。

【讨论】:

  • 你能解释一下吗?
  • 在我的情况下,发生的事情是从 ios 9 更新到 10 时,我的应用程序服务器正在将通知发送到我处于开发阶段的生产服务器,因此将其更改回开发沙箱地址修复了问题
【解决方案4】:

我遇到了 iOS 10 静默推送通知的问题。在 iOS9 和更早的版本中,发送一个包含额外数据字段但数据中有一个空的aps 属性的推送通知可以正常工作。但是在 iOS10 中,带有空 aps 属性的推送通知根本不会命中 didReceiveRemoteNotification 应用程序委托方法,这意味着我所有的静默推送通知(我们只是在内部用于在应用程序打开时触发操作的通知)停止工作iOS10。

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

【讨论】:

  • aps = { 徽章 = 7; “内容可用”= 1;优先级 = 5; };
  • 我遇到了完全相同的问题。它帮助了我@jakedunc,谢谢你分享这个。
  • 同样的问题。似乎 iOS 10 对允许通过的内容更加严格。
  • 请注意,使用priority 5 可能会导致推送根本不显示,如文档中所示:"They are throttled, and in some cases are not delivered."!
  • @jakedunc。 aps 属性来自服务器端,或者我们可以。
【解决方案5】:

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

    }

【讨论】:

    【解决方案6】:

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

    推送通知必须开启。

    【讨论】:

    • 我通过单击推送通知部分中的“修复我”按钮修复了它。该问题缺少推送通知的权利
    • 每个人都喜欢一个答案,你所要做的就是点击一个按钮:D
    • @Honey 你能告诉我当应用程序在后台并接收用户通知时执行哪个方法吗?
    • @ArgaPK 见this answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    相关资源
    最近更新 更多