ios - iOS 中的通知操作后台获取?

标签 ios objective-c apple-push-notifications

我使用这段代码在推送通知中将应用程序从接近后台唤醒

UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];////UIUserNotificationActivationModeBackground
[action2 setTitle:@"ACCEPT"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

现在我通过以下方法访问网络服务 //远程通知

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler 
{
    /Call to web services.
    //show local notification in background

     UILocalNotification *localNotification = [[UILocalNotification alloc] init];
     if (localNotification == nil)
     {
         return;
     }
     else
     {
         localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
         localNotification.alertAction = nil;
         localNotification.soundName = UILocalNotificationDefaultSoundName;
         localNotification.alertBody = [NSString stringWithFormat:@"Worked=> %@",jsonArray];
         localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
         localNotification.applicationIconBadgeNumber=1;
         localNotification.repeatInterval=0;
         [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     }
}

我在应用程序中启用了后台获取。我的问题是

  1. 我正在使用 NSURLConnetion 类进行网络调用。它有时有效,但有时似乎无效。
  2. 通知提醒未在后台显示。
  3. 我需要使用 performFetchWithCompletionHandler completionHandler 委托(delegate)吗? 任何人都可以帮我解决这个问题吗?提前致谢。

最佳答案

我是这样实现的:

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler {
        self.backgroundSessionCompletionHandler = completionHandler;

        //add notification
        [self presentNotification];
}

-(void)presentNotification{
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = @"Upload Complete!";
        localNotification.alertAction = @"Background Transfer Upload!";

        //On sound
        localNotification.soundName = UILocalNotificationDefaultSoundName;

        //increase the badge number of application plus 1
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}

关于ios - iOS 中的通知操作后台获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067864/

相关文章:

iphone - HTML 字符串设置为 UILabel

ios - 自定义 View 中的 UiLabel 文本分配

ios - 隐式转换丢失整数精度 : 'unsigned long' to 'int' - Error

ios - Xcode 6.1 : Unknown class x in Interface Builder file

php - 向多个 iPhone 设备发送推送通知时收到警告消息

ios - 将 block 传递给方法

objective-c - 如何使用NSScrollview?

iphone - respondsToSelector 发送到释放的对象

ios - 即使应用程序未处于事件状态,也可以在没有警报消息的情况下在 iOS 中接收推送通知

ios - 如何在 Apple Local/Push 通知消息中添加图像?