当前位置: 首页 > 面试题库 >

在iOS 10上未收到推送通知,但在iOS 9及更高版本上可以使用

从阎宝
2023-03-14
问题内容

我有几个用Swift 2.2编写,用Xcode 7.3编译的应用程序,这些应用程序在App Store上实时运行。这些应用程序利用推送通知,并且在iOS
9.3和更早版本中可以正常运行。

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

考虑到可能是证书或权利问题,我尝试了以下操作:将我的一个应用程序升级到Swift 2.3,添加了APS Environment
Entitlement并在Xcode 8中进行了编译,但这没有什么区别。

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

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

即使在iOS 10上,此注册似乎也成功,因为随后调用了 Application
didRegisterForRemoteNotificationsWithDeviceToken
,因此我正在从APNS接收令牌。

问题是,当我向该设备发送推送通知时,永远不会调用 Application didReceiveRemoteNotification

现在,这里说UIApplicationDelegate上的方法在iOS
10上已过时,我应该实现userNotificationCenter( :didReceivewithCompletionHandler
:)和userNotificationCenter(
:willPresent: withCompletionHandler :)

问题是我不仅仅针对iOS10。我仍然需要该应用程序才能在iOS 8和9上运行,因此我怀疑这是实现这些方法的正确方法。

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


问题答案:

好的,我知道了。我现在有原始的推送通知,该通知在iOS 9上运行,并且在Xcode 8和Swift 2.3的iOS 10上运行。

我通过对AppDelegate进行以下更改来实现此目的:

1)在我的项目设置中,在“功能”选项卡下,向下滚动到“推送通知”,然后将其打开为“开”。这将自动生成一个授权文件,该文件包含键“ APS
Environment”和值为“ development”。

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中为推送通知实现的任何方法。



 类似资料:
  • 我没有收到Android推送通知。我能够 < li >注册设备 < li >接收设备标识符 < li >将通知消息推送到GCM 我成功进入通知发送事件,但我的设备上没有收到任何消息。成功的推送代码如下:

  • 我使用FCM(Firebase Cloud Messaging)在iOS中发送推送通知。 我能够在应用程序处于前台状态时收到通知。但当应用程序处于后台状态时,不会收到通知。每当应用程序进入前台状态时,才会收到通知。 我的代码是: 当App处于后台状态时: --根本不叫。 我在应用程序功能中启用了推送通知和后台模式下的远程通知。但应用程序仍然没有收到通知。 我提到了一些问题,但没能解决这个问题。iO

  • 我有一个在eclipse上运行的项目,但当我制作“java-jar”时,它会出现以下错误: 当我制作“java-version”时,输出是: 我做错了什么?

  • 问题内容: 如何检查用户是否在ios 9或ios 10上启用了远程通知? 如果用户不允许或单击“否”,我想切换一条消息,询问他们是否要启用通知。 问题答案: 此答案已过时,并且在iOS 10上不支持,您可以检查此答案。 使用此代码

  • 我是一名php开发人员。我正在为iPhone实现推送通知模块。为此,我使用php进行服务器端实现。虽然我收到“已连接到APNS {”aps“:{”警报“:”嗨推送“,”徽章“:1,”声音“:”默认“}}消息已成功发送”消息,但iPhone没有收到任何通知。我的 php 代码是这样的: ? 目标c代码是这样的:

  • 我正在开发简单的android应用程序,非常新的解析。我遵循解析文档添加后端功能。解析核心是一个简单的部分,但我不能发送推送通知从android设备。但从解析仪表板发送时会收到推送通知。我的清单文件如下: 订阅活动类中的频道: 正在另一个活动类中发送推送通知: 在“installation”类中添加了行,而GCMSenderId为空!!这是问题所在吗?请帮助我,为什么我不能收到通知时,从我的and