UNUserNotificationCenter didReceive未被调用
当我发送推送通知时,应用程序在前台willPresent
被调用。 didReceive
永远不会被调用。当应用程序在后台,并且收到推送通知时,会显示警报,但应用程序不会调用didReceive
或willPresent
。UNUserNotificationCenter didReceive未被调用
在项目>功能中,我检查了背景模式Location updates
和Remote notifications
。位置更新是针对不相关的代码。
我也启用了推送通知。这是工作正常,因为他们正在接受。
我已经实现了UNUserNotificationCenter
通知东西在我的AppDelegate
,见下图:
import UserNotifications
...
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
registerForPushNotifications(application: application)
...
}
// MARK: - Push Notifications
func registerForPushNotifications(application: UIApplication) {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted)
{
UIApplication.shared.registerForRemoteNotifications()
}
else{
//Do stuff if unsuccessful...
print("Unsuccessful in registering for push notifications")
}
})
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//convert the deviceToken to a string
let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
let ud = UserDefaults.standard
ud.set(deviceTokenString, forKey: "deviceToken")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
//handle the notification
print("Push notification: ",response.notification.request.content.userInfo)
... code to import the notification data into Core Data.
completionHandler()
}
...
}
任何想法,为什么didReceive
不会被调用?
这是收到推送通知:
[AnyHashable("aps"): {
alert = "[name] has added you as a friend.";
category = "NEWS_CATEGORY";
"related_user_id" = 55;
sound = default;
type = "friend_request";
}]
iOS的10.1,斯威夫特3
如果您在通知旗帜挖掘的willPresent
方法将被调用。
如果您希望在后台处理推送通知而无需点击横幅,那么您应该在通知负载中附加{“content-available”:1}。这种类型的推送通知被称为无声推送。这将而不是创建一个通常的推送通知横幅/警报,你将不得不自己创建横幅。
当您收到带有“content-available”的推送通知时,将在应用程序位于后台时调用didReceive
委托方法。
N.B.您还必须在plist文件的UIBackgroundModes
密钥中添加“远程通知”。
didReceive当你收到通知,这是当用户执行在https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
打电话让你的应用程序知道收到的通知
从苹果文档的操作执行不执行哪一个动作是由用户选择的一个给定的通知。
含义这种方法,当用户对收到的通知的动作
你在波纹管IOS 10.0的测试被称为(推送通知,可向下滑动,等等)? –
@jigneshVadadoriya不,ios 10.1 – toast