我不明白如何通过两种方式向我的 iOS 应用程序发送推送通知:
- 振动 + 声音
- 没有振动和声音
现在我无法实现第二个。
一些信使使用第一种方式发送 DM 的推送通知,使用第二种方式进行群聊。 Instagram 也会使用第二种方式通知您新点赞。
请帮忙!
假设您已在 AppDelegate 中正确注册推送通知 (
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
) 并将设备 token 存储在 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
这一切都与您通过推送通知发送的有效负载有关。您会在远程通知负载上找到 Apple 文档 đây .
Kiểm tra this SO thread一个很好的解释。
但为了快速解释:
一个 super 基本的 PN 有效载荷看起来像这样:
{
"aps": {
"alert": "Slide this alert to open my awesome app"
}
}
1) 如果你想要作品(声音和振动),你会添加声音键它看起来像这样:
{
"aps" : {
"alert" : "Slide this alert to open my awesome app",
"sound" : "default"
}
}
2) 根据我提供的第一个链接,如果您想让警报静音,您可以添加 content-available
键,这实际上是在后台启动您的应用程序,但本质上是一个沉默的 PN。它看起来像这样:
{
"aps" : {
"alert" : "Slide this alert to open my awesome app",
"content-available": 1
}
}
希望这对您有所帮助。
Tôi là một lập trình viên xuất sắc, rất giỏi!