Nor*_*lim 4 apple-push-notifications ios swift swift2
我实际上收到了带有JSON和消息的通知,所以我想要的是处理该通知并将其显示给用户.
当应用程序未运行或在后台运行时,会显示该消息,但是当应用程序打开时,则不会显示任何通知.事实上,我在收到时会收到json didReceiveRemoteNotification,但我想要的是像whatsapp那样的通知框.
像这样:
我有这个:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Notification received: \(userInfo)")
let notification = userInfo["aps"] as? NSDictionary
let message = notification?.valueForKey("alert")
}
Run Code Online (Sandbox Code Playgroud)
这在'didfinishlaunchWithOptions'中
let readAction = UIMutableUserNotificationAction()
readAction.identifier = "READ_IDENTIFIER"
readAction.title = "Read"
readAction.activationMode = UIUserNotificationActivationMode.Foreground
readAction.destructive = false
readAction.authenticationRequired = true
let deleteAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_IDENTIFIER"
deleteAction.title = "Delete"
deleteAction.activationMode = UIUserNotificationActivationMode.Foreground
deleteAction.destructive = true
deleteAction.authenticationRequired = true
let ignoreAction = UIMutableUserNotificationAction()
ignoreAction.identifier = "IGNORE_IDENTIFIER"
ignoreAction.title = "Ignore"
ignoreAction.activationMode = UIUserNotificationActivationMode.Foreground
ignoreAction.destructive = false
ignoreAction.authenticationRequired = false
let messageCategory = UIMutableUserNotificationCategory()
messageCategory.identifier = "MESSAGE_CATEGORY"
messageCategory.setActions([readAction, deleteAction], forContext: UIUserNotificationActionContext.Minimal)
messageCategory.setActions([readAction, deleteAction, ignoreAction], forContext: UIUserNotificationActionContext.Default)
let types: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Sound, UIUserNotificationType.Alert]
application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes: types,
categories: (NSSet(array: [messageCategory])) as? Set<UIUserNotificationCategory>))
UIApplication.sharedApplication().registerForRemoteNotifications()
let notTypes:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let noteSettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(noteSettings)
Run Code Online (Sandbox Code Playgroud)
希望有人能提供帮助.谢谢大家
当您的应用程序位于前台时,iOS将不会显示通知横幅.你必须展示自己.您可以使用其中一些第三代码来显示横幅并处理横幅上的触摸以处理适当的代码:
https://github.com/KrauseFx/TSMessages https://github.com/terryworona/TWMessageBarManager
在你的回调didReceiveRemoteNotification中,检查应用程序状态:
if ( application.applicationState == UIApplicationStateActive ) {
// show your banner
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14622 次 |
| 最近记录: |