2016-09-27 105 views
1

我正在通過蘋果推送通知在iOS推送通知功能上工作,現在我正在通知應用程序處於後臺或前臺,但我想處理遠程通知當我的應用程序在後臺時,基本上當我的應用程序在後臺時,它只是顯示來自有效內容的警報消息。實際上,我只是想自定義我的遠程通知。如何在應用程序處於後臺時處理iOS遠程通知

代碼:

- (BOOL)application:(UIApplication)application didFinishLaunchingWithOptions:(NSDictionary)launchOptions { 
// Override point for customization after application launch. 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
{ 
//  [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 
} 
else 
{ 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
} 
return YES; 
} 
- (void)application:(UIApplication *)application 
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
{ 
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", error); 
} 
- (void)application:(UIApplication)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData)deviceToken { 
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken); 
} 
-(void)application:(UIApplication)application didReceiveRemoteNotification:(NSDictionary)userInfo fetchCompletionHandler:(void (UIBackgroundFetchResult))completionHandler 
{ 
NSDictionary * aps=[userInfo valueForKey"aps"]; 
NSLog(@"did recevie %@",aps); 
NSLog(@"userinfo details %@",[aps valueForKey"alert"]); 
} 
+0

您正在測試設備中使用哪個版本的iOS? – Himanth

+0

對於iOS 10及以上版本,你必須做一些其他的東西 – Himanth

+0

它是在ios 10.0 –

回答

2

在iOS中10首先你要設置UNUserNotificationCenterDelegateAppDelegate.h文件

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,UNUserNotificationCenterDelegate> 

後,在AppDelegate.m這樣寫代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.1) { 
      // iOS 7.1 or earlier. Disable the deprecation warnings. 
      UIRemoteNotificationType allNotificationTypes = 
      (UIRemoteNotificationTypeSound | 
      UIRemoteNotificationTypeAlert | 
      UIRemoteNotificationTypeBadge); 
      [application registerForRemoteNotificationTypes:allNotificationTypes]; 
      [[UIApplication sharedApplication] registerForRemoteNotifications]; 
     } else { 
      // iOS 8 or later 
      // [START register_for_notifications] 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) 
    { 
       UIUserNotificationType allNotificationTypes = 
       (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); 
       UIUserNotificationSettings *settings = 
       [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; 
       [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 
       [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
       [[UIApplication sharedApplication] registerForRemoteNotifications]; 
       [application registerForRemoteNotifications]; 
      } else { 
       // iOS 10 or later 
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
       UNAuthorizationOptions authOptions = 
       UNAuthorizationOptionAlert 
       | UNAuthorizationOptionSound 
       | UNAuthorizationOptionBadge; 
       [[UNUserNotificationCenter currentNotificationCenter] 
       requestAuthorizationWithOptions:authOptions 
       completionHandler:^(BOOL granted, NSError * _Nullable error) { 
       } 
       ]; 
       // For iOS 10 display notification (sent via APNS) 
       [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];    
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
     return YES; 
} 

現在實現這個方法適用於iOS10以下版本

- (void)application:(UIApplication *)application 
    didReceiveRemoteNotification:(NSDictionary *)userInfo 
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler { 
     NSLog(@"Notification received: %@", userInfo); 
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) 
     { 
      NSLog(@"iOS version >= 10. Let NotificationCenter handle this one."); 
      return; 
     } 
     NSLog(@"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo); 
      else{ 
      handler(UIBackgroundFetchResultNewData); 
} 

    } 

Apple在iOS10中引入了這兩種方法來接收推送通知。

寫這些方法也

// Receive displayed notifications for iOS 10 devices. 
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center 
      willPresentNotification:(UNNotification *)notification 
      withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { 

     NSDictionary *userInfo = notification.request.content.userInfo; 

     NSLog(@"%@", userInfo); 

      completionHandler(UNNotificationPresentationOptionAlert); 
    } 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 

    NSLog(@"Userinfo %@",response.notification.request.content.userInfo); 
// completionHandler(UNNotificationPresentationOptionAlert); 
     } 

就是這樣。

試試這個

+1

沒有這些方法是imho執行,而應用程序是背景 – luky

+0

耶沒有這些方法調用 – kemdo

相關問題