【问题标题】:How to handle on click on push notifications ios如何处理点击推送通知ios
【发布时间】:2014-04-01 08:16:23
【问题描述】:

我设法收到了推送通知,但我不知道如何处理到达应用程序的推送通知的点击,我想告诉应用程序点击时根据通知类型转到特定活动,而不仅仅是打开应用程序(默认行为)

我知道我可以收到

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    NSLog(@"Here I should Recive The notification .... ") ;
    //call the commented functions .....
}

在这个函数中,但是当我实现它时,通知不会出现,只是在这个函数中执行什么操作

【问题讨论】:

    标签: ios push-notification


    【解决方案1】:

    尝试按代码所示处理此点击,

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    
    //Accept push notification when the app is not open.
            if (remoteNotifiInfo) {
                [self application:application didReceiveRemoteNotification: remoteNotifiInfo];
            }
    
    }
    
    
    
       // On click of notification open related screen.
    

    像这样覆盖 didReceiveRemoteNotification

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
            {
                        UIApplicationState state = [[UIApplication sharedApplication] applicationState];
                        if ( state == UIApplicationStateInactive )
                        {
                            //Your actions on notification click
                            double delayInSeconds = 0.3;
                            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                //code to be executed on the main queue after delay
                                [[NSNotificationCenter defaultCenter] postNotificationName:@"pushTOEventDetails" object:eventVO];
                            });
    
                        }
            }
    

    【讨论】:

      【解决方案2】:

      如果你想通过点击 pushnotification 来导航一些特定的 ViewController 只需在 didReceiveRemoteNotification 中添加 ViewController 导航代码

      -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
      
      NSLog(@"Here I should Receive The notification .... ") ;
      //call the commented functions .....
      
      FirstController *firstviewcontroller = [[FirstController alloc] initWithNibName:@"FirstController" bundle:nil];
      
          [self.navigationController pushviewcontroller:firstviewcontroller animated:YES];
      
      }
      

      【讨论】:

      • 但是一旦我收到通知就会这样做,并且通知不会出现在设备中,我想要通知出现在设备中的默认行为,当我点击它时我可以做到东西..
      • 首先请具体说明您想做什么
      • 没有这个功能我可以收到通知,我只想“点击通知做某事”把这段代码放在哪里
      【解决方案3】:

      只有在应用程序处于活动状态时才会触发该委托方法。如果应用程序没有运行,您应该在委托方法application:didFinishLaunchingWithOptions: 中使用launchOptions 获取通知负载,并根据负载进行导航。

      【讨论】:

      • 感谢您的回复 :),我知道这一点,但这一切都会自动发生,我只想处理点击通知以执行特定行为,而不仅仅是打开应用程序
      • 哦,我明白了,您想向通知中心添加通知。当您的应用程序处于活动状态时收到远程通知时,您可以立即添加UILocalNotification
      • 如何实现这个?
      猜你喜欢
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多