| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // AppDelegate.m
- // Apex And Drivers
- //
- // Created by Ray on 2018/5/26.
- // Copyright © 2018 USAI. All rights reserved.
- //
- #import "AppDelegate.h"
- #import <UserNotifications/UserNotifications.h>
- @interface AppDelegate ()<UNUserNotificationCenterDelegate>
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- //消息推送注册
- UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
- [center setDelegate:self];
- UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
- [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
- if (granted) {
- NSLog(@"注册成功");
- }else{
- NSLog(@"注册失败");
- }
- }];
- [application registerForRemoteNotifications];
-
- return YES;
- }
- //完成注册后收到devicetoken
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- //上传deviceToken给后台服务器
- NSString *fullStr = [deviceToken description];
- NSLog(@"fullstr= %@",fullStr);
- NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
- NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- NSLog(@"注册推送失败:%@",error);
- }
- //处理通知
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
-
- }
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
- NSLog(@"接收到推送内容==%@", response.notification.request.content.userInfo);
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
- NSLog(@"收到静默推送");
- }
- @end
|