Ios 如何重定向到推送通知的特定页面?
我正在处理ios推送通知,需要在点击时将其重定向到特定页面。Ios 如何重定向到推送通知的特定页面?,ios,apple-push-notifications,swift4,Ios,Apple Push Notifications,Swift4,我正在处理ios推送通知,需要在点击时将其重定向到特定页面。 如何实现这一功能 若要在收到通知时重定向到特定页面,必须首先处理收到的推送通知 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchRe
如何实现这一功能 若要在收到通知时重定向到特定页面,必须首先处理收到的推送通知
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("userInfo:-> \(userInfo)")
let redirect_flag = userInfo["redirect_flag"]as! String
if application.applicationState == .inactive {
//MARK: - code here to redirect when app is not in background or inactive
if (UserDefaults.standard.value(forKey: KEY_IS_LOGIN) != nil)
{
switch redirect_flag {
case "1":
print("redirect controller")
case "2":
print("redirect controller")
case "3":
print("redirect controller")
default:
break
}
}
}else {
//MARK: - code here to redirect when app is in background or active mode
switch redirect_flag {
case "1":
print("redirect controller")
case "2":
print("redirect controller")
case "3":
print("redirect controller")
default:
break
}
}
}
要处理收到的推送通知,请执行以下步骤:
didfishlaunchwithoptions中
将消息传递委托设置为selfUnuseNotificationCenterDelegate
,并实现didReceive
方法。就是这样 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
处理收到的消息:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
处理收到的iOS 10及更高版本的通知:
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert, .badge, .sound])
}
//When clicked
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
//You can here parse it, and redirect.
completionHandler()
}
}
您可以遵循UnuseNotificationCenterDelegate协议并实现didReceive方法,该方法将在单击通知时调用。请给出一个实现示例。