ios - 首次启动应用程序时调用 didReceiveRemoteNotification

标签 ios objective-c xcode push-notification apple-push-notifications

我已经实现了我的 didReceiveRemoteNotification 方法。它工作并显示一个 View Controller ,其中包含传递的通知数据。这仅在应用程序已经在前台或在后台运行时才有效。但是,当应用程序未运行且用户单击通知时,应用程序启动,但看起来好像没有收到通知。通知没有写入文本文件,也没有推送 View Controller 。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive)
    {
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        NSString *alertMsg = @"";
        NSString *badge = @"";
        NSString *sound = @"";
        NSString *custom = @"";

        if( [apsInfo objectForKey:@"alert"] != NULL)
        {
            alertMsg = [apsInfo objectForKey:@"alert"];
        }


        if( [apsInfo objectForKey:@"badge"] != NULL)
        {
            badge = [apsInfo objectForKey:@"badge"];
        }


        if( [apsInfo objectForKey:@"sound"] != NULL)
        {
            sound = [apsInfo objectForKey:@"sound"];
        }

        if( [userInfo objectForKey:@"Type"] != NULL)
        {
            custom = [userInfo objectForKey:@"Type"];
        }

        // Set your appending text.
        NSString *textToAdd = [NSString stringWithFormat:@":%@", alertMsg];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *fileContents = [[NSString alloc]  initWithContentsOfFile:fileName usedEncoding:nil error:nil];

        NSString *textToFile;

        if (fileContents == NULL)
        {
            textToFile = alertMsg;
        }

        // Here you append new text to the existing one
        if (fileContents != NULL)
        {
            textToFile = [fileContents stringByAppendingString:textToAdd];
        }

        // Here you save the updated text to that file
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *content = textToFile;
        [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

        NSArray *fileData = [textToFile componentsSeparatedByString:@":"];

        NSMutableArray *tableDataFromFile;
        tableDataFromFile = [[NSMutableArray alloc] init];

        int i = 0;

        for (i = 1; i < [fileData count]; i++)
        {
            [tableDataFromFile addObject:fileData[i]];
        }

        NotificationViewController *vc = [[NotificationViewController alloc] initWithNibName:@"NotificationViewController" bundle:nil];
        vc.tableData = tableDataFromFile;

        UIViewController *root = self.mainNavController.topViewController;
        NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
        [self.mainNavController setViewControllers:vcs animated:YES];
    }
        // app was already in the foreground
    else
    {
        while (done == FALSE)
        {

        }

        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        NSString *alertMsg = @"";
        NSString *badge = @"";
        NSString *sound = @"";
        NSString *custom = @"";

        if( [apsInfo objectForKey:@"alert"] != NULL)
        {
            alertMsg = [apsInfo objectForKey:@"alert"];
        }


        if( [apsInfo objectForKey:@"badge"] != NULL)
        {
            badge = [apsInfo objectForKey:@"badge"];
        }


        if( [apsInfo objectForKey:@"sound"] != NULL)
        {
            sound = [apsInfo objectForKey:@"sound"];
        }

        if( [userInfo objectForKey:@"Type"] != NULL)
        {
            custom = [userInfo objectForKey:@"Type"];
        }

        // Set your appending text.
        NSString *textToAdd = [NSString stringWithFormat:@":%@", alertMsg];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *fileContents = [[NSString alloc]  initWithContentsOfFile:fileName usedEncoding:nil error:nil];

        NSString *textToFile;

        if (fileContents == NULL)
        {
            textToFile = alertMsg;
        }

        // Here you append new text to the existing one
        if (fileContents != NULL)
        {
            textToFile = [fileContents stringByAppendingString:textToAdd];
        }

        // Here you save the updated text to that file
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *content = textToFile;
        [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

        NSArray *fileData = [textToFile componentsSeparatedByString:@":"];

        NSMutableArray *tableDataFromFile;
        tableDataFromFile = [[NSMutableArray alloc] init];

        int i = 0;

        for (i = 1; i < [fileData count]; i++)
        {
            [tableDataFromFile addObject:fileData[i]];
        }

        NotificationViewController *vc = [[NotificationViewController alloc] initWithNibName:@"NotificationViewController" bundle:nil];
        vc.tableData = tableDataFromFile;

        UIViewController *root = self.mainNavController.topViewController;
        NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
        [self.mainNavController setViewControllers:vcs animated:YES];

    }
            // app was just brought from background to foreground


}

有人可以帮我解决这个问题吗? didFinishLaunchingWithOptions 完成后, bool 值 done 将设置为 true。如果在应用程序根本没有运行时按下通知,我只希望 notificationviewcontroller 打开并显示通知。

最佳答案

你应该在你的代码中添加这样的东西:

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

        NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

        //Accept push notification when app is not open
        if (remoteNotif) {      
            [self handleRemoteNotification:application userInfo:remoteNotif];
            return YES;
        }

        return YES;
    }

您可以将逻辑从 didReceiveRemoteNotification 移动到一些通用函数并从两个地方调用该函数。这仅在用户通过点击通知打开应用程序时才有效。如果用户通过点击应用图标打开应用,通知数据将不会到达应用。

关于ios - 首次启动应用程序时调用 didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14968578/

相关文章:

ios - 如何使用 NSURLProtocol 模拟 AJAX 调用?

ios - 如何在编辑 uitextfield 时停止在 uitableview 中滚动

objective-c - 将用户默认绑定(bind)到不同的标识符

ios - '-' 的右操作数是垃圾值

ios - 启用 UITextView 滚动 = 不可见文本?

objective-c - 证书、配置文件、公钥/私钥揭秘

ios - 缺少方法声明的上下文 - 应用内收据 verificationController

ios - 哪个是通用 iOS 应用启动画面 iOS 12 的最佳方式?

iOS swift : Load new url to a webview after receiving a push notification

ios - RealmSwift - librealm-ios.a 占用太多空间