2016-09-16 59 views
27

之前我有几个应用程序是用Swift 2.2编写的,使用Xcode 7.3编译并在App Store上运行。这些应用程序利用推送通知,并在iOS 9.3及更早版本中正常运行。推送通知不在iOS 10上收到,但在iOS 9上和

但是,在已升级到iOS 10的设备上,我的应用程序不会收到任何推送通知。仍在运行iOS 9的设备仍在接收通知。

想这可能是一个证书或资格的问题,我已经试过如下: 升级我的应用程序,以雨燕2.3的一个中加入了APS环境权利,并在Xcode 8编译它,但是这并没有区别。

在我的AppDelegate我仍然有现有的方法用于推送通知,其中包括注册:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil) 
application.registerUserNotificationSettings(notificationSettings) 

这个注册似乎是成功的,甚至在iOS上10日以来应用didRegisterForRemoteNotificationsWithDeviceToken然后调用,所以我从APNS接收令牌。

问题是,当我向该设备发送推送通知时,从不会调用应用程序didReceiveRemoteNotification

现在,here据说在UIApplicationDelegate方法已被弃用iOS上10,我应该实现userNotificationCenter(:didReceive:withCompletionHandler :)和userNotificationCenter(:willPresent:withCompletionHandler :)

的问题是我并不是只针对iOS 10.我仍然需要该应用程序在iOS 8和9上工作,所以我怀疑这是实现这些方法的正确方法。

如何获取现有应用程序的推送通知以继续在已升级到iOS 10的设备上工作?我需要重写代码吗?我是否需要更新一些证书或授权,并在Xcode 8中用一些“新”设置重新编译?

+0

有几个预先存在的答复这个“推送通知不适用于iOS 10”.. – vaibhav

+0

你能请参考你指的是vaibhav的答案吗?我已经搜索了两天,无法找到适用于我描述的场景的任何问题或答案。 – Stanley

+0

肯定[链接1](http:// stackoverflow。com/questions/39382852/didreceiveremotenotification-not-called-ios-10),[link 2](http://stackoverflow.com/questions/39267549/ios-how-to-integrate-push-notification-in-ios- 10).. – vaibhav

回答

47

好的我已经想通了。我现在有我的原始推送通知在iOS 9上工作,使用Xcode 8和Swift 2.3在iOS 10上工作。

我实现了这个通过进行以下修改我的AppDelegate:

1)在我的项目设置,功能选项卡下,我向下滚动到“推送通知”,并把它置于“ON”。这会自动生成包含关键“APS环境”和值“开发”的权利文件。

2)在AppDelegate.swift中,我做了几个代码更改。我开始通过导入UserNotifications框架:

import UserNotifications 

然后我的AppDelegate实现UNUserNotificationCenterDelegate协议:

class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate { 

然后,添加下面的方法:

func registerForPushNotifications(application: UIApplication) { 

    if #available(iOS 10.0, *){ 
     UNUserNotificationCenter.currentNotificationCenter().delegate = self 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in 
      if (granted) 
      { 
       UIApplication.sharedApplication().registerForRemoteNotifications() 
      } 
      else{ 
       //Do stuff if unsuccessful... 
      } 
     }) 
    } 

    else{ //If user is not on iOS 10 use the old methods we've been using 
     let notificationSettings = UIUserNotificationSettings(
      forTypes: [.Badge, .Sound, .Alert], categories: nil) 
     application.registerUserNotificationSettings(notificationSettings) 

    } 

} 

然后我重新把这个在didFinishLaunchingWithOptions中创建函数:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
... 
    registerForPushNotifications(application) 
... 
} 

我离开我的didRegisterForRemoteNotificationsWithDeviceToken方法不变:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    //Implement this. Do something with the token I receive. Send it to my push notification server to register the device or something else I'd like to do with the token. 
} 

现在我实现了iOS版10两种新的方法来处理推送通知的接收:

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
    //Handle the notification 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
    //Handle the notification 
} 

我没有删除任何方法我之前在iOS 9中实现了推送通知。

+2

非常重要:我们正在使用我们自己的推送通知服务器来发送消息。除了我在这里描述的客户端更改之外,我们还必须对我们的某些静默通知进行服务器更改。我们发现了一些默认通知,其结构{“my_field”:“some_value”,“my_field2”:“myvalue2”}在iOS 10之前运行良好,但似乎在iOS 10上必须包含“alert”或“aps”领域也是如此。所以我不得不改变我的消息在服务器上{“my_field”:“some_value”,“my_field2”:“myvalue2”,“aps”:{“content-available”:1}}否则我不会收到任何东西APNS。 – Stanley

+0

你是我的英雄正式 – po5i

+0

Thatssss非常有用的答案。谢谢你Stanley –

8

IOS 10有一个不同的通知套件。

import UserNotifications 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 



    if #available(iOS 10.0, *) { 
     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in 
      if granted { 
       UIApplication.shared.registerForRemoteNotifications() 
      } 
     } 
    } else { 
     let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound] 
     let setting = UIUserNotificationSettings(types: type, categories: nil) 
     UIApplication.shared.registerUserNotificationSettings(setting) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
} 



func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){ 
    let token = String(format: "%@", deviceToken as CVarArg) 
} 
+0

这是swift 3.0语法。 –

2

在针对有关iOS 9和iOS 10推送通知的问题的各种SO响应中,我已经尝试以下测试:

1)更新通知要求专门使用UNUserNotificationCenter为iOS 10设备

2)更新推送能力,从而使权利文件更新

我可以证实,这样做#2,并保持所有现有的iOS 9推同一代码将允许基本的推送通知对iOS的10台设备

+1

可以确认 - 刚开始通过添加权利文件来实现接受的答案,并且它在iOS 10上没有任何其他更改时工作:) – Fengson

7

我有固定的使用下面的步骤这个问题来通过:

第1步:转到项目 - >目标 - >性能 - >推送通知 - >开机

enter image description here 第2步:修复与正确的证书问题,配置轮廓

第3步:退出Xcode中,清理并运行

+0

此答案对我来说很甜。 在我的情况下,iOS9.x确实收到设备令牌,但iOS10没有 – Kernelzero

0

当我的应用程序从IOS 9移动到IOS 10时,由Heroku上托管的我的Parse服务器发送的静默推送通知停止被应用程序接收。

我取得了令通知的工作唯一的变化又是修改的Javascript我的服务器上,这样的参数内容提供为1的整数,而不是一个字符串“1”

  Parse.Push.send({ 
       where: notifyDeletedRequestsQuery, 
       data: { 
        'content-available': 1, 
        refresh: constants.requestsClassName 
        } 
      }, { useMasterKey: true }); 

另请注意,内容可用不能与徽章,声音或警报相同的推送通知中出现。