RCTMIPushModule.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // RCTMIPushModule.m
  3. // RCTMIPushModule
  4. //
  5. // Created by zhangzy on 2016/10/24.
  6. // Copyright © 2016年 zzy. All rights reserved.
  7. //
  8. #import "RCTMIPushModule.h"
  9. @implementation RCTMIPushModule
  10. RCT_EXPORT_MODULE();
  11. RCT_EXPORT_METHOD(setAlias:(NSString *)text)
  12. {
  13. [MiPushSDK setAlias:text];
  14. }
  15. RCT_EXPORT_METHOD(unsetAlias:(NSString *)text)
  16. {
  17. [MiPushSDK unsetAlias:text];
  18. }
  19. RCT_EXPORT_METHOD(subscribe:(NSString *)text)
  20. {
  21. [MiPushSDK subscribe:text];
  22. }
  23. RCT_EXPORT_METHOD(unsubscribe:(NSString *)text)
  24. {
  25. [MiPushSDK unsubscribe:text];
  26. }
  27. RCT_EXPORT_METHOD(setAccount:(NSString *)text)
  28. {
  29. [MiPushSDK setAccount:text];
  30. }
  31. RCT_EXPORT_METHOD(unsetAccount:(NSString *)text)
  32. {
  33. [MiPushSDK unsetAccount:text];
  34. }
  35. - (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
  36. {
  37. NSLog(@"data:%@", data);
  38. }
  39. - (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
  40. {
  41. // 请求失败
  42. NSLog(@"error:%@", error);
  43. }
  44. - (void)miPushReceiveNotification:(NSDictionary *)data {
  45. NSLog(@"data2:%@", data);
  46. }
  47. + (void)application:(id)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  48. [MiPushSDK registerMiPush:self];
  49. NSLog(@"注册");
  50. }
  51. + (void)application:(id)application didRegisterUserNotificationSettings:(id)notificationSettings {
  52. [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
  53. }
  54. + (void)application:(id)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  55. [MiPushSDK bindDeviceToken:deviceToken];
  56. [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  57. }
  58. + (void)application:(id)application didReceiveRemoteNotification:(NSDictionary *)notification {
  59. [RNCPushNotificationIOS didReceiveRemoteNotification:notification];
  60. }
  61. + (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  62. [RNCPushNotificationIOS didReceiveLocalNotification:notification];
  63. [[NSNotificationCenter defaultCenter] postNotificationName:@"xmpush_click" object:notification.userInfo];
  64. }
  65. // 应用在前台收到通知
  66. + (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  67. NSDictionary * userInfo = notification.request.content.userInfo;
  68. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  69. [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo];
  70. }
  71. }
  72. // 点击通知进入应用
  73. + (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  74. NSDictionary * userInfo = response.notification.request.content.userInfo;
  75. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  76. [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo];
  77. }
  78. [[NSNotificationCenter defaultCenter] postNotificationName:@"xmpush_click" object:userInfo];
  79. completionHandler();
  80. }
  81. - (void) handleSend:(NSNotification *)notification {
  82. [self sendEventWithName:notification.name body:notification.object];
  83. }
  84. - (void)startObserving
  85. {
  86. [[NSNotificationCenter defaultCenter] addObserver:self
  87. selector:@selector(handleSend:)
  88. name:@"xmpush_click"
  89. object:nil];
  90. }
  91. - (void)stopObserving
  92. {
  93. [[NSNotificationCenter defaultCenter] removeObserver:self];
  94. }
  95. - (NSArray<NSString *> *)supportedEvents {
  96. NSMutableArray *arr = [[NSMutableArray alloc] init];
  97. [arr addObject:@"xmpush_click"];
  98. return arr;
  99. }
  100. @end