AjFlutterAppspPushPlugin.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // AjFlutterAppspPushPlugin.m
  3. // PushDemo
  4. //
  5. // Created by zfl
  6. // Copyright © 2018 anji-plus 安吉加加信息技术有限公司 http://www.anji-plus.com. All rights reserved.
  7. //
  8. #import "AjFlutterAppspPushPlugin.h"
  9. #import <objc/runtime.h>
  10. #import "JPUSHService.h"
  11. #import "AJPushSDK.h"
  12. @interface AjFlutterAppspPushPlugin()
  13. @end
  14. @implementation AjFlutterAppspPushPlugin
  15. static AjFlutterAppspPushPlugin *sharedInstance;
  16. + (instancetype)sharedInstance {
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. sharedInstance = [[AjFlutterAppspPushPlugin alloc] init];
  20. [sharedInstance initNSNotificationCenter];
  21. });
  22. return sharedInstance;
  23. }
  24. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  25. FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"aj_flutter_appsp_push" binaryMessenger:[registrar messenger]];
  26. AjFlutterAppspPushPlugin* instance = [AjFlutterAppspPushPlugin sharedInstance];
  27. instance.channel = channel;
  28. [registrar addMethodCallDelegate:instance channel:channel];
  29. [registrar addApplicationDelegate:instance];
  30. }
  31. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  32. if ([@"getPlatformVersion" isEqualToString:call.method]) {
  33. result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  34. } else if ([@"getDeviceId" isEqualToString:call.method]) {
  35. result([UIDevice currentDevice].identifierForVendor.UUIDString ?: @"");
  36. } else {
  37. result(FlutterMethodNotImplemented);
  38. }
  39. }
  40. #pragma mark - AppDelegate
  41. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  42. #ifdef DEBUG
  43. BOOL pro = NO;
  44. #else
  45. BOOL pro = YES;
  46. #endif
  47. NSString *path = [[NSBundle mainBundle] pathForResource:@"AJPush" ofType:@"plist"];
  48. NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
  49. if (dic && path) {
  50. [self setDataWithDic:dic];
  51. [self initAPNSWithOptions:launchOptions apsForProduction:pro];
  52. } else {
  53. NSLog(@"初始化失败");
  54. }
  55. return YES;
  56. }
  57. /** 成功并上报 DeviceToken */
  58. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  59. [[AJPushService shareService] registerDeviceToken:deviceToken withUrl:self.host];
  60. }
  61. /** 远程通知注册失败委托 */
  62. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  63. NSLog(@"didFailToRegisterForRemoteNotificationsWithError");
  64. }
  65. - (void)applicationDidEnterBackground:(UIApplication*)application {
  66. [JPUSHService resetBadge];
  67. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  68. }
  69. #pragma mark - private
  70. - (void)setBadge:(FlutterMethodCall*)call result:(FlutterResult)result {
  71. NSDictionary *ConfigurationInfo = call.arguments;
  72. NSUInteger value = [ConfigurationInfo[@"badge"] integerValue];
  73. [JPUSHService setBadge:value];
  74. }
  75. //初始化推送服务
  76. - (void)initAPNSWithOptions:(NSDictionary *)launchOptions apsForProduction:(BOOL)isPro{
  77. [[AJPushService shareService] registerJPushOption:launchOptions appKey:self.jpushAppKey ajAppKey:self.appspAppKey channel:self.jchannel apsForProduction:isPro];
  78. }
  79. //注册推送通知中心
  80. - (void)initNSNotificationCenter {
  81. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  82. [defaultCenter addObserver:self
  83. selector:@selector(noticeBarMessage:)
  84. name:kAJPushServiceNoticeBarMessageNotification
  85. object:nil];
  86. [defaultCenter addObserver:self
  87. selector:@selector(innerMessage:)
  88. name:kAJPushServiceInnerMessageNotification
  89. object:nil];
  90. [defaultCenter addObserver:self
  91. selector:@selector(customMessage:)
  92. name:kAJPushServiceCustomMessageNotification
  93. object:nil];
  94. }
  95. //消息栏通知响应通知
  96. - (void)noticeBarMessage:(NSNotification *)notification {
  97. NSDictionary *userInfo = notification.userInfo;
  98. [_channel invokeMethod:@"onMessageClicked" arguments:[self formattedJsonStr:userInfo]];
  99. }
  100. //应用内部响应通知
  101. - (void)innerMessage:(NSNotification *)notification {
  102. NSDictionary *userInfo = notification.userInfo;
  103. NSMutableDictionary *extras = [NSMutableDictionary dictionary];
  104. NSString *title = @"";
  105. NSString *body = @"";
  106. if ([userInfo[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]]) {
  107. title = userInfo[@"aps"][@"alert"][@"title"] ?: @"";
  108. body = userInfo[@"aps"][@"alert"][@"body"] ?: @"";
  109. }
  110. for (NSString * key in userInfo.allKeys) {
  111. if (![key isEqualToString:@"_j_business"] && ![key isEqualToString:@"_j_data_"] && ![key isEqualToString:@"_j_msgid"] &&
  112. ![key isEqualToString:@"_j_uid"] && ![key isEqualToString:@"aps"]) {
  113. [extras setObject:userInfo[key] ?: @""forKey:key];
  114. }
  115. }
  116. NSDictionary *dic = @{@"content":body,
  117. @"title":title,
  118. @"extras":extras};
  119. [_channel invokeMethod:@"onMessageArrived" arguments:[self formattedJsonStr:dic]];
  120. }
  121. //自定义消息响应通知
  122. - (void)customMessage:(NSNotification *)notification {
  123. NSDictionary *userInfo = notification.userInfo;
  124. [_channel invokeMethod:@"onMessageArrived" arguments:[self formattedJsonStr:userInfo]];
  125. }
  126. - (void)setDataWithDic:(NSDictionary *)dic {
  127. AjFlutterAppspPushPlugin* instance = [AjFlutterAppspPushPlugin sharedInstance];
  128. instance.host = [dic objectForKey:@"AppSpTestURL"];
  129. instance.appspAppKey = [dic objectForKey:@"APPSP_APPKEY"];
  130. instance.appspSecretKey = [dic objectForKey:@"APPSP_SECRETKEY"];
  131. instance.jpushAppKey = [dic objectForKey:@"JG_APPKEY"];
  132. instance.jchannel = [dic objectForKey:@"JG_CHANNEL"];
  133. }
  134. - (NSString *)formattedJsonStr:(NSDictionary *)dic
  135. {
  136. NSError *error = nil;
  137. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
  138. options:NSJSONWritingPrettyPrinted
  139. error:&error];
  140. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  141. return jsonString;
  142. }
  143. @end