var data = {
alert: "Your driver is here!",
sound: "ding.caf"
session_id: session.sessionId
}
parse.Push.send({
channels: ['user_id-2'],
data: data
},{
我发送带警报的推送通知。当应用程序处于后台时,它可以正常工作 - 我收到警报。
然而,当我的应用程序位于前台时,UIAlertView
仍然会弹出,当用户正在使用它时会突然发出警报。< / p>
当应用处于前台状态时,如何禁用此功能?这是我在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)
}
}
答案 0 :(得分:4)
如果
,则调用didReceiveRemoteNotification
委托方法
您可以使用application.applicationState
选择如何处理通知。来自How to respond to push notification view if app is already running in the background:
您可以判断您的应用是否刚刚被带到前台或 不在
application:didReceiveRemoteNotification:
使用这个位 代码:- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { if ( application.applicationState == UIApplicationStateActive ) // app was already in the foreground else // app was just brought from background to foreground ... }
“无效”不会打印,因为UIApplicationStateInactive
(UIApplicationState.Inactive
)是以下情况时的情况:
应用程序在前台运行但未接收事件。这可能是由于中断或应用程序正在转换到后台或从后台转换而发生的。 <子> Source 子>
所以,你实际需要的是UIApplicationState.Background
:
if application.applicationState == UIApplicationState.Background {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayloadInBackground(data, block:nil)
}
因此,这解决了后台开放问题,但是当用户在应用中收到通知时呢?你不想要震撼的弹出窗口,所以禁用它的方法就是摆脱它的来源,PFPush.handlePush()
。
handlePush:
所做的就是创建该警报视图并将其呈现给用户,因此删除它不会影响其他任何内容:
应用处于活动状态时推送通知的默认处理程序,可用于在应用程序后台运行或未运行时模拟iOS推送通知的行为。
<子> Source 子>
就是这样 - 只需删除它,就不会有警报视图。
如果您想增加徽章数量,您仍然可以:
if userInfo.objectForKey("badge") {
let badgeNumber: Int = userInfo.objectForKey("badge").integerValue
application.applicationIconBadgeNumber = badgeNumber
}