我正在尝试为我的应用程序设置推送通知系统。我有服务器和开发人员许可证,可以设置推送通知服务。
我当前正在Swift4 Xcode 9中运行我的应用
这是我的问题:
1_是否可以设置通知消息的标题和正文?
2_接受按摩的功能是什么?我正在使用didReceiveRemoteNotification
,但是当我触摸通知时会调用该函数,在显示可以对其进行按摩的通知之前,我需要调用func
3_我正在appDelegate和服务器的登录页面中生成彼此不同的设备令牌。这是不正确的吧?
这是我的应用程序代表:
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()
}
}
答案 0 :(得分:0)
{ "aps" : { "alert" : { "title" : "Game Request", "body" : "Bob wants to play poker", }, "badge" : 5 } }
didReceiveRemoteNotification
中获得呼叫,否则,当用户点击通知时,系统将处理显示部分并获得应用程序中的控制权。 您不能从应用程序一侧编辑通知的内容。
APN可以出于多种原因发行新的设备令牌:
用户将您的应用安装在新设备上
用户从备份中还原设备
用户重新安装操作系统
其他系统定义的事件
因此建议在启动时请求设备令牌。
您可以在登录页面中发送令牌,而无需在登录名中请求新令牌。