从UILocalNotification

时间:2015-08-17 10:55:11

标签: ios objective-c iphone

我知道在Show AlertView dialog from UILocalNotification之前已经问过这个问题,但是由于不满意的答案和不同的情况,我提到我的问题我要做的是检查应用程序的状态是否在应用程序的前景中使用操作显示 alertView alertControl ,并创建具有给定名称,发件人和信息的通知,并将其发布到接收方,这里我们将自己放在两个前面溶液

第一个解决方案:

使用警告视图及其委托方法,例如How to create an alert box in iphone?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this.  This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
//That will display the message.

//Then to check whether they tapped delete or cancel, use this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        //delete it
    }
}

或使用第二个解决方案:

 //    we need to show alertDialogue //
    UIAlertController* alert = [UIAlertController 

alertControllerWithTitle:@"Reminder"
                                                                       message:notification.alertBody
                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {

                                                             }];

        UIAlertAction *viewDetailsAction = [UIAlertAction actionWithTitle:@"View Details" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
                                                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
                                                                  }];
        [alert addAction:viewDetailsAction];
        [alert addAction:cancelAction];

了解我的问题的完整代码:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

     if (application.applicationState == UIApplicationStateActive)
    {








     //    we need to show alertDialogue //
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Reminder"
                                                                       message:notification.alertBody
                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {

                                                             }];

        UIAlertAction *viewDetailsAction = [UIAlertAction actionWithTitle:@"View Details" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
                                                                      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
                                                                  }];
        [alert addAction:viewDetailsAction];
        [alert addAction:cancelAction];
   // [self presentViewController:alert animated:YES completion:nil];
   //this line cause not visible interface inside appdelegate//

    }
    else
    {
        reservationId = (NSNumber *)[notification.userInfo valueForKey:@"reservationId"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:notification.userInfo];
    }


}

正如您所看到的,如果应用程序位于前台或处于任何不同的状态,我正在检查didRecieveLocalNotification方法。如果后台内的应用程序创建一个带有名称和发件人的本地通知,如果是前台创建alertController或带有名称和发件人的本地通知的alertView

我无法使用第一个解决方案,因为它的委托方法会阻止我访问本地通知的userInfo,对于第二个解决方案我无法使用它,因为错误说AppDelegate没有可见的界面声明selector presentViewController:animated:completed

提前感谢和Happy Coding:D

1 个答案:

答案 0 :(得分:0)

当您收到<!-- Pass this to AcquireToken() during authentication. This should be the root of your SharePoint instance. --> <add key="ServiceResourceLoginId" value="https://mycompanylive.sharepoint.com/"/> <!-- The base URI when using HttpClient to call the API. --> <add key="APIBaseURI" value="https://mycompanylive.sharepoint.com/MySiteName/"/> 时,有两种应用状态:

当应用程序当前处于前台时,将触发以下方法:

UILocalNotification

当应用程序处于后台时,如果应用程序是从-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [self showBannerForNotification:notification]; application.applicationIconBadgeNumber = 0; } 打开的(用户在UILocalNotification上向右滑动并且应用程序已打开),则需要在启动时检查:

UILocalNotification