【问题标题】:Add images via Push Notification Service?通过推送通知服务添加图像?
【发布时间】:2009-07-23 04:27:06
【问题描述】:

我有一个内容应用程序,并希望使用 Apple 推送通知服务通知用户新内容,绕过现在很长的 App Store 提交。用户收到通知后,将启用下载更新按钮。用户将从我的网站下载一个 sql 文件和图像。 sql 文件将执行插入语句,图像将下载到磁盘。

我目前将内容(字符串和捆绑图像引用)加载到 UIWebView。图像显示为内容的一部分。我将如何执行 sql 文件来插入新内容?那么,我是否还需要开始引用磁盘上的图像,而不是从捆绑包中引用图像,这是我在使用 App Store 更新提交时放置图像的位置?

对于 sql 文件,我可能会在下载完成后运行一些基于事件的代码。但是随后需要重新加载只读数据库才能看到新内容。用户是否必须重新启动应用程序?

【问题讨论】:

    标签: cocoa-touch sqlite iphone-sdk-3.0 push-notification


    【解决方案1】:

    正如您所说,数据库对您的应用程序是只读的,我只需让 APN 触发下载新数据库文件并将其写入磁盘,而不必执行 SQL 语句来修改现有数据库。 (除非您的数据库非常大并且您只进行了很小的更改,否则下载大小是一个考虑因素)。

    无论如何,如果您从包中加载数据库,它将是只读的,因此您无法对其执行插入语句 - 您需要制作一个可写副本。让更新过程完全替换此副本意味着您不必担心 SQL 在每个设备上正确执行。例如如果应用在插入过程中终止会发生什么?

    与图像类似,如果在第一次启动时将它们复制到可写的图像目录而不是从捆绑包中加载,那么您的生活会更轻松,因此您总是在同一个地方寻找它们,而不在乎是否存在更新。只需确保在更新后清理不再需要的任何内容,这样您就不会占用过多的用户存储空间。

    【讨论】:

      【解决方案2】:

      当然,你可以这样做。

      • 在我的情况下,您在推送通知有效负载中添加了一些自定义字段, 'msgId' 可以是你需要的字段。

        $body = array();
        $body['device_tokens'] = str_replace(' ', '', $_REQUEST["Token"]);
        $body['msgId'] = $MP_ID;
        $body['aps'] = array('alert' => $alertShort);
        $payload = json_encode($body);
        
      • 将payload发送到APN服务器(沙箱服务器/官方服务器,看你的情况)

      • 添加接收APN的代码

            -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
              NSLog(@"I got notification");
        
              [NSThread detachNewThreadSelector:@selector(handleRemoteNotification:) toTarget:self withObject:userInfo];
             }
        
        
        - (void)handleRemoteNotification:(NSDictionary *)userInfo {
          NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        
          [UIApplication sharedApplication].applicationIconBadgeNumber++;// = [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
        
              if ([userInfo objectForKey:@"msgId"]) {
                  iRetrievingAPNMessage++;
                  NSString *msgId = [[NSString alloc] initWithString:[userInfo objectForKey:@"msgId"]];
                  NSMutableString *theURL = [[NSMutableString alloc] init];
                  [theURL appendFormat:@"http://yourDATAFeedPROGRAM.php?msgID=%@",msgId];
        
                  MIPFileConnection *APNMsgConnection = [[MIPFileConnection alloc] initWithTargetURL:theURL useCache:NO];
                  APNMsgConnection.delegate = self;
                  [APNMsgConnection start];
        
                  NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
                  do {
                  } while ((iRetrievingAPNMessage > 0) &&
                           [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
                  NSLog(@"done");
              }
              [pool release];
          }
        
          - (void)updateDB:(NSDictionary *)msgDic {
              // do anything to update your sql db.
              // ATTENTION, the files located in bundle cannot be replaced.
              // so you need to duplicate your sql into cache/document path.
              // then you can update it successfully.
          }
        
        
          -(void)connectionDidFinishLoading:(NSURLFileConnection *)connection {
              CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)connection.receiveData, kCFPropertyListImmutable, NULL);
        
                  if (plist) {
                      NSDictionary *tmpDic = (NSDictionary *)plist;
        
                      [self performSelectorOnMainThread:@selector(updateDB:) withObject:[tmpDic copy] waitUntilDone:NO];
        
                      [tmpDic release];
                  }
                  iRetrievingAPNMessage--;
          }
        

      我在“updateDB”中使用了伪代码和虚假 URL,请将其替换为您的测试网址。上述动作有效,我做到了。但是当我的应用处于后台时,我无法强制通知显示图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-18
        • 2016-11-27
        • 2010-11-24
        • 2011-03-09
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 2016-08-07
        相关资源
        最近更新 更多