【问题标题】:Handling notification like whatsapp when application is forcefully close in iOS?在iOS中强制关闭应用程序时处理whatsapp之类的通知?
【发布时间】:2016-10-10 12:01:22
【问题描述】:

我正在创建一个类似于 whatsapp 的聊天信使应用程序,并尝试实现类似于 whatsapp 的通知功能。在 whatsapp 中,当您收到通知时,它会将数据存储在某处,当您关闭 wifi 并进入应用程序时,消息会被注入应用程序中。这意味着即使应用程序关闭,whatsapp 也会以某种方式访问​​通知。

我的方法:我在后台模式下收到通知并将其保存到文件中。因此,如果用户断开互联网连接并进入应用程序,消息仍会注入到 applicationWillAppear 上。这很完美,但是当您强行关闭您的应用程序(双击主页并向上滑动应用程序)时,它不起作用。我几乎搜索了所有内容,它说如果您强行关闭应用程序,后台提取将不起作用。

那么whatsapp是怎么做的呢?还有什么其他解决方案?

我的解决方案:

为后台获取和来自 XCode 的远程通知打开后台模式。接下来在 application:willFinishLaunchingWithOptions:

中添加以下代码
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

在 AppDelegate.m 文件中添加了这个

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{

  NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

  if (0 < [paths count]) {
    NSString *documentsDirPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirPath stringByAppendingPathComponent:@"notification.txt"];

    NSString *content = [NSString stringWithFormat:@"%@", userInfo];
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
      // Append text to the end of file
      NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:filePath];
      [fileHandler seekToEndOfFile];
      [fileHandler writeData:data];
      [fileHandler closeFile];
    } else {
      // Create the file and write text to it.
      [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
  }

  handler(UIBackgroundFetchResultNewData);

}

更新:我的通知中有以下标志

content-available: 1

【问题讨论】:

  • 如果您已经将数据保存到文件中,为什么还需要后台提取?
  • 研究abt静默推送通知。
  • @TejaNandamuri 数据仅在调用该函数时保存。但是当你强行关闭你的应用程序时它不会被调用。
  • 这可以帮助quora.com/…。看看讨论。
  • @PrajeetShrestha 我在做什么。它确实在后台工作,但在强制关闭时不能。

标签: ios objective-c swift push-notification whatsapp


【解决方案1】:

后台推送不会传递到用户终止的应用程序。

除非是 voip 推送,在这种情况下,如果需要,应用程序将由操作系统启动(但是是的,如果您的应用程序向用户提供 voip 功能,您只能使用 voip 推送。)

【讨论】:

  • 当您说“由操作系统启动”时,这是否需要从操作系统获取我们的应用程序的额外权限? Facebook、whatsaapp 是怎么做的?
  • @the-pumping-lama whatsapp 无需启动应用程序即可完成。
  • 您必须向应用程序添加 voip 的后台模式功能,它使用与常规 ssl 推送证书分开的 voip 服务证书。 VoIP 推送通知在应用程序中的实现方式与常规推送不同,它们是使用推送工具包完成的。
  • 我会看看推送工具包和 voip 推送通知
  • @Ravi H Malviya,是的,如果您的应用向用户提供 voip 功能,您只能使用 voip 推送。
猜你喜欢
  • 1970-01-01
  • 2019-02-19
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
相关资源
最近更新 更多