Ответ 1
Я использовал LNNotificationsUI для оповещения iOS 9 на iOS 9, когда приложение находится на переднем плане. С iOS 10 вы можете теперь отображать собственные системные уведомления внутри вашего приложение. При необходимости обратитесь к документу.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
...
//iOS9
[[LNNotificationCenter defaultCenter] registerApplicationWithIdentifier:@"123" name:@"YourAppName" icon:nil defaultSettings:[LNNotificationAppSettings defaultNotificationAppSettings]];
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
[self handelRemoteNotification:[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}
}
return YES;
}
//iOS9
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
PrintLog(@"%@",userInfo);
[self handelRemoteNotification:userInfo];
}
//iOS10
#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;
PrintLog(@"%@", userInfo);
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
PrintLog(@"%@", response.notification.request.content);
[self handelRemoteNotification:response.notification.request.content.userInfo];
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);
}
#endif
- (void)handelRemoteNotification:(NSDictionary*)userInfo {
NSDictionary *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
NSString* chatAction = [userInfo objectForKey:_remoteNotificationClick_action];
if ([chatAction isEqualToString:_remoteNotificationClickActionTypeChat]) {
if ([ChatViewController isOnTop] && [[userInfo objectForKey:_remoteNotificationReceiver_uid] isEqualToString:[ChatViewController userID]]) {
return;
}
}
LNNotification* notification = [LNNotification notificationWithMessage:[alert objectForKey:@"body"]];
notification.title = [alert objectForKey:@"title"];
notification.date = [NSDate date];
notification.icon = [UIImage imageNamed:@"ios_9_notification_20"];
notification.defaultAction = [LNNotificationAction actionWithTitle:@"View" handler:^(LNNotificationAction *action) {
[self actionForRemoteNotification:userInfo];
}];
[JSQSystemSoundPlayer jsq_playMessageReceivedAlert];
[[LNNotificationCenter defaultCenter] presentNotification:notification forApplicationIdentifier:@"123"];
}
else {
[self actionForRemoteNotification:userInfo];
}
}
else {
[self actionForRemoteNotification:userInfo];
}
}
- (void)actionForRemoteNotification:(NSDictionary*)userInfo {
...
Manage your code here
}