我正在尝试为我的应用程序设置推送通知系统。我有一个服务器和一个开发人员许可证来设置推送通知服务。
我目前正在 Swift4 Xcode 9 中运行我的应用程序
这是我的问题:
1_是否有可能我设置了通知消息的标题和正文??
2_接收按摩的功能是什么?我正在使用 didReceiveRemoteNotification
但当我触摸通知时会调用此函数,我需要一个函数,在显示我可以在其上设置按摩的通知之前调用该函数
3_ 我在 appDelegate 和我的服务器的登录页面中生成设备 token ,它们彼此不同。这不正确吧?
这是我的应用程序委托(delegate):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print("lunch",launchOptions?.description,launchOptions?.first)
application.registerForRemoteNotifications()
FirebaseApp.configure()
GMSPlacesClient.provideAPIKey("AIzaSyAXGsvzqyN3ArpWuycvQ5GS5weLtptWt14")
UserDefaults.standard.set(["fa_IR"], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
registerForPushNotifications()
return true
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("test : ",messaging.apnsToken)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("Recived: \(userInfo)")
print()
// completionHandler(.newData)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("userInfo : ",userInfo)
if application.applicationState == .active {
print("active")
//write your code here when app is in foreground
} else {
print("inactive")
//write your code here for other state
}
}
func getNotificationSettings() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
}
}
func registerForPushNotifications() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
self.getNotificationSettings()
}
} else {
let settings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
// self.getNotificationSettings()
}
}
最佳答案
- 是的,您可以通过在通知中发送适当的负载来管理通知的内容。按以下模式发送有效负载将在通知中显示标题和正文
{ "aps" : { "alert" : { "title" : "Game Request", "body" : "Bob wants to play poker", }, "badge" : 5 } }
- 显示系统根据应用状态处理通知。如果应用程序处于前台状态,您将在
didReceiveRemoteNotification
中收到调用,否则,系统将处理显示部分,并在用户点击通知时在应用程序中获得控制权。
您无法从应用程序端编辑通知内容。
- 根据文档
APNs can issue a new device token for a variety of reasons:
User installs your app on a new device
User restores device from a backup
User reinstalls the operating system
Other system-defined events
因此建议在启动时请求设备 token 。
您可以在登录页面中发送 token ,而不是在登录中请求新 token 。
关于ios - 如何在 swift 中设置 AppDelegate 进行推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52128667/