【问题标题】:How to handle push notifications in iOS app when the app is closed or inactive?当应用程序关闭或不活动时,如何处理 iOS 应用程序中的推送通知?
【发布时间】:2015-02-20 13:36:24
【问题描述】:

我想在应用未打开或处于非活动状态时将从推送通知接收到的数据插入数据库。 当应用程序未打开或处于非活动状态时,有什么方法可以将所有收到的推送通知保存到我的数据库中?

【问题讨论】:

标签: ios objective-c iphone apple-push-notifications


【解决方案1】:

我建议您在此 AppDelegate 方法中添加方法(在此处保存数据库中的信息)以及可能的方法。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

当您收到通知时,您可以在此处处理您想要发生的事情。传入的 NSDictionary 值存储了所有推送信息,您可以通过执行类似操作来获取它们的值

//Your dictionary may be set up a bit differently but that's okay
[userInfo objectForKey:@"alert"];

这包含您的推送通知中的消息或“警报”消息。您可以使用该密钥并将其存储在 NSString 实例中并将其保存到数据库中(但是这对您有用)。我在后端使用 Parse.com,所以我会做类似的事情来在后台保存一个对象。

PFObject *someObject = [PFObject objectWithClassName:<Class Name>];
messageObject[@"message"] = [userInfo objectForKey:@"alert"];
[someObject saveInBackground];

我还没有真正尝试过,但我认为它会起作用

【讨论】:

    【解决方案2】:

    收到通知时需要检查application对象中的applicationState属性。

    UIApplicationState applicationState = [application applicationState];
    

    它可以是 UIApplicationStateActive、UIApplicationStateInactive 或 UIApplicationStateBackground。

    希望这会有所帮助;-)

    【讨论】:

      【解决方案3】:

      你总是会收到一个通知,它直接进入回调方法

      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
      
          // Get info to save from userInfo dictionary
      
          // Save it with Core Data, Archiving, NSUserDefaults, SQLite...
      
      }
      

      它独立地来自BackgroundForeground。您只需从userInfo Dictionary 获取您需要的通知信息。

      您也可以在此处使用UIApplicationState 检查应用的状态。如果您想在您的应用程序位于Foreground 上时显示例如UIAlertView,这使您有机会拆分背景/前景状态的逻辑。或者不是,如果你在Background

      【讨论】:

        【解决方案4】:

        使用Notification service extension,我们可以将数据存储到数据库中。

        a) 使用底部的+ 图标添加Notification service extension

        b) 为此创建配置文件

        c) 打开新创建的目标并打开NotificationService.swift文件

        d) 使用以下方法处理通知

        override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
            self.contentHandler = contentHandler
            bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
           // print("Notification service extension user info :", bestAttemptContent?.userInfo)
            
            if let bestAttemptContent = bestAttemptContent {
                // Modify the notification content here...
               // bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
                if let aps = bestAttemptContent.userInfo["aps"] as? NSDictionary,
                   let data = aps["data"] as? NSDictionary {
                    saveNotificationIntoDB(data: data)
                }
                contentHandler(bestAttemptContent)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-26
          • 2020-08-30
          • 2021-06-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多