答案 0 :(得分:7)
如果您的应用程序处于后台/前台模式,则调用此方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
如果您使用上述方法,您将在控制台
中遇到以下错误application:didReceiveRemoteNotification:fetchCompletionHandler:],但您仍需要将“remote-notification”添加到Info.plist中支持的UIBackgroundModes列表中。
解决此问题
按照步骤的图像
如果你的应用程序处于前台模式,则调用此方法
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
选择no-2
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
// user tapped notification while app was in background
if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
// go to screen relevant to Notification content
} else {
// App is in UIApplicationStateActive (running in foreground)
// perhaps show an UIAlertView
}
}
<强>夫特强>
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
var state: UIApplicationState = application.applicationState()
// user tapped notification while app was in background
if state == .Inactive || state == .Background {
// go to screen relevant to Notification content
}
else {
// App is in UIApplicationStateActive (running in foreground)
// perhaps show an UIAlertView
}
}
答案 1 :(得分:1)
如果在后台模式下未调用didReceiveRemoteNotification方法,请按照以下步骤操作
首先打开推送通知并勾选目标功能的背景模式远程通知复选框
然后
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void
(^)(UIBackgroundFetchResult))completionHandler
{
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( @"INACTIVE" );
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( @"BACKGROUND" );
}
else
{
NSLog( @"FOREGROUND" );
}
return YES;
}