【问题标题】:Push notification is not working in iPhone 6 with iOS 8.0推送通知在带有 iOS 8.0 的 iPhone 6 中不起作用
【发布时间】:2014-12-29 11:22:18
【问题描述】:

我正在使用推送通知开发 iPhone 应用程序。我遇到了通知问题,我在 iOS 6、7、8.1、8.1.2 中成功获取了设备令牌 ID,但对于我的 iPhone 6 和 iOS 8.0。

所以在我的推送通知方法“didRegisterForRemoteNotificationsWithDeviceToken”中,令牌 ID 返回 nil。所以,我无法获得推送通知。我不知道是什么问题。

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    else{
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
    .
    .
    .
    .

    [self.window makeKeyAndVisible];

    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *newToken = [deviceToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    [GlobalVariables proTrain].deviceToken = newToken;

    NSLog(@"device token id = %@", newToken);
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

【问题讨论】:

    标签: ios ios8 push-notification apple-push-notifications


    【解决方案1】:
    #define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    
    if (IS_OS_8_OR_LATER)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    

    【讨论】:

      【解决方案2】:

      也许不是这样:

      [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
           (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
      

      试试这个:

      [application registerForRemoteNotificationTypes:
           (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
      

      【讨论】:

      • 感谢您的回复,但是我在iOS8.0中遇到了问题,所以替换上述代码后应该不起作用。
      【解决方案3】:
      /*--- Setup Push Notification ---*/
          //For iOS 8
      if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)] && [UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)])
      {
          UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
          [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
      
      }
      //For iOS 7 & less
      else if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotificationTypes:)])
      {
          UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
          [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
      }
      

      通知方式如下:

      #ifdef __IPHONE_8_0
      - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
      {
          //register to receive notifications
          [application registerForRemoteNotifications];
      }
      
      - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
      {
          //handle the actions
          if ([identifier isEqualToString:@"declineAction"]){
          }
          else if ([identifier isEqualToString:@"answerAction"]){
              NSLog(@"didReceiveRemoteNotification ios 8");// push some specific view when notification received       
          }
      }
      #endif
      
      - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
      {
          self.strDeviceToken = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
          NSLog(@"Device Token ==== %@", self.strDeviceToken);
      }
      
      - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
          NSLog(@"error : %@",error.localizedDescription);
      }
      
      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
      {
          NSLog(@"didReceiveRemoteNotification  ios 7");// push some specific view when notification received
      
      }
      

      【讨论】:

        【解决方案4】:
          if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
            {
                // iOS 8 Notifications
                [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        
                [application registerForRemoteNotifications];
            }
            else
            {
                // iOS < 8 Notifications
                [application registerForRemoteNotificationTypes:
                 (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-28
          • 2019-09-22
          • 2015-12-19
          相关资源
          最近更新 更多