【问题标题】:Background handling of Push NotificationPush Notification 的后台处理
【发布时间】:2015-04-27 07:02:28
【问题描述】:

当 iPhone 应用程序完全关闭,即甚至不在后台时,如何进行 APNS 处理。 例如当应用程序完全关闭时,将 APNS 数据存储在 sqlite 中。

【问题讨论】:

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


【解决方案1】:

在 AppDelegate 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if (launchOptions)
    { //launchOptions is not nil

        NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
        userInfoDic = userInfo;

        if (apsInfo)
        { //apsInfo is not nil
            [self performSelector:@selector(postNotificationToPresentPushMessagesVC)
                       withObject:nil
                       afterDelay:1];
        }
    }

    return YES;
}

-(void)postNotificationToPresentPushMessagesVC
{    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"recievePush" object:userInfoDic];
}

在所有 VC 中:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recievePush:) name:@"recievePush"  object:nil];

}

- (void) recievePush : (NSNotification *) notif
{
    NSDictionary *dict = notif.object;

// do something with message data
}

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 2023-03-14
    • 2018-02-20
    • 2015-04-18
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多