IOS 7 通过单击推送通知查看特定视图控制器

Posted

技术标签:

【中文标题】IOS 7 通过单击推送通知查看特定视图控制器【英文标题】:IOS 7 view Specific View Controller by Clicking Push Notification 【发布时间】:2014-03-06 12:21:51 【问题描述】:

我为我的应用设置了推送通知,这样当我点击推送通知时,应用会转到主控件视图。但是,我想根据添加到应用程序中的内容查看特定的视图控制器。我该怎么做?

我的应用委托代码。

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

          [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
          return YES;
      

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
     

        const char* data = [deviceToken bytes];
        NSMutableString * token = [NSMutableString string];

        for (int i = 0; i < [deviceToken length]; i++) 
        [token appendFormat:@"%02.2hhX", data[i]];
      


         NSString *urlString = [NSString stringWithFormat:@"url"?token=%@",token];

         NSURL *url = [[NSURL alloc] initWithString:urlString];
         NSLog(@"token %@",urlString);


         NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
         NSLog(@"request %@ ",urlRequest);
         NSData *urlData;
         NSURLResponse *response;
         urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
         NSLog(@"data %@",urlData);

        //  NSLog(@"token ",sendUserToken);

      

我的 php 推送通知脚本。

 <?php



    token ="my token"

          $payload = '
         "aps" :
     
        "alert" :"'.$message.'",
        "badge" : 1,
        "sound" : "bingbong.aiff"
        
   ';
     $ctx = stream_context_create();
     stream_context_set_option($ctx,'ssl', 'local_cert','ck.pem');
     stream_context_set_option($ctx,'ssl','passphrase', 'balpad');
     $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr,60,STREAM_CLIENT_CONNECT,$ctx);
     if(!fp)
       print "Failed to connect $err $errstrn";
       return;
     else
        print "notifications sent!";
     
       $devArray = array();
       $devArray[] = $deviceToken;

      foreach($deviceToken as $token)
      $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ',' ',$token)).pack("n",strlen($payload)) . $payload;
      print "sending message:" .$payload . "n";
      fwrite($fp,$msg);
     
     fclose($fp);
     

  ?>

这是我第一次使用推送通知,但我还没有找到合适的解决方案。我找到了一些建议(link1link2),但我发现它们有点令人困惑,我没有任何想法。请有人指导我如何制作这个。

【问题讨论】:

【参考方案1】:

我有一个解决方案给你。请参考下面的示例代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
       
   UIApplicationState state = [application applicationState];
   if (state == UIApplicationStateActive) 

       // Below Code Shows Message While Your Application is in Active State

       NSString *cancelTitle = @"Ok";

       NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"App Start"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:cancelTitle
                                              otherButtonTitles: nil];
       [alertView show];
       [alertView release];

     else 

       // Do stuff that you would do if the application was not active
       // Please add your code to go to specific view controller here.
    

【讨论】:

【参考方案2】:

每当我们收到推送通知时,我们都会通过 ios 调用接收通知方法。

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

In this method base on your requirement you can navigate on any view; suppose that you want to navigate on firstviewcontroller, so code is like that:



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

// here you need to check in which view controller is right now on screen;
//1. if it is same in which you are then no problem just referesh controller;
//otherwise push to your view controller like following"
    firstviewcontroller = [[firstviewcontroller alloc] initWithNibName:@"firstviewcontroller" bundle:nil];

    [self.navigationController pushviewcontroller:firstviewcontroller animated:YES];

【讨论】:

【参考方案3】:

好的,这样做:

在您的 .php 中添加另一个键,例如:

...

        "alert" :"'.$message.'",
        "badge" : 1,
        "sound" : "bingbong.aiff",
        "condition" : "viewController1"
      
      ...  

你可以在那里写任何你想要的。这将告诉您收到推送时要显示的屏幕,它不需要是您的真实控制器的名称,只是一些条件,以便您可以不同的通知

然后像这样覆盖 didReceiveRemoteNotification:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
       
   UIApplicationState state = [application applicationState];
   if (state == UIApplicationStateActive) 

       // Below Code Shows Message While Your Application is in Active State

      NSString *strControllerToShow = [[userInfo valueForKey:@"aps"] valueForKey:@"condition"];;
      if(condition != nil)
          if([condition isEqualToString:@"viewController1"])
              // create vc
              // set properties
              // push it on navigation stack
          
          if([condition isEqualToString:@"viewController2"])
              // create vc
              // set properties
              // push it on navigation stack
          
          ...
      
     else 

       // Do stuff that you would do if the application was not active
       // Please add your code to go to specific view controller here.
    

就是这样……

【讨论】:

小心不要将太多信息放入推送中,Apple可以拒绝它:(

以上是关于IOS 7 通过单击推送通知查看特定视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 10 下使用 FCM 在前台单击推送通知后无法导航到特定的 viewController

Phonegap 推送插件。单击通知后打开特定的应用程序视图

当用户使用 iOS 13 Swift 5 点击推送通知时,在特定视图中打开应用程序

重定向以查看 IOS 通知栏中的点击推送消息

关于 iOS 推送通知和登录的几个问题

IOS 10如何处理用户点击推送通知