【发布时间】:2016-10-24 15:45:49
【问题描述】:
我正在尝试在收到远程 FCM 通知时发送位置通知,因为据我所知 FCM 通知不支持操作按钮。但是本地通知有时只会发送,在我看来它是随机的。我总是收到远程通知。我想在所有三个状态、前台、后台或应用程序被杀死时获得本地通知。
这里是我在 didFinishLoadingWithOptions 中添加按钮的地方:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = "First"
incrementAction.title = "First"
incrementAction.activationMode = UIUserNotificationActivationMode.foreground
incrementAction.isAuthenticationRequired = true
incrementAction.isDestructive = true
let decrementAction = UIMutableUserNotificationAction()
decrementAction.identifier = "second"
decrementAction.title = "second"
decrementAction.activationMode = UIUserNotificationActivationMode.foreground
decrementAction.isAuthenticationRequired = true
decrementAction.isDestructive = false
// Category
let counterCategory = UIMutableUserNotificationCategory()
counterCategory.identifier = "BOOKING_CATEGORY"
// A. Set actions for the default context
counterCategory.setActions([incrementAction, decrementAction],
for: UIUserNotificationActionContext.default)
// B. Set actions for the minimal context //No more than 2
counterCategory.setActions([incrementAction, decrementAction],
for: UIUserNotificationActionContext.minimal)
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>))
UIApplication.shared.registerForRemoteNotifications()
}
}
这里是我创建本地通知的地方:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if UIApplication.shared.applicationState == UIApplicationState.active {
// Do something you want when the app is active
} else {
// Do something else when your app is in the background
}
// fire a local notification upon receiving FCM notification
let localNotification = UILocalNotification()
localNotification.alertTitle = "Notification Title"
localNotification.alertBody = "more details"
localNotification.alertAction = "ShowDetails"
localNotification.fireDate = NSDate().addingTimeInterval(5) as Date
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = 1
localNotification.category = "BOOKING_CATEGORY"
UIApplication.shared.scheduleLocalNotification(localNotification)
print("Push notification received bkg: \(userInfo)")
print("completed" , completionHandler)
}
任何帮助将不胜感激。
【问题讨论】:
-
通知的有效负载是什么样的?您有通知负载、数据负载还是两者都有?
-
感谢您的回复,我都有,我在应用委托类中实现了接收本地通知的功能,似乎每次都会调用它。所以我的问题是如何在应用程序被杀死或在后台并且我收到远程通知时安排本地通知,因为当应用程序被杀死时,应用程序委托函数将不会被调用!
标签: swift push-notification swift3 firebase-cloud-messaging localnotification