iOS自定义推送消息提示框

2020-01-18 15:40:50刘景俊

然后再AppDelegate 实现以下方法


/** 自定义:APP被“推送”启动时处理推送消息处理(APP 未启动--》启动)*/- (void)receiveNotificationByLaunchingOptions:(NSDictionary *)launchOptions {
 if (!launchOptions)
  return;
 /*
  通过“远程推送”启动APP
  UIApplicationLaunchOptionsRemoteNotificationKey 远程推送Key
  */
 NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
 if (userInfo) {
  NSLog(@"n>>>[Launching RemoteNotification]:%@", userInfo);
 }
}

#pragma mark - 用户通知(推送)回调 _IOS 8.0以上使用

/** 已登记用户通知 */
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
 // 注册远程通知(推送)
 [application registerForRemoteNotifications];
}

#pragma mark - 远程通知(推送)回调

/** 远程通知注册成功委托 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
 NSString *myToken = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
 myToken = [myToken stringByReplacingOccurrencesOfString:@" " withString:@""];
 NSUserDefaults *kr = [NSUserDefaults standardUserDefaults];
 [kr setValue:myToken forKey:@"deviceToken"];
 [kr synchronize];
 [GeTuiSdk registerDeviceToken:myToken];
 [[PostDeviceToken sharedInstance] postUpDeviceToken];
 NSLog(@"n>>>[DeviceToken Success]:%@nn", myToken);
}

/** 远程通知注册失败委托 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
 [GeTuiSdk registerDeviceToken:@""];
 NSLog(@"n>>>[DeviceToken Error]:%@nn", error.description);
}


#pragma mark - APP运行中接收到通知(推送)处理
/** APP已经接收到“远程”通知(推送) - 透传推送消息 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
 // 处理APN
 NSLog(@"n>>>[Receive RemoteNotification - Background Fetch]:%@nn", userInfo);
 completionHandler(UIBackgroundFetchResultNewData);
 
 NSString *payLoadString = [[MyPrevent sharedInstance] dictionary:userInfo objectForKey:@"payload"];
 [[SpotPunch sharedInstance] spotPunch:@"999.999.1" pointTwo:@"21" info:payLoadString];
 // NSUserDefaults *kr = [NSUserDefaults standardUserDefaults];
 if (!([[STCommonInfo getAuthType] intValue] != 1)) {
  NSData *jsonData = [payLoadString dataUsingEncoding:NSUTF8StringEncoding];
  NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData
                options:NSJSONReadingMutableContainers
                 error:nil];
  
  STPushModel *model = [STPushModel modelObjectWithDict:jsonDic];
  [NSKeyedArchiver archiveRootObject:model toFile:KRAPI_PUSH_DATA];
//如果应用程序在前台 就显示客服提示框
  if (application.applicationState == UIApplicationStateActive) {
   self.topView.model = model;
   [self displayPushView]; //此方法 的实现 在上一步中 就是展示提示框出来
  }

  
 }
}