应用程序第一次启动时调用 didReceiveRemoteNotification [英] Calling didReceiveRemoteNotification when app is launching for the first time

查看:23
本文介绍了应用程序第一次启动时调用 didReceiveRemoteNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了我的 didReceiveRemoteNotification 方法.它工作并显示一个带有通过的通知数据的视图控制器.这只适用于应用程序已经在前台或在后台运行的情况.但是,当应用程序未运行且用户单击通知时,应用程序会启动,但看起来好像没有收到通知.通知没有写入文本文件,视图控制器没有被推送.

I have implemented my didReceiveRemoteNotification method. It works and displays a view controller with the notification data which is passed through. This only works when the app was already in the foreground or if it was running in the background. However, when the app is not running and the user clicks a notification, the app launches, but it appears as if no notification has been received. The notification is not written into the text file and the viewcontroller is not being pushed.

- (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 完成,布尔值 done 就会设置为 true.如果在应用程序根本没有运行时按下通知,我只希望通知视图控制器打开并显示通知.

Could someone please help me solve this problem? The boolean done is set to true once the didFinishLaunchingWithOptions is completed. I just want the notificationviewcontroller to open and display the notification if a notification is pressed while the app is not running at all.

推荐答案

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

You should add something like this to your code :

    - (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 移动到某个通用函数并从两个地方调用该函数.这仅在用户通过点击通知打开应用程序时才有效.如果用户通过点击应用图标打开应用,通知数据将不会到达应用.

You can move the logic from didReceiveRemoteNotification to some common function and call that function from both places. This will only work if the user open the app by tapping the notification. If the user opens the app by tapping the app icon, the notification data won't reach the app.

这篇关于应用程序第一次启动时调用 didReceiveRemoteNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆