AppDelegate.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // AppDelegate.m
  3. // Apex And Drivers
  4. //
  5. // Created by Ray on 2018/5/26.
  6. // Copyright © 2018 USAI. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import <UserNotifications/UserNotifications.h>
  10. @interface AppDelegate ()<UNUserNotificationCenterDelegate>
  11. @end
  12. @implementation AppDelegate
  13. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  14. // Override point for customization after application launch.
  15. //消息推送注册
  16. UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
  17. [center setDelegate:self];
  18. UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
  19. [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
  20. if (granted) {
  21. NSLog(@"注册成功");
  22. }else{
  23. NSLog(@"注册失败");
  24. }
  25. }];
  26. [application registerForRemoteNotifications];
  27. return YES;
  28. }
  29. //完成注册后收到devicetoken
  30. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  31. //上传deviceToken给后台服务器
  32. NSString *fullStr = [deviceToken description];
  33. NSLog(@"fullstr= %@",fullStr);
  34. NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
  35. NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
  36. }
  37. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  38. NSLog(@"注册推送失败:%@",error);
  39. }
  40. //处理通知
  41. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  42. }
  43. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  44. NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
  45. }
  46. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
  47. NSLog(@"收到静默推送");
  48. }
  49. @end