【发布时间】:2015-04-12 02:57:25
【问题描述】:
var data = {
alert: "Your driver is here!",
sound: "ding.caf"
session_id: session.sessionId
}
parse.Push.send({
channels: ['user_id-2'],
data: data
},{
我正在发送带有警报的推送通知。当应用程序在后台运行时,它可以正常工作——我会收到警报。
但是,当我的应用程序在前台时,UIAlertView 仍然会弹出,并且在用户使用它时会非常刺耳,并且会突然弹出警报。
当应用程序处于前台状态时如何禁用此功能?这是我在 Swift 中的全部代码。不过,我仍然想访问 JSON。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let center = NSNotificationCenter.defaultCenter()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIApplication.sharedApplication().idleTimerDisabled = true
Parse.setApplicationId("SOMEID", clientKey: "SOMEKEY")
// Register for Push Notitications
if application.applicationState != UIApplicationState.Background {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced in iOS 7).
// In that case, we skip tracking here to avoid double counting the app-open.
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var noPushPayload = false;
if let options = launchOptions {
noPushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil;
}
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
}
}
if application.respondsToSelector("registerUserNotificationSettings:") {
let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
application.registerForRemoteNotificationTypes(types)
}
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
println("Push notifications are not supported in the iOS Simulator.")
} else {
println("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification data: [NSObject : AnyObject]) {
PFPush.handlePush(data)
var dat = JSON(data)
println("dat") //yes, we got a notification. this alerts even in foreground, which it shouldn't.
if application.applicationState == UIApplicationState.Inactive {
println("Inactive - this never prints")
PFAnalytics.trackAppOpenedWithRemoteNotificationPayloadInBackground(data, block:nil)
}
}
【问题讨论】:
标签: ios swift parse-platform push-notification