OGeek|极客世界-中国程序员成长平台

标题: iOS 10 在 iPhone 中获得两次推送通知警报 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:52
标题: iOS 10 在 iPhone 中获得两次推送通知警报

enter image description here

我在 iOS 10 和 iPhone 中遇到问题。两次获得推送通知警报。请查看以下视频。

我在 willPresentNotification 和 didReceiveNotificationResponse 通知委托(delegate)方法中编写了类似的代码。我在这两种方法中进行代码更改是否有任何更改,因为我在 iPad 中没有遇到问题。

您的帮助将不胜感激。



Best Answer-推荐答案


iOS 10中,我们需要在appDelegate didFinishLaunchingWithOptions方法中调用UNUserNotificationCenter

您必须导入UserNotifications.framework并在Appdelegate中添加UNUserNotificationCenterDelegate

AppDelegate.h

#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
   if([[[UIDevice currentDevice]systemVersion]floatValue]<10.0)
   {
      [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypesUIUserNotificationTypeSound |    UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
      [[UIApplication sharedApplication] registerForRemoteNotifications];
   }
   else
   {
      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
      center.delegate = self;
      [center requestAuthorizationWithOptionsUNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
      {
         if( !error )
         {
             [[UIApplication sharedApplication] registerForRemoteNotifications];
             NSLog( @"ush registration success." );
         }
         else
         {
             NSLog( @"ush registration FAILED" );
             NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
             NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );  
         }  
     }];  
   }
  return YES;
}

在 Swift 中获取信息 See this

关于iOS 10 在 iPhone 中获得两次推送通知警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40101154/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4