#import <UserNotifications/UserNotifications.h>@interface AppDelegate () <UNUserNotificationCenterDelegate>@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"启动了");
[self setNormalPushNotification];
return YES;}// 初始化普通推送- (void)setNormalPushNotification {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"是否允许通知: %d", granted);
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
// ⚠️注意: 如果不设置delegate,普通推送也会走didReceiveRemoteNotification
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];}// App在前台时收到普通推送- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary* userInfo = notification.request.content.userInfo;
NSLog(@"前台收到普通推送: %@", userInfo);}// App在后台时点击普通推送栏- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
NSDictionary* userInfo = response.notification.request.content.userInfo;
NSLog(@"后台收到普通推送: %@", userInfo);
completionHandler();}// APNS注册成功,返回token- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
if (![deviceToken isKindOfClass:[NSData class]]) return;
const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
NSLog(@"deviceToken:%@",hexToken);}// APNS注册失败- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error: %@", error);}// App在任何状态下收到静默推送- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"收到静默推送: %@", userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:@"Recieve" object:nil];
UIApplicationState state = [UIApplication sharedApplication].applicationState;
if(state == UIApplicationStateBackground) {
NSLog(@"App在后台,运行5秒");
} else if (state == UIApplicationStateActive) {
NSLog(@"App在前台");
}
// 5秒后通知系统将处于后台的App挂起(suspend)
[NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer * _Nonnull timer) {
completionHandler(UIBackgroundFetchResultNewData);
}];}@end
// 静默推送{"aps":{"content-available":1}}// 普通推送{"aps":{"alert":"普通推送","sound":"default","userinfo":{"username":"tom"}}}