【问题标题】:Calling didReceiveRemoteNotification when app is launching for the first time首次启动应用时调用 didReceiveRemoteNotification
【发布时间】:2013-02-04 18:55:56
【问题描述】:

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

- (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。如果在应用程序根本没有运行时按下通知,我只希望通知视图控制器打开并显示通知。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!这非常有效。是的,将逻辑从 didReceiveRemoteNotification 移到另一个方法会更好,因为它会使代码更短。
    • @vinbhai4u 不,我不知道 Swift。
    • @Eran 感谢您尝试让我尝试转换为 swift。 :)
    • @Eran 这条线是什么意思? [self handleRemoteNotification:application userInfo:remoteNotif]; handleRemoteNotification 是内置函数吗?
    • 我得到 remoteNotif 字典的空值
    【解决方案2】:

    Swift 代码

    let remoteNotif: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey]
    
                //Accept push notification when app is not open
                if ((remoteNotif) != nil) {
    
                    self.handleRemoteNotification(remoteNotif!)
                }
    
    
    func handleRemoteNotification(remoteNotif: AnyObject?){
    //handle your notification here
    }
    

    @Eran 谢谢 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      相关资源
      最近更新 更多