[英]how to setting up AppDelegate for push notification in swift
I am trying to setup a push notification system for my application. 我正在尝试为我的应用程序设置推送通知系统。 I have a server and a developer license to setup the push notification service.
我具有服务器和开发人员许可证,可以设置推送通知服务。
I am currently running my app in Swift4 Xcode 9 我目前在Swift4 Xcode 9中运行我的应用
here are my questions : 这是我的问题:
1_ is that possible that I set the title and body of notification massage ?? 1_是否可以设置通知消息的标题和正文?
2_ what is the func of receiving massage ? 2_接受按摩的功能是什么? I'm using
didReceiveRemoteNotification
but this is called when I touch the notification I need a func which is called before showing notification that I can set my massage on it 我正在使用
didReceiveRemoteNotification
但是当我触摸通知时会调用该函数,我需要一个函数,该函数在显示可以设置按摩功能的通知之前被调用
3_ I'm generating device token in appDelegate and also in my login page for my server which are different from each other. 3_我正在appDelegate和服务器的登录页面中生成彼此不同的设备令牌。 this is not correct right ?
这是不正确的吧?
this is my app 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
, otherwise, the system handles the displaying part and get control in the app when the user taps on the notification. didReceiveRemoteNotification
获得呼叫,否则,当用户点击通知时,系统将处理显示部分并获得应用程序中的控制权。 You cannot edit the content of notification from the app side. 您无法从应用程序一侧编辑通知的内容。
APNs can issue a new device token for a variety of reasons:
APN可以出于多种原因发行新的设备令牌:
User installs your app on a new device
用户在新设备上安装您的应用
User restores device from a backup
用户从备份中还原设备
User reinstalls the operating system
用户重新安装操作系统
Other system-defined events
其他系统定义的事件
So its recommended requesting device token at launch time. 因此,建议在启动时请求设备令牌。
You can send the token in login page rather than requesting a new token in the login. 您可以在登录页面中发送令牌,而无需在登录名中请求新令牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.