应用程序打开时的iOS应用程序通知(如whatsapp)
我实际上接收到带有JSON和消息的通知,所以我想要的是处理该通知并将其显示给用户 当应用程序未运行或处于后台时,会显示消息,但当应用程序打开时,不会显示任何通知。事实上,我在应用程序打开时的iOS应用程序通知(如whatsapp),ios,swift,apple-push-notifications,swift2,Ios,Swift,Apple Push Notifications,Swift2,我实际上接收到带有JSON和消息的通知,所以我想要的是处理该通知并将其显示给用户 当应用程序未运行或处于后台时,会显示消息,但当应用程序打开时,不会显示任何通知。事实上,我在didReceiveMemotentification中收到json,但我想要的是一个类似whatsapp的通知框 像这样: 我有这个: func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject
didReceiveMemotentification
中收到json,但我想要的是一个类似whatsapp的通知框
像这样:
我有这个:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Notification received: \(userInfo)")
let notification = userInfo["aps"] as? NSDictionary
let message = notification?.valueForKey("alert")
}
这是“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)
let readAction=UIMutableUserNotificationAction()
readAction.identifier=“读取标识符”
readAction.title=“读取”
readAction.activationMode=UIUserNotificationActivationMode.前台
readAction.com=false
readAction.authenticationRequired=true
让deleteAction=UIMutableUserNotificationAction()
deleteAction.identifier=“删除\u标识符”
deleteAction.title=“删除”
deleteAction.activationMode=UIUserNotificationActivationMode.Foreground
deleteAction.deleteAction=true
deleteAction.authenticationRequired=true
让ignoreAction=UIMutableUserNotificationAction()
ignoreAction.identifier=“忽略\u标识符”
ignoreAction.title=“忽略”
ignoreAction.activationMode=UIUserNotificationActivationMode.前台
ignoreAction.destromical=false
ignoreAction.authenticationRequired=false
让messageCategory=UIMutableUserNotificationCategory()
messageCategory.identifier=“消息\类别”
messageCategory.setActions([readAction,deleteAction],forContext:UIUserNotificationActionContext.Minimal)
messageCategory.setActions([readAction,deleteAction,ignoreAction],forContext:UIUserNotificationActionContext.Default)
let类型:UIUserNotificationType=[UIUserNotificationType.Badge,UIUserNotificationType.Sound,UIUserNotificationType.Alert]
application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes:类型,
类别:(NSSet(数组:[messageCategory])作为?集合)
UIApplication.sharedApplication().registerForRemoteNotifications()
let notTypes:UIUserNotificationType=[UIUserNotificationType.Alert,UIUserNotificationType.Badge,UIUserNotificationType.Sound]
let-noteSettings:UIUserNotificationSettings=UIUserNotificationSettings(forTypes:notTypes,categories:nil)
UIApplication.sharedApplication().registerUserNotificationSettings(noteSettings)
希望有人能帮忙。感谢您所做的一切您只需创建一个视图,使用您的内容对其进行自定义,并在应用程序窗口中显示即可
还有一些框架可以为您做到这一点,比如当您的应用程序位于前台时,iOS将不会显示通知横幅。你必须展示你自己。您可以使用其中一些第三代码来显示横幅,并处理横幅上的触摸以处理相应的代码: 在回拨DidReceiveMotonification中,检查应用程序状态:
if ( application.applicationState == UIApplicationStateActive ) {
// show your banner
}
您不需要使用其他框架或库。在iOS 10中,您可以使用方法
userNotificationCenter(ux0:willPresent:withCompletionHandler:)
只有将属性content available:1
添加到有效负载时,才会调用此方法。有效载荷应类似于:
{
"aps":{
"alert":"Testing.. (7)",
"badge":1,"sound":"default"
},
"content-available":1
}
试着把你的问题改写成“我有一个字符串,我想把它放在标签上”。