|
|
@@ -7,16 +7,41 @@
|
|
|
//
|
|
|
|
|
|
#import "AppDelegate.h"
|
|
|
+#import <UserNotifications/UserNotifications.h>
|
|
|
+#import "CRMUser.h"
|
|
|
+#import "RADataProvider.h"
|
|
|
+#import "CRMRemoteNotificationBroadcast.h"
|
|
|
|
|
|
-@interface AppDelegate ()
|
|
|
+@interface AppDelegate () <UNUserNotificationCenterDelegate>
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
+#pragma mark - App State
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
|
|
// Override point for customization after application launch.
|
|
|
+
|
|
|
+ // register remote notification
|
|
|
+ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
+ UNAuthorizationOptions options = UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge;
|
|
|
+ [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
|
+ if (granted) {
|
|
|
+ [application registerForRemoteNotifications];
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }];
|
|
|
+ center.delegate = self;
|
|
|
+
|
|
|
+ // 用户点击通知启动应用
|
|
|
+ if (launchOptions && application.applicationState == UIApplicationStateInactive) {
|
|
|
+ NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
|
|
|
+ if (userInfo) {
|
|
|
+ [self receiveNotification:userInfo];
|
|
|
+ }
|
|
|
+ }
|
|
|
return YES;
|
|
|
}
|
|
|
|
|
|
@@ -40,6 +65,8 @@
|
|
|
|
|
|
- (void)applicationDidBecomeActive:(UIApplication *)application {
|
|
|
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
|
|
+
|
|
|
+ [application setApplicationIconBadgeNumber:0];
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -47,5 +74,101 @@
|
|
|
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
|
|
}
|
|
|
|
|
|
+#pragma mark - Notification
|
|
|
+
|
|
|
+- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
|
|
|
+ NSLog(@"Fail To Register Remote Notification: %@",error);
|
|
|
+}
|
|
|
+
|
|
|
+- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
|
|
+
|
|
|
+ //向APNS注册成功,收到返回的deviceToken
|
|
|
+ NSString *realDeviceToken = [NSString stringWithFormat:@"%@",deviceToken];
|
|
|
+ //去除<>和空格
|
|
|
+ realDeviceToken = [realDeviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
|
|
|
+ realDeviceToken = [realDeviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];
|
|
|
+ realDeviceToken = [realDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
|
+
|
|
|
+ [CRMUser sharedUser].deviceToken = realDeviceToken;
|
|
|
+ if (CRMUser.sharedUser.shouldUploadToken) {
|
|
|
+ [RADataProvider uploadDeviceToken];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 用户点击通知的响应
|
|
|
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response withCompletionHandler:(nonnull void (^)(void))completionHandler {
|
|
|
+
|
|
|
+ // 用户点击
|
|
|
+ if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
|
|
|
+
|
|
|
+ NSDictionary *userInfo = response.notification.request.content.userInfo;
|
|
|
+ [self receiveNotification:userInfo];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 前台接收通知
|
|
|
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
|
|
|
+
|
|
|
+ NSDictionary *userInfo = notification.request.content.userInfo;
|
|
|
+ [self showDialogForNotification:userInfo];
|
|
|
+
|
|
|
+ if (completionHandler) {
|
|
|
+ completionHandler(UNNotificationPresentationOptionNone);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+content-available = 1
|
|
|
+ 前台收到通知:
|
|
|
+ 1. userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
|
|
|
+ 2. application:didReceiveRemoteNotification:fetchCompletionHandler:
|
|
|
+ 后台收到通知:
|
|
|
+ 1. application:didReceiveRemoteNotification:fetchCompletionHandler:
|
|
|
+
|
|
|
+ content-available = 0
|
|
|
+ 前台收到通知:
|
|
|
+ 1. userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
|
|
|
+ 后台收到通知:
|
|
|
+ 1. 没有任何回调
|
|
|
+
|
|
|
+静默通知
|
|
|
+ "content-available": 1,
|
|
|
+ "alert":""
|
|
|
+
|
|
|
+*/
|
|
|
+//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
|
|
+//
|
|
|
+// if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
|
|
|
+//
|
|
|
+// } else {
|
|
|
+//
|
|
|
+// }
|
|
|
+// if (completionHandler) {
|
|
|
+// completionHandler(UIBackgroundFetchResultNewData);
|
|
|
+// }
|
|
|
+//}
|
|
|
+
|
|
|
+#pragma mark - Handle Notification
|
|
|
+
|
|
|
+- (void)showDialogForNotification:(NSDictionary *)userInfo {
|
|
|
+
|
|
|
+ __weak typeof(self) weakSelf = self;
|
|
|
+ UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"you have a message, read it now?" preferredStyle:UIAlertControllerStyleAlert];
|
|
|
+ UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ [weakSelf receiveNotification:userInfo];
|
|
|
+ }];
|
|
|
+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ [alertVC addAction:cancelAction];
|
|
|
+ [alertVC addAction:okAction];
|
|
|
+
|
|
|
+ [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)receiveNotification:(NSDictionary *)userInfo {
|
|
|
+ [[NSNotificationCenter defaultCenter] postNotificationName:CRM_REMOTE_NOTIFICATION_RECEVIED object:self userInfo:userInfo];
|
|
|
+}
|
|
|
|
|
|
@end
|