我想在我的应用上实施推送通知。我可以向设备发送通知。我想要做的是检查用户是否通过单击警报打开应用程序。如果是,我将弹出一些对话框或根据警报内容显示不同的内容。我不知道如何在我的应用程序中这样做。我知道有一个方法如下所示,我可以覆盖AppDelegate类。当用户收到通知时,将调用此方法。但我不知道用户是否通过单击警报打开应用程序。我怎样才能实现它?
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
答案 0 :(得分:1)
如果您按下Alert on APNS
确认提醒,则会触发以下代表
如果用户按下“允许”按钮
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let trimmedDeviceToken = deviceToken.description .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
.stringByReplacingOccurrencesOfString(" ", withString: "")
print("Device Token \(trimmedDeviceToken)")
}
如果用户按下“不允许”按钮
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to get token, error: \(error)")
}
在您收到通知后,以下代表被称为
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
print(userInfo) // you can get the details in here
if ( application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background ){
print("opened from a push notification when the app was on background")
}else{
print("opened from a push notification when the app was on foreground")
}
}
示例教程请参阅this
答案 1 :(得分:0)
使用以下条件检查应用程序状态,在 didReceiveRemoteNotification 方法中,
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
if ( application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background ){
//Tapped from a notification and the app is in background.
}else{
//App is in Foreground...
}
}
希望这有帮助。
答案 2 :(得分:-1)
通过将标记设置为警报视图来使用UIAlertViewDelegate方法